添加总计特定列的总计行

时间:2018-03-16 17:32:09

标签: sql sql-server tsql

我在MS SQL Server 2014环境中有以下查询,我想添加" GRAND_TOTALS"底部的行,用于汇总聚合(COUNT)列(Total_Exams)中的值。理想情况下,寻找相对干净的东西,如果可能的话,不必使用临时表。谢谢!

SELECT 

A.TEST_TYPE,(CONVERT(CHAR(10),A.TEST_DT,121)), B.UNIT, COUNT(*) AS 
'Total_Tests'

FROM USERSCT B, SEARCHPARAM B1, USERTEST A, CUSTOMEXAM C

WHERE ( B.USERID = B1.USERID
AND B.USRACCT = B1.USRACCT
AND ( B.ASOFDATE =
    (SELECT MAX(B_ED.ASOFDATE) FROM USERSCT B_ED
    WHERE B.USERID= B_ED.USERID
      AND B.USRACCT = B_ED.USRACCT
      AND B_ED.ASOFDATE <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10))
AND B.ASOFTIME =
    (SELECT MAX(B_ES.ASOFTIME) FROM PS_EMPLOYEES B_ES
    WHERE B.USERID = B_ES.USERID
      AND B.USRACCT = B_ES.USRACCT
      AND B.ASOFDATE = B_ES.ASOFDATE)
 AND B.USERID = A.USERID
 AND A.TEST_TYPE = 'CHISQUARE'
 AND A.USERID = C.USERID

 GROUP BY  A.TEST_TYPE,  A.TEST_DT,  B.UNIT

当前样本结果:

enter image description here

期望的结果:

enter image description here

1 个答案:

答案 0 :(得分:1)

Usin CTE

WITH cte AS
(
SELECT 

Cast(A.TEST_TYPE as Varchar) as TEST_TYPE, cast((CONVERT(CHAR(10),A.TEST_DT,121)) as varchar) as Date_Col, cast(B.UNIT as varchar) AS UNIT, COUNT(*) AS 
'Total_Tests'

FROM USERSCT B, SEARCHPARAM B1, USERTEST A, CUSTOMEXAM C

WHERE ( B.USERID = B1.USERID
AND B.USRACCT = B1.USRACCT
AND ( B.ASOFDATE =
    (SELECT MAX(B_ED.ASOFDATE) FROM USERSCT B_ED
    WHERE B.USERID= B_ED.USERID
      AND B.USRACCT = B_ED.USRACCT
      AND B_ED.ASOFDATE <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10))
AND B.ASOFTIME =
    (SELECT MAX(B_ES.ASOFTIME) FROM PS_EMPLOYEES B_ES
    WHERE B.USERID = B_ES.USERID
      AND B.USRACCT = B_ES.USRACCT
      AND B.ASOFDATE = B_ES.ASOFDATE)
 AND B.USERID = A.USERID
 AND A.TEST_TYPE = 'CHISQUARE'
 AND A.USERID = C.USERID

 GROUP BY  A.TEST_TYPE,  A.TEST_DT,  B.UNIT
)
SELECT * FROM cte
UNION
SELECT 'Grand Total','','',SUM(Total_Tests)
FROM cte

或使用嵌套查询

Select * from(
SELECT 

Cast(A.TEST_TYPE as Varchar) as TEST_TYPE,cast((CONVERT(CHAR(10),A.TEST_DT,121)) as varchar) as Date_Col, cast(B.UNIT as varchar) UNIT, COUNT(*) AS 
'Total_Tests'

FROM USERSCT B, SEARCHPARAM B1, USERTEST A, CUSTOMEXAM C

WHERE ( B.USERID = B1.USERID
AND B.USRACCT = B1.USRACCT
AND ( B.ASOFDATE =
    (SELECT MAX(B_ED.ASOFDATE) FROM USERSCT B_ED
    WHERE B.USERID= B_ED.USERID
      AND B.USRACCT = B_ED.USRACCT
      AND B_ED.ASOFDATE <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10))
AND B.ASOFTIME =
    (SELECT MAX(B_ES.ASOFTIME) FROM PS_EMPLOYEES B_ES
    WHERE B.USERID = B_ES.USERID
      AND B.USRACCT = B_ES.USRACCT
      AND B.ASOFDATE = B_ES.ASOFDATE)
 AND B.USERID = A.USERID
 AND A.TEST_TYPE = 'CHISQUARE'
 AND A.USERID = C.USERID

 GROUP BY  A.TEST_TYPE,  A.TEST_DT,  B.UNIT
) as t1

UNION

SELECT 'Grand Total','','',SUM(Total_Tests)
FROM (SELECT 

Cast(A.TEST_TYPE as Varchar) as TEST_TYPE,cast((CONVERT(CHAR(10),A.TEST_DT,121)) as varchar) as Date_Col, cast(B.UNIT as varchar) UNIT, COUNT(*) AS 
'Total_Tests'

FROM USERSCT B, SEARCHPARAM B1, USERTEST A, CUSTOMEXAM C

WHERE ( B.USERID = B1.USERID
AND B.USRACCT = B1.USRACCT
AND ( B.ASOFDATE =
    (SELECT MAX(B_ED.ASOFDATE) FROM USERSCT B_ED
    WHERE B.USERID= B_ED.USERID
      AND B.USRACCT = B_ED.USRACCT
      AND B_ED.ASOFDATE <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10))
AND B.ASOFTIME =
    (SELECT MAX(B_ES.ASOFTIME) FROM PS_EMPLOYEES B_ES
    WHERE B.USERID = B_ES.USERID
      AND B.USRACCT = B_ES.USRACCT
      AND B.ASOFDATE = B_ES.ASOFDATE)
 AND B.USERID = A.USERID
 AND A.TEST_TYPE = 'CHISQUARE'
 AND A.USERID = C.USERID

 GROUP BY  A.TEST_TYPE,  A.TEST_DT,  B.UNIT
)as t2