将总行添加到SQL Query的底部

时间:2016-11-02 21:40:56

标签: sql sql-server tsql grouping

我有一个SQL语句可以获得每日销售,但我希望总计这些行,可能使用CTE。

我的代码如下,我尝试使用GROUPING和ROLLUP但无济于事。非常感谢任何帮助!

 DECLARE @StartDate NVARCHAR(MAX) = '20161101'
 DECLARE @FinishDate NVARCHAR(MAX) = '20161102'

 SELECT salesquery.Department, 
    SUM(CASE WHEN salesquery.Type = 'Invoice' THEN salesquery.Cost ELSE -salesquery.Cost END) AS 'Cost',
    SUM(CASE WHEN salesquery.Type = 'Invoice' THEN salesquery.[GP $] ELSE -salesquery.[GP $] END) AS 'GP $',  
    SUM(CASE WHEN salesquery.Type = 'Invoice' THEN salesquery.TotalExGST ELSE -salesquery.TotalExGST END) AS 'TotalExGST', 
    SUM(CASE WHEN salesquery.Type = 'Invoice' THEN salesquery.TotalExGST ELSE -salesquery.TotalExGST END) * 1.1 AS 'TotalInclGST', 
    SUM(CASE WHEN salesquery.Type = 'Invoice' THEN salesquery.[GP $] ELSE -salesquery.[GP $] END) / SUM(CASE WHEN salesquery.Type = 'Invoice' THEN salesquery.TotalExGST ELSE -salesquery.TotalExGST END) * 100 AS 'GP %'

    FROM 
      (SELECT 
        iid.DepartmentCode AS 'Department', 
        ci.InvoiceDate,
        ci.Type,
        ci.InvoiceCode,
        SUM(cid.ExtActualCost) AS 'Cost', 
        SUM(cid.ExtPrice) + MAX(ci.Freight) + MAX(ci.Other) AS 'TotalExGST', 
        (SUM(cid.ExtPrice) - SUM(cid.ExtActualCost)) AS 'GP $',
        (CASE WHEN SUM(cid.ExtPrice) = 0 THEN 0 ELSE ((SUM(cid.ExtPrice) - SUM(cid.ExtActualCost)) / SUM(cid.ExtPrice)) END) * 100 as 'GP %'



      FROM CustomerInvoice ci
      JOIN CustomerInvoiceDetail cid ON ci.InvoiceCode = cid.InvoiceCode
      JOIN InventoryItemDepartment iid ON cid.ItemCode = iid.ItemCode

      WHERE ci.IsVoided = 0
        AND ci.InvoiceDate BETWEEN @StartDate AND @FinishDate
      GROUP BY ci.invoicecode, iid.DepartmentCode, ci.Type, ci.InvoiceDate) salesquery

  GROUP BY salesquery.Department

这给了我这样的样本输出

╔════════════╦══════════════╦══════════════╦══════════════╦════════════════╦═══════════╗
║ Department ║ Cost         ║ GP $         ║ Total Ex GST ║ Total Incl GST ║ GP %      ║
╠════════════╬══════════════╬══════════════╬══════════════╬════════════════╬═══════════╣
║ EP         ║ 4720.262000  ║ 8076.918000  ║ 13179.180000 ║ 14497.098000   ║ 61.285400 ║
╠════════════╬══════════════╬══════════════╬══════════════╬════════════════╬═══════════╣
║ F          ║ 11307.420000 ║ 11465.690000 ║ 23210.110000 ║ 25531.121000   ║ 49.399500 ║
╠════════════╬══════════════╬══════════════╬══════════════╬════════════════╬═══════════╣
║ M          ║ 85.860000    ║ 45.310000    ║ 131.170000   ║ 144.287000     ║ 34.542900 ║
╚════════════╩══════════════╩══════════════╩══════════════╩════════════════╩═══════════╝

我希望表格输出表格行'Total',它会添加上面的行,并平均最后一列。

╔════════════╦══════════════╦══════════════╦══════════════╦════════════════╦════════════╗
║ Department ║ Cost         ║ GP $         ║ Total Ex GST ║ Total Incl GST ║ GP %       ║
╠════════════╬══════════════╬══════════════╬══════════════╬════════════════╬════════════╣
║ EP         ║ 4720.262000  ║ 8076.918000  ║ 13179.180000 ║ 14497.098000   ║ 61.285400  ║
╠════════════╬══════════════╬══════════════╬══════════════╬════════════════╬════════════╣
║ F          ║ 11307.420000 ║ 11465.690000 ║ 23210.110000 ║ 25531.121000   ║ 49.399500  ║
╠════════════╬══════════════╬══════════════╬══════════════╬════════════════╬════════════╣
║ M          ║ 85.860000    ║ 45.310000    ║ 131.170000   ║ 144.287000     ║ 34.542900  ║
╠════════════╬══════════════╬══════════════╬══════════════╬════════════════╬════════════╣
║ Total      ║ 11612.23     ║ 19587.70     ║ etc          ║ etc            ║ AVG(Above) ║
╚════════════╩══════════════╩══════════════╩══════════════╩════════════════╩════════════╝

3 个答案:

答案 0 :(得分:1)

我倾向于使用GROUPING SETS。对于上一个GROUP BY

GROUP BY GROUPING SETS ((salesquery.Department), ())

您可以使用惰性方法(Department)或正确方法(使用COALESCE(salesquery.Department, 'Total') as Department)更改GROUPING()

答案 1 :(得分:1)

在末尾使用UNION重新求和您的原始查询并将其追加......就像这样(概念问题):

DECLARE @StartDate NVARCHAR(MAX) = '20161101'
DECLARE @FinishDate NVARCHAR(MAX) = '20161102'

SELECT a.*
FROM (

    -- Your original query here

) AS a

UNION

SELECT SUM(b.This), SUM(b.That) FROM (

    -- Your original query here

) AS b

可能有更好的方法,但这可行。无论如何,你都需要重新查询原始集合(或者使用光标然后去RBAR,但这会更糟)

我倾向于说使用任何UI显示来对原始结果求和(不包括在查询中)可能是一种更标准的方法。

答案 2 :(得分:0)

您可以使用UNION并通过统一选择

中的salesquery.Department来重新组合该组
 SELECT salesquery.Department, 
    SUM(CASE WHEN salesquery.Type = 'Invoice' THEN salesquery.Cost ELSE -salesquery.Cost END) AS 'Cost',
    SUM(CASE WHEN salesquery.Type = 'Invoice' THEN salesquery.[GP $] ELSE -salesquery.[GP $] END) AS 'GP $',  
    SUM(CASE WHEN salesquery.Type = 'Invoice' THEN salesquery.TotalExGST ELSE -salesquery.TotalExGST END) AS 'TotalExGST', 
    SUM(CASE WHEN salesquery.Type = 'Invoice' THEN salesquery.TotalExGST ELSE -salesquery.TotalExGST END) * 1.1 AS 'TotalInclGST', 
    SUM(CASE WHEN salesquery.Type = 'Invoice' THEN salesquery.[GP $] ELSE -salesquery.[GP $] END) / SUM(CASE WHEN salesquery.Type = 'Invoice' THEN salesquery.TotalExGST ELSE -salesquery.TotalExGST END) * 100 AS 'GP %'

    FROM 
      (SELECT 
        iid.DepartmentCode AS 'Department', 
        ci.InvoiceDate,
        ci.Type,
        ci.InvoiceCode,
        SUM(cid.ExtActualCost) AS 'Cost', 
        SUM(cid.ExtPrice) + MAX(ci.Freight) + MAX(ci.Other) AS 'TotalExGST', 
        (SUM(cid.ExtPrice) - SUM(cid.ExtActualCost)) AS 'GP $',
        (CASE WHEN SUM(cid.ExtPrice) = 0 THEN 0 ELSE ((SUM(cid.ExtPrice) - SUM(cid.ExtActualCost)) / SUM(cid.ExtPrice)) END) * 100 as 'GP %'



      FROM CustomerInvoice ci
      JOIN CustomerInvoiceDetail cid ON ci.InvoiceCode = cid.InvoiceCode
      JOIN InventoryItemDepartment iid ON cid.ItemCode = iid.ItemCode

      WHERE ci.IsVoided = 0
        AND ci.InvoiceDate BETWEEN @StartDate AND @FinishDate
      GROUP BY ci.invoicecode, iid.DepartmentCode, ci.Type, ci.InvoiceDate) salesquery

  GROUP BY salesquery.Department

  UNION 


 SELECT 'Total , 
    SUM(CASE WHEN salesquery.Type = 'Invoice' THEN salesquery.Cost ELSE -salesquery.Cost END) ,
    SUM(CASE WHEN salesquery.Type = 'Invoice' THEN salesquery.[GP $] ELSE -salesquery.[GP $] END) ,  
    SUM(CASE WHEN salesquery.Type = 'Invoice' THEN salesquery.TotalExGST ELSE -salesquery.TotalExGST END) , 
    SUM(CASE WHEN salesquery.Type = 'Invoice' THEN salesquery.TotalExGST ELSE -salesquery.TotalExGST END) * 1.1 , 
    SUM(CASE WHEN salesquery.Type = 'Invoice' THEN salesquery.[GP $] ELSE -salesquery.[GP $] END) / SUM(CASE WHEN salesquery.Type = 'Invoice' THEN salesquery.TotalExGST ELSE -salesquery.TotalExGST END) * 100 

    FROM 
      (SELECT 
        iid.DepartmentCode AS 'Department', 
        ci.InvoiceDate,
        ci.Type,
        ci.InvoiceCode,
        SUM(cid.ExtActualCost) AS 'Cost', 
        SUM(cid.ExtPrice) + MAX(ci.Freight) + MAX(ci.Other) AS 'TotalExGST', 
        (SUM(cid.ExtPrice) - SUM(cid.ExtActualCost)) AS 'GP $',
        (CASE WHEN SUM(cid.ExtPrice) = 0 THEN 0 ELSE ((SUM(cid.ExtPrice) - SUM(cid.ExtActualCost)) / SUM(cid.ExtPrice)) END) * 100 as 'GP %'



      FROM CustomerInvoice ci
      JOIN CustomerInvoiceDetail cid ON ci.InvoiceCode = cid.InvoiceCode
      JOIN InventoryItemDepartment iid ON cid.ItemCode = iid.ItemCode

      WHERE ci.IsVoided = 0
        AND ci.InvoiceDate BETWEEN @StartDate AND @FinishDate
      GROUP BY ci.invoicecode, iid.DepartmentCode, ci.Type, ci.InvoiceDate) salesquery