1

我有一个 SQL 语句,我试图提取所有 JobTypes 的总和,例如,如果有 5 个名为“Process”的作业,它们应该只显示在一行上,其中包含 GB 和 KB 的总和。我该如何做到这一点?谢谢。

所以我会显示: - jpavlov 10 分钟前
2012 年 12 月 1 日,Data Extrat,sum(60),sum(56) - jpavlov 8 分钟前
,2012 年 12 月 1 日,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
4

1 回答 1

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,您必须删除DateCompletedfromSelectGroup By.

编辑2:

看看这个问题并修改你的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
于 2012-12-04T14:44:09.947 回答