0

我在 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))
4

2 回答 2

1

我认为您想要group by with rollup声明(在此处描述):

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将适用于您的查询,但很难确切地说出什么会产生您想要的东西。样本结果总是有助于有效沟通。

于 2013-07-12T13:59:05.087 回答
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]
于 2015-02-12T21:12:40.103 回答