想要计数,但每条记录都重复 1

Want a count but it repeats 1 with every record

我想要一个计数,但它对每条记录都重复 1。你能建议怎么做吗?

SELECT Count(*),
       innerTable.*
FROM   (SELECT (SELECT NAME
                FROM   tours
                WHERE  tours.id = tourbooking.tourid)         AS NAME,
               (SELECT url
                FROM   tours
                WHERE  tours.id = tourbooking.tourid)         AS Url,
               (SELECT TOP 1 NAME
                FROM   tourimages
                WHERE  tourimages.tourid = tourbooking.tourid
                ORDER  BY id ASC)                             AS ImageName,
               (SELECT duration + ' ' + CASE WHEN durationtype = 'd' THEN
                       'Day(s)' WHEN
                       durationtype =
                       'h' THEN 'Hour(s)' END
                FROM   tours
                WHERE  tours.id = tourbooking.tourid)         AS Duration,
               (SELECT Replace(Replace('<a> Adult(s) - <c> Children', '<a>', Sum
                               (CASE
                                       WHEN [type] = 1 THEN 1
                                       ELSE 0
                                END)),
                       '<c>',
                       Sum(CASE
                       WHEN [type] = 2 THEN 1
                       ELSE 0
                       END))
                FROM   tourperson
                WHERE  tourperson.bookingid = tourbooking.id) AS TotalPassengers
               ,
               startdate,
               createddate                                    AS BookingDate,
               id                                             AS BookingID,
               [status],
               serviceprice
        FROM   tourbooking
        WHERE  memberid = 6)AS innerTable
GROUP  BY innerTable.NAME,
          innerTable.bookingdate,
          innerTable.bookingid,
          innerTable.duration,
          innerTable.imagename,
          innerTable.serviceprice,
          innerTable.startdate,
          innerTable.status,
          innerTable.totalpassengers,
          innerTable.url 

您 select 记录来自 tourbooking。您 select 的其中一列是 id。这可能是 table 的主键,因此是唯一的。 (如果没有,你就赶紧改名字吧。)

您将此 ID 称为 BookingID,它是您分组所依据的列之一。因此,您在 tourbooking 中每条记录获得一个结果记录。这样一个"group"里面的记录数当然是1;这是您 select 并显示的记录。

如果你建立了真实组,比如每记录一个结果,那么你会得到一个真实的计数,例如每天的预订数量。