查询总收入

时间:2017-09-13 12:23:14

标签: mysql sql

我有下表'收集'。它以现金和卡的形式存储来自2家商店的销售:

Date        |  Shop | Cash | Card |
-----------------------------------
2017-01-01  |   A   |  10  |  5   |
2017-01-01  |   B   |   8  |  2   |
2017-01-02  |   A   |   9  |  6   |
2017-01-02  |   B   |   8  |  5   |
2017-01-03  |   A   |   9  |  7   |
2017-01-03  |   B   |  10  |  1   |

我想运行SQL查询并从两家商店获得每日总收入作为以下输出

Day    |   Earnings
-------------------
1      |   25
2      |   28
3      |   27

2 个答案:

答案 0 :(得分:2)

使用简单的GROUP BY就可以轻松完成:

   SELECT Date
         ,SUM(Cash + Card) AS Earnings
     FROM yourtable
 GROUP BY Date

答案 1 :(得分:0)

请检查如下:

SELECT row_number() over (order by date) AS Day
         ,SUM(Cash + Card) AS Earnings
     FROM #TEMP
 GROUP BY Date