SQL查询,其中SUM包含where子句

时间:2015-08-06 20:54:32

标签: sql postgresql group-by where where-clause

我有一个如下所示的数据库结构:

|  Date  | Amount | Source |
|02/02/02| 100    |  "A"   |
|02/02/02| 300    |  "B"   |
|05/05/02| 200    |  "A"   |
|05/05/02| 700    |  "B"   |
|07/05/02| 200    |  "C"   |
|07/05/02| 700    |  "C"   |

我想做的是获取一个SQL查询,它给出了如下所示的输出:

|  Date  | Amount | Source |
|02/02/02| 100    |  "A"   |
|02/02/02| 300    |  "B"   |
|05/05/02| 200    |  "A"   |
|05/05/02| 700    |  "B"   |
|07/05/02| 900    |  "C"   |

这主要是按日期和来源分组,然后对金额求和。我该怎么做?

1 个答案:

答案 0 :(得分:2)

here你可以看到你基本上需要:

SELECT Date, SUM(Amount) AS Total_Amount, Source FROM my_table GROUP BY Date, Source