2011-06-28 178 views
2

我想知道如何查詢一個數據庫,我可以返回兩個總和,第一個是總檢查次數,然後是每個督察總數,即喬總共做了4個檢查,但仍顯示不同層次的細分。我是sql新手,所以包含我的腳本到目前爲止,但讓我知道如果你需要更多的信息。SQL Server 2005,計數總計

SELECT  InspectorCode AS Inspector, InspectionProcedureName AS [Procedure], COUNT(*) AS  Total 
FROM   UnitData_Vehicle 
WHERE  (DATEDIFF(day, InspectionDateTime, GETDATE()) = 1) 
AND InspectionProcedureName LIKE '%Inspection%' 
GROUP BY InspectionProcedureName, InspectorCode 

我想看到

Inspector Procedure Total InspTotal 
joe   1 - Inspection 1  4 
joe   2 - Inspection 3 
kit   3 - Inspection 14  14 
hugh  1 - Inspection 17  32 
hugh  6 - Inspection 15 
mike  4 - Inspection 18  18 

回答

1

編輯:基於@pratik GARG的回答,看起來像這樣的作品在SQL Server:

select InspectorCode as Inspector 
,  InspectionProcedureName as Procedure 
,  count(*) as Total 
,  count(*) over (partition by InspectorCode) as InspTotal 
from UnitData_Vehicle as uv1 
where (datediff(day, InspectionDateTime, getdate()) = 1) 
     and InspectionProcedureName like '%Inspection%' 
group by 
     InspectorCode 
,  Procedure 

count(*) over (partition by InspectorCode)顯然榮譽的where條款,但確實包括多個組。有意思:)

+0

謝謝你們,這一個工程但它是計算有多少每個檢查員完成的程序,即喬完成了兩種檢查。如果可能的話,我希望看到每個檢查員完成的檢查總數,即喬已經完成了4.但是,感謝您迄今的努力! – Tyrone2011

+0

@ Tyrone2011:InspTotal列應顯示經理的總體檢查情況,請參閱[odata中的此示例](http://data.stackexchange.com/stackoverflow/q/104184/) – Andomar

2

我不知道有關SQL服務器,但在Oracle下面的查詢您want-

SELECT zz.Inspector as Inspector, 
     zz.Procedure as Procedure, 
     zz.total as total, 
     case 
     when zz.c_1 =1 then 
     zz.InspTotal 
     else 
     null 
     end as InspTotal   
     FROM (SELECT InspectorCode AS Inspector, 
     InspectionProcedureName AS Procedure, 
     COUNT(*) AS  Total , 
     count(*) over(partition by InspectorCode) InspTotal, 
     row_number() over(partition by InspectorCode order by InspectionProcedureName) c_1 
FROM UnitData_Vehicle 
WHERE (DATEDIFF(day, InspectionDateTime, GETDATE()) = 1) 
     AND InspectionProcedureName LIKE '%Inspection%' 
GROUP BY InspectionProcedureName, InspectorCode)zz; 

請檢查您的where條件會給結果.. 我被檢查和最後所需的列工作只有.. 在你的問題你還沒有告訴任何條件...

,如果你想澄清在此查詢任何評論,請點擊這裏..

還有一件事 - 我以爲你想InspTotal列值只有一次InspectorCode代碼.. 我是嗎?

+0

+1有趣,我以爲你不能'使用'over'去組 – Andomar

+0

感謝這個,但它不適用於SQL,但是你是對的我只想看到每個總值一個值,或者如果沒有返回值,則爲null。謝謝 ! – Tyrone2011

+0

@Andomar:我很抱歉,但我沒有正確理解你。無法使用!!!! –

0

下面的示例代碼爲我的數據庫

SELECT fiscalmonth,item,fiscalyear 
     ,SUM(valuesale) 
     , 
(SELECT SUM(valueSale) 
FROM inbalance b 
WHERE b.fiscalmonth <= a.fiscalmonth 
and a.fiscalyear =b.fiscalyear 
and a.item = b.item 
) AS RunningTotal 
FROM inbalance a 
WHERE a.item='1101' 
AND a.fiscalyear=2011 
GROUP BY fiscalmonth,item,fiscalyear 

運行良好因此,通過「翻譯」我的代碼來查詢

SELECT  InspectorCode AS Inspector, InspectionProcedureName AS [Procedure], COUNT(*) AS  Total 
, (
SELECT COUNT(*) FROM UnitData_Vehicle b 
WHERE a.InspectorCode = b.InspectorCode 
AND b.InspectionProcedureName <= b.InspectionProcedureName 
GROUP BY InspectionProcedureName,InspectorCode 
) As InspTotal 
FROM   UnitData_Vehicle a 
WHERE  (DATEDIFF(day, InspectionDateTime, GETDATE()) = 1) 
AND InspectionProcedureName LIKE '%Inspection%' 
GROUP BY InspectionProcedureName, InspectorCode 
+0

消息207,級別16,狀態1,行4 列名稱'inspector'無效。 Msg 4104,Level 16,State 1,Line 4 無法綁定多部分標識符「b.inspector」。 消息4104,級別16,狀態1,行4 無法綁定多部分標識符「b.InspectionProcedureName」。 消息4104,級別16,狀態1,行4 無法綁定多部分標識符「b.InspectionProcedureName」。 消息207,級別16,狀態1,行6 列名稱'Inspector'無效。 – Tyrone2011

+0

感謝您的腳本,看起來不錯,但我收到了一些錯誤?有任何想法嗎?謝謝 – Tyrone2011

+0

現在應該可以。如果不是,請給我錯誤輸出。 – niktrs