我有一个SQL语句,我试图为所有JobTypes提取总和,例如,如果有5个名为“Process”的作业,它们应该只显示一行,其中包含GB和KB的总和。我该如何做到这一点?感谢。
所以我会表示: - jpavlov 10分钟前
DEC 1,2012,Data Extrat,sum(60),sum(56) - jpavlov 8分钟前
DEC 1,2012,Process,sum(60),sum(56)
SELECT
Jobs.DateCompleted AS 'DateCompleted',
JobTypes.Name AS 'JobName',
SUM(Metrics.GB) AS GB,
SUM(Metrics.KB) AS KB
FROM Metrics
INNER JOIN
Jobs ON Jobs.JobId = Metrics.JobId
INNER JOIN
Projects ON Projects.ProjectId = Jobs.ProjectId
INNER JOIN
JobTypes ON JobTypes.JobTypeId = Jobs.JobTypeId
WHERE Jobs.DateCompleted
Between '12/01/2012' AND '12/03/2012'
GROUP BY
Jobs.DateCompleted,
JobTypes.Name
2012-12-01 04:28:15.477 Data Extract 0.200 210903.738
2012-12-01 04:40:07.913 Data Extract 0.781 819388.602
2012-12-01 04:48:45.493 Data Extract 5.278 5538731.844
2012-12-01 04:54:55.483 Data Extract 6.927 7265870.682
2012-12-01 14:11:38.357 Export 13.189 13828648.369
2012-12-01 16:57:54.840 Process 2.054 2156185.331
答案 0 :(得分:1)
从CAST (Jobs.DateCompleted AS VARCHAR(MAX))
以及SELECT
删除GROUP BY
。
假设:JobTypes.Name
和JobTypes.JobTypeId
有1对1的映射。
修改强>
SELECT
JobTypes.Name AS 'JobName',
SUM(Metrics.GB) AS GB,
SUM(Metrics.KB) AS KB
FROM Metrics
INNER JOIN
Jobs ON Jobs.JobId = Metrics.JobId
INNER JOIN
Projects ON Projects.ProjectId = Jobs.ProjectId
INNER JOIN
JobTypes ON JobTypes.JobTypeId = Jobs.JobTypeId
WHERE Jobs.DateCompleted
Between '12/01/2012' AND '12/03/2012'
GROUP BY
JobTypes.Name
要获得Name
字段总结的结果,您必须从DateCompleted
以及Select
中删除Group By
。
EDIT2:
看看this question并修改你的sql。
SELECT
convert(varchar(10), Jobs.DateCompleted, 120) AS DateCompleted
JobTypes.Name AS 'JobName',
SUM(Metrics.GB) AS GB,
.....
.....
GROUP BY
convert(varchar(10), Jobs.DateCompleted, 120),
JobTypes.Name