首页 > 解决方案 > Sum sql问题 - 里面的两列是连接的

问题描述

请问,是否可以通过一个查询返回此结果(来自下表)?

CountId = 4,SumTime = 60,SumServices = 11(问题在于与服务连接的 Sumtime)

ID      Date        Time Services
6267    2018-07-10  10  1
6846    2018-08-21  5   4
7129    2018-09-11  5   4
7224    2018-09-18  5   2

标签: sqlsql-servertsql

解决方案


调用 Sum 时只需将列相乘

SELECT COUNT(*) AS CountId, SUM(time*services) AS SumTime, SUM(services) AS SumServices
FROM table

推荐阅读