我正在运行下面的SQL语句,但最后我需要总计。使用SQL Server
SELECT tblAllocation.Allocation AS TimeSlots, COUNT(*) AS [Applicant Total]
FROM tblAllocation
INNER JOIN tblRegistration ON tblAllocation.AllocationID = tblRegistration.AllocationID
INNER JOIN tblcontact ON tblRegistration.ContactID = tblcontact.ContactID
INNER JOIN tbladdress AS tbladdress_1 ON tblcontact.AddressID = tbladdress_1.AddressID
INNER JOIN tblNadraRegistrationDates ON tblRegistration.NRegDateID = tblNadraRegistrationDates.NRegDateID
WHERE (tblNadraRegistrationDates.[Nadra Reg Dates] = '26 April 2015')
GROUP BY tblAllocation.Allocation;
以下是
TimeSlots Applicant Total
09:30 to 10:30 3
10:30 to 11:30 10
12:30 to 13:30 11
Reservation 8
TOTAL 32 <--- how can I get this
答案 0 :(得分:3)
对WITH ROLLUP
语句使用GROUP BY
选项:
SELECT
CASE
WHEN GROUPING(tblAllocation.Allocation) = 1 THEN 'Total'
ELSE tblAllocation.Allocation
END AS TimeSlots,
COUNT(*) AS [Applicant Total]
FROM
tblAllocation
INNER JOIN
tblRegistration ON tblAllocation.AllocationID = tblRegistration.AllocationID
INNER JOIN
tblcontact ON tblRegistration.ContactID = tblcontact.ContactID
INNER JOIN
tbladdress AS tbladdress_1 ON tblcontact.AddressID = tbladdress_1.AddressID
INNER JOIN
tblNadraRegistrationDates ON tblRegistration.NRegDateID = tblNadraRegistrationDates.NRegDateID
WHERE
(tblNadraRegistrationDates.[Nadra Reg Dates] = '26 April 2015')
GROUP BY
tblAllocation.Allocation WITH ROLLUP;
您可能希望对表名使用别名来缩短查询文本,并可能使其更具可读性(但这当然只是一种意见)。