繁体   English   中英

将总行添加到SQL查询的底部

[英]Add total row to bottom of SQL Query

我有一条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 ║
╚════════════╩══════════════╩══════════════╩══════════════╩════════════════╩═══════════╝

我希望表格与表格行“总计”一起输出,该行将上面的行相加,并对最后一列取平均值。

╔════════════╦══════════════╦══════════════╦══════════════╦════════════════╦════════════╗
║ 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) ║
╚════════════╩══════════════╩══════════════╩══════════════╩════════════════╩════════════╝

我倾向于使用GROUPING SETS 对于最后一个GROUP BY

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

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

最后使用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显示来求和原始结果(不包括在查询中)总和可能是一种更标准的方法。

您可以使用UNION并通过salesquery.group重新组合组

 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM