2012-12-04 49 views
1

我有一個SQL語句,我試圖爲所有JobTypes提取總和,例如,如果有5個名稱爲「Process」的作業,它們應該只在一行上顯示Sum GB和KB。我該如何做到這一點?謝謝。所有作業的SUM

,所以我將顯示: - jpavlov 10分鐘前
2012年12月1日,數據Extrat,總和(60),和(56) - jpavlov 8分鐘前
2012年12月1日,流程,和(60 ),和(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 

回答

1

刪除CAST (Jobs.DateCompleted AS VARCHAR(MAX))SELECT以及GROUP BY

假設:JobTypes.NameJobTypes.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場得到總結出來的,你必須將DateCompletedSelect刪除以及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 
+0

我編輯了查詢,刪除了演員,但仍然沒有運氣。此外,JobType.JobId被刪除,所以它可以被日期和作業名稱 – jpavlov

+0

如何獲取日期? – jpavlov

+0

您是否想在沒有時間部分的結果中獲得日期,以便結果是每個工作的具有GB和KB的按日期詳細信息? – shahkalpesh

相關問題