[英]SQL Grand Total row from Multi table Select Query
I have been able to use the below SQL query to give me 4 rows of data我已经能够使用下面的 SQL 查询给我 4 行数据
SELECT 'Sales Order ' as Type, Format(Sum(T1.C_NetAmountLessDiscount),
'#.00') As NetAmount, Format(Sum(T1.C_MarginAmount), '#.00') As
MarginAmount, Format(Sum(T1.C_MarginAmount)/Sum(T1.C_NetAmountLessDiscount),
'#.00%') As Margin
From T_SalesOrder as T1
Where cast(T1.C_Date as Date) = cast(getdate() as Date) AND T1.C_OrderType
!= 'BK' AND T1.C_OrderType != 'INV'
Union
SELECT 'Despatch Notes ' AS Type, Format(Sum(T1.C_NetAmountLessDiscount),
'#.00') As NetAmount, Format(Sum(T1.C_MarginAmount), '#.00') As
MarginAmount, Format(Sum(T1.C_MarginAmount)/Sum(T1.C_NetAmountLessDiscount),
'#.00%') As Margin
From T_SalesDeliveryNote as T1
Where cast(T1.C_Date as Date) = cast(getdate() as Date) AND T1.C_Order is
null AND T1.C_BillingStatus = '0'
Union
SELECT 'Invoices ' AS Type, Format(Sum(T1.C_NetAmountLessDiscount), '#.00')
As NetAmount, Format(Sum(T1.C_MarginAmount), '#.00') As MarginAmount,
Format(Sum(T1.C_MarginAmount)/Sum(T1.C_NetAmountLessDiscount), '#.00%') As
Margin
From T_SalesInvoice as T1
Where cast(T1.C_Date as Date) = cast(getdate() as Date) And
T1.C_DeliveryNote is null And T1.C_SourceOrder is null and T1.C_InvoiceType
= '0'
Union
SELECT 'Credit Notes ' AS Type, Format(Sum(T1.C_NetAmountLessDiscount), '-
#.00') As NetAmount, Format(Sum(T1.C_MarginAmount), '-#.00') As
MarginAmount, Format(Sum(T1.C_MarginAmount)/Sum(T1.C_NetAmountLessDiscount),
'#.00%') As Margin
From T_SalesCreditNote as T1
Where cast(T1.C_Date as Date) = cast(getdate() as Date)
This gives me a breakdown of the orders as I need but I also want to have a Grand Total row that sums each column.这使我可以根据需要对订单进行细分,但我还希望有一个总计行来对每一列求和。
If I insert the above sql query into the below如果我将上面的 sql 查询插入到下面
Select 'Grand Total', Sum(CAST(NetAmount AS float)), Sum(CAST(MarginAmount
AS float)),null
From
(
-----Above SQL Query in Here
)tbl
I get one single line with the correct totals but no breakdown rows.我得到一个包含正确总数但没有细分行的单行。
How can I do this so it displays the four rows of each type and a grand total row at the bottom.我该怎么做才能显示每种类型的四行和底部的总计行。
Use common table expressions (CTE).使用公用表表达式(CTE)。
WITH t AS(
-- Your query goes here
-- SELECT 1 AS A, 'Sales Order' ...
-- UNION SELECT 2 AS A, 'Despatch Notes' ...
-- UNION SELECT 3 AS A, 'Invoices' ...
-- UNION SELECT 4 AS A, 'Credit Notes' ...
)
SELECT
A,
Type,
NetAmount,
MarginAmount,
Margin
FROM
t
UNION SELECT
5 AS A,
'Grand Total' AS Type,
FORMAT(SUM(CAST(NetAmount AS FLOAT)), '#.00') As NetAmount,
FORMAT(SUM(CAST(MarginAmount AS FLOAT)), '#.00') As MarginAmount,
NULL
FROM
t
ORDER BY
A
Note: I added formatting the total values to ensure the total values of NetAmount and MarginAmount to share the same data type as the breakdown values.注意:我添加了总值的格式,以确保 NetAmount 和 MarginAmount 的总值与细分值共享相同的数据类型。 Consider to remove formatting the values from your query and add it to the presentation layer of your application.
考虑从查询中删除值的格式并将其添加到应用程序的表示层。
Grouping with a ROLLUP might be just the trick for this.使用ROLLUP进行分组可能只是解决此问题的诀窍。
Simplified Example Snippet:简化示例片段:
DECLARE @Table1 TABLE (
ID INT IDENTITY(1,1) PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
NetAmount DECIMAL(10,2) NOT NULL DEFAULT 0
);
DECLARE @Table2 TABLE (
ID INT IDENTITY(1,1) PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
NetAmount DECIMAL(10,2) NOT NULL DEFAULT 0
);
INSERT INTO @Table1 (Name, NetAmount)
VALUES ('A', 4.1), ('A', 6.1);
INSERT INTO @Table2 (Name, NetAmount)
VALUES ('B', 9.1), ('B', 11.1);
WITH CTE_AMOUNTS AS
(
SELECT 'T1' as [Type],
SUM(NetAmount) AS [NetAmount]
FROM @Table1
UNION ALL
SELECT 'T2', SUM(NetAmount)
FROM @Table2
)
SELECT
COALESCE([Type], 'Grand Total') AS [Type],
SUM([NetAmount]) AS [NetAmount]
FROM CTE_AMOUNTS
GROUP BY Type WITH ROLLUP
ORDER BY GROUPING_ID(Type), Type;
Returns:回报:
Type NetAmount
T1 10,20
T2 20,20
Grand Total 30,40
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.