我在SAP B1中有一个SQL查询,列出了本周的销售额。我还有一个查询,可以得到相同销售额的总和。如何组合查询,以便结果是所有销售行,总和显示为这些行下面的行?
SQL列表查询 -
SELECT T0.[DocNum], T0.[DocDate],
T1.[CardName], T1.[CardFName], T0.[DocTotalSy] AS Total
FROM ORDR T0
INNER JOIN OCRD T1
ON T0.CardCode = T1.CardCode
WHERE T0.[DocDate] > (SELECT CONVERT(varchar(10), DATEADD(day, -6, GETDATE()), 101))
SQL Sum查询 -
SELECT SUM (T0.[DocTotalSy]) AS Total
FROM ORDR T0
INNER JOIN OCRD T1
ON T0.CardCode = T1.CardCode
WHERE T0.[DocDate] > (SELECT CONVERT(varchar(10), DATEADD(day, -6, GETDATE()), 101))
答案 0 :(得分:1)
我想你想要group by with rollup
语句(描述为here):
SELECT T0.[DocNum], T0.[DocDate], T1.[CardName], T1.[CardFName], sum(T0.[DocTotalSy]) AS Total
FROM ORDR T0 INNER JOIN
OCRD T1
ON T0.CardCode = T1.CardCode
WHERE T0.[DocDate] > (SELECT CONVERT(varchar(10), DATEADD(day, -6, GETDATE()), 101))
group by T0.[DocNum], T0.[DocDate], T1.[CardName], T1.[CardFName] with rollup
但是,这可能不是您想要的。我可以看到rollup
或grouping sets
适用于您的查询,但很难准确说出会产生您想要的内容。样本结果总是有助于有效沟通。
答案 1 :(得分:0)
您可以在现有的第一个查询中添加总和,然后使用group by:
SELECT T0.[DocNum], T0.[DocDate], T1.[CardName], T1.[CardFName], T0.[DocTotalSy] AS Total, sum(T0.[DocTotalSy]) AS SumTotal
FROM ORDR T0 INNER JOIN
OCRD T1
ON T0.CardCode = T1.CardCode
WHERE T0.[DocDate] > (SELECT CONVERT(varchar(10), DATEADD(day, -6, GETDATE()), 101))
group by T0.[DocNum], T0.[DocDate], T1.[CardName], T1.[CardFName]