从sql中检索总计金额

时间:2011-03-06 17:27:51

标签: sql sqlite

以下查询有什么问题:

SELECT wbCode||yr, CAST(coalesce(Cons,0) AS FLOAT), wbCode
FROM Commodities
Where CCode in (2611,2513,2961) Group by wbCode||yr

我希望上面的查询返回给定年份(USA1990)中每个wbCode的总和,例如;但上面的代码并没有总结;它检索的数据与:

相同
SELECT wbCode||yr, CAST(coalesce(Cons,0) AS FLOAT), wbCode 
FROM Commodities
Where CCode in (2611) Group by wbCode||yr

SELECT wbCode||yr, CAST(coalesce(Cons,0) AS FLOAT), wbCode 
FROM Commodities Where CCode in (2513,2961)
Group by wbCode||yr

我怎样才能得到它?

非常感谢你的帮助。

3 个答案:

答案 0 :(得分:3)

为什么||?只需将它们分开分组

SELECT wbCode, yr, SUM(CAST(coalesce(Cons,0) AS FLOAT)), wbCode
FROM Commodities
Where CCode in (2611,2513,2961)
Group by wbCode, yr

或者

SELECT wbCode||yr, SUM(CAST(coalesce(Cons,0) AS FLOAT)), wbCode
FROM Commodities
Where CCode in (2611,2513,2961)
Group by wbCode, yr

答案 1 :(得分:1)

为什么不使用SUM函数来汇总结果?

答案 2 :(得分:0)

这个怎么样:

  SELECT wbCode
  ,      yr
  ,      SUM(CAST(coalesce(Cons,0) AS FLOAT))
    FROM Commodities
   WHERE CCode in (2611,2513,2961) 
GROUP BY wbCode
,        yr;