0

我有一个运行良好的以下选择查询

SELECT
  division, 
  division_name, 
  object_account, 
  account_description, 
  Sum(value) AS PreferedSpend 
FROM jde_dw.blanket_rpt 
WHERE division = 02 
AND vendor_type = 'PV' 
AND month = 07 
AND year = 2013 
GROUP BY division, division_name, object_account, account_description 
ORDER BY division

现在我想添加一个显示总支出的列,它将与上面的查询相同,但在 where 语句中没有 vendor_type 条件。

所以最后的查询将显示 Division, Division_name, Object_account, Account_Description, Value( as preferred) 和 value as( 包括所有内容的总计)

我怎样才能做到这一点?

4

1 回答 1

1

将条件移到内部,SUM()以便只有匹配的行对总和有贡献:

SELECT
  division, 
  division_name, 
  object_account, 
  account_description, 
  SUM(CASE WHEN vendor_type = 'PV' THEN value ELSE 0 END) as PreferedSpend,
  SUM(value) AS TotalSpend
FROM jde_dw.blanket_rpt 
WHERE division = 02 
AND month = 07 
AND year = 2013 
GROUP BY division, division_name, object_account, account_description 
ORDER BY division
于 2013-09-30T15:21:08.443 回答