SQL SERVER T-SQL按组计算子总计和总计

时间:2015-09-25 16:17:59

标签: sql sql-server tsql subtotal rollup

我正在尝试按组和总计添加小计到表格。我使用以下示例重新创建了数据。

DECLARE @Sales TABLE(
        CustomerName  VARCHAR(20),
        LegalID VARCHAR(20),
        Employee VARCHAR(20),
        DocDate DATE,
        DocTotal Int,
        DueTotal Int
)
INSERT INTO @Sales SELECT 'Jhon Titor','12345', 'Employee1','2015-09-01',1000,200 
INSERT INTO @Sales SELECT 'Jhon Titor','12345', 'Employee1','2015-08-20',500,100
INSERT INTO @Sales SELECT 'Jhon Titor','12345', 'Employee1','2015-08-18',200,50 
INSERT INTO @Sales SELECT 'Deli Armstrong','2345', 'Employee1','2015-09-17',2300,700
INSERT INTO @Sales SELECT 'Deli Armstrong','2345', 'Employee1','2015-09-11',5000,1000
INSERT INTO @Sales SELECT 'Ali Mezzu','6789', 'Employee1','2015-09-07',300,200

选择@Sales

enter image description here

我需要在客户事件的下方添加客户小计,并在表格的最后一行添加总计,如下所示:

enter image description here

我到目前为止所尝试的内容:

select 
    case 
        when GROUPING(CustomerName) = 1 and
             GROUPING(Employee) = 1 and 
             GROUPING(DocDate) = 1 and
             GROUPING(LegalID) = 0 then 'Total ' + CustomerName

        when GROUPING(CustomerName) = 1 and
             GROUPING(Employee) = 1 and
             GROUPING(DocDate) =1 and
             GROUPING(LegalID) = 1 then 'Total'

        else CustomerName end as CustomerName,
    LegalID, Employee,DocDate,
    sum(DocTotal) as DocTotal,
    sum(DueTotal) as DueTotal 
From @Sales 
group by LegalID, CustomerName,Employee,DocDate with rollup

但是当我在查询中将其设置为静态时,我得到小数为null,它应该说Total Jhon Titor,对于每个未聚合的列(3)重复它,

enter image description here

如何在上表中添加小计和总计?

我愿意使用没有ROLLUP运算符的查询。我认为使用工会是可能的,但不知道如何开始。

感谢您考虑我的问题。

2 个答案:

答案 0 :(得分:6)

我认为这就是你想要的:

select (case when GROUPING(CustomerName) = 0 and
                  GROUPING(Employee) = 1 and 
                  GROUPING(DocDate) = 1 and
                  GROUPING(LegalID) = 1
             then 'Total ' + CustomerName
             when GROUPING(CustomerName) = 1 and
                  GROUPING(Employee) = 1 and
                  GROUPING(DocDate) =1 and
                  GROUPING(LegalID) = 1 then 'Total'
             else CustomerName
        end) as CustomerName,
       LegalID, Employee,DocDate,
       sum(DocTotal) as DocTotal,
       sum(DueTotal) as DueTotal 
From @Sales 
group by grouping sets((LegalID, CustomerName ,Employee, DocDate),
                       (CustomerName),
                       ()
                      );

答案 1 :(得分:2)

您可以使用以下查询:

SELECT CustomerName, LegalID, Employee, DocDate, DocTotal, DueTotal
FROM (       
  SELECT CustomerName AS cName, CustomerName, 
         LegalID, Employee, DocDate, DocTotal, DueTotal,
         1 AS ord
  FROM Sales

  UNION ALL

  SELECT CustomerName AS cName, CONCAT('Total ', CustomerName), 
         NULL, NULL, NULL, 
         SUM(DocTotal), SUM(DueTotal), 2 AS ord
  FROM Sales
  GROUP BY CustomerName

  UNION ALL 

  SELECT 'ZZZZ' AS cName, 'Total', NULL, NULL, NULL, 
         SUM(DocTotal), SUM(DueTotal), 3 AS ord
  FROM Sales ) AS t
ORDER BY cName, ord

Demo here