我完全对sql查询的输出感到困惑。这是我的SQL:
SELECT
PROJECT_ID, ACTIVITY_RUB4, CALCULATION_VERSION, ORIGINAL_QUANTITY,
SUM(MySum) Orig_Qty
FROM
Test
GROUP BY
PROJECT_ID, ACTIVITY_RUB4, CALCULATION_VERSION, ORIGINAL_QUANTITY
此查询不求和并返回单行。我希望Orig_Qty
是340。
这里是SQL Fiddle
我期望以下输出:
答案 0 :(得分:1)
ORIGINAL_QUANTITY
在这两行之间有所不同,这就是为什么您没有得到340
的原因。使用这个:
SELECT PROJECT_ID, ACTIVITY_RUB4, CALCULATION_VERSION, SUM(MySum) Orig_Qty
from Test r
Group by PROJECT_ID, ACTIVITY_RUB4, CALCULATION_VERSION
答案 1 :(得分:0)
使用子查询
select sum(Orig_Qty) q from
(SELECT PROJECT_ID, ACTIVITY_RUB4, CALCULATION_VERSION, ORIGINAL_QUANTITY, SUM(MySum) Orig_Qty
from Test r
Group by PROJECT_ID, ACTIVITY_RUB4, CALCULATION_VERSION, ORIGINAL_QUANTITY
) a
或仅需使用数量即可使用
SELECT SUM(MySum) Orig_Qty
from Test r
如果需要所有列的总和,则使用窗口函数
SELECT distinct PROJECT_ID, ACTIVITY_RUB4, CALCULATION_VERSION,
SUM(MySum) over() Orig_Qty
from Test r
PROJECT_ID ACTIVITY_RUB4 CALCULATION_VERSION Orig_Qty
36002 12.C01-01-RP V001 340