2016-05-04 42 views
1

我不是SQL的專家。我有下面的查詢,我想要做的是將狀態作爲單獨的列移動並計算不同的actionid。針對列標題的數據透視表聲明

Action ID Sample image

查詢:

SELECT 
CONVERT (Date,[ActionTime],103) AS DATE, 
COUNT(DISTINCT ActionID) AS Actions, 
VehicleID AS VehicleID, 
DriverID AS DriverID, 
    CASE 
    WHEN ActionTypeID = 7 THEN 'Not Suitable' 
    WHEN ActionTypeID = 8 THEN 'Job Acknowledged' 
    WHEN ActionTypeID = 9 THEN 'Job Accepted' 
    WHEN ActionTypeID = 10 THEN 'Job Rejected' 
    WHEN ActionTypeID = 12 THEN 'Job Recall' 
    WHEN ActionTypeID = 66 THEN 'Child Fleet Logon' 
    WHEN ActionTypeID = 67 THEN 'Child Fleet Logoff' 
    ELSE '' END AS Status 

FROM [TaxiHistory].[dbo].[Actions] 
WHERE actiontime between '2016-02-01 00:00:00' AND '2016-02-02 23:59:59' 
    AND ActiontypeID IN (7,8,9,10,12,66,67) 

    GROUP BY CONVERT (Date,[ActionTime],103), 
CASE 
    WHEN ActionTypeID = 7 THEN 'Not Suitable' 
    WHEN ActionTypeID = 8 THEN 'Job Acknowledged' 
    WHEN ActionTypeID = 9 THEN 'Job Accepted' 
    WHEN ActionTypeID = 10 THEN 'Job Rejected' 
    WHEN ActionTypeID = 12 THEN 'Job Recall' 
    WHEN ActionTypeID = 66 THEN 'Child Fleet Logon' 
    WHEN ActionTypeID = 67 THEN 'Child Fleet Logoff' 
    ELSE '' END, 
VehicleID, 
DriverID 
+0

請發佈樣本數據和預期結果。 –

+0

因此,您想要計算與特定ActionTypeID關聯的所有不同ActionIDs的數量?你得到的是一輛由特定駕駛員操作的車輛,該車輛的行動次數爲整個設置狀態,我猜,索賠或檔案。所以你只需要ActionTypeID |完全不同的行動完成? – Dresden

+0

我試圖做到這一點菲利克斯,努力尋找如何上傳文件,所以希望圖像就足夠了,它應該使用原始帖子中的鏈接工作。 –

回答

0

我認爲這是你想要做的。

SELECT CONVERT (DATE,[ActionTime],103) AS [Date], 
     VehicleID AS VehicleID, 
     DriverID AS DriverID, 
     COUNT(CASE WHEN ActionTypeID = 7 THEN 1 END) AS [Not Suitable], 
     COUNT(CASE WHEN ActionTypeID = 8 THEN 1 END) AS [Job Acknowledged], 
     COUNT(CASE WHEN ActionTypeID = 9 THEN 1 END) AS [Job Accepted], 
     COUNT(CASE WHEN ActionTypeID = 10 THEN 1 END) AS [Job Rejected], 
     COUNT(CASE WHEN ActionTypeID = 12 THEN 1 END) AS [Job Recall], 
     COUNT(CASE WHEN ActionTypeID = 66 THEN 1 END) AS [Child Fleet Logon], 
     COUNT(CASE WHEN ActionTypeID = 67 THEN 1 END) AS [Child Fleet Logoff] 
FROM [TaxiHistory].[dbo].[Actions] 
WHERE actiontime BETWEEN '2016-02-01 00:00:00' 
        AND  '2016-02-02 23:59:59' 
     AND ActiontypeID IN (7,8,9,10,12,66,67) 
GROUP BY CONVERT (DATE,[ActionTime],103), 
     VehicleID, 
     DriverID 
+0

謝謝JamieD77 –

0
select 
    CONVERT (Date,[ActionTime],103) AS DATE, 
    COUNT(DISTINCT ActionID) AS Actions, 
    VehicleID AS VehicleID, 
    DriverID AS DriverID, 
    [7] 'Not Suitable', 
    [8] 'Job Acknowleged', 
    [9] 'Job Accepted', 
    [10] 'Job Rejected', 
    [12] 'Job Recall', 
    [66] 'Child Freet Logon', 
    [67] 'Child Fleet Logoff' 
(
    select ActionTime, VehicleId, DriverId, ActionTypeId, count(*) as TotalType 
    FROM [TaxiHistory].[dbo].[Actions] 
    WHERE actiontime between '2016-02-01 00:00:00' AND '2016-02-02 23:59:59' 
     AND ActiontypeID IN (7,8,9,10,12,66,67) 
    group by ActionTime, VehicleId, DriverId, ActionTypeId 
pivot(sum(TotalType) for ActionTypeId in ([7],[8],[9],[10],[12], [66], [67])) 
) p 

內部查詢是基本一,它獲取對於每個ActionTime,車輛,Deiver和操作類型操作的數量。最後的PIVOT基本上對每個列出的ActionTypeId表示,總結TotalType並將結果集中的一列放入ActionTypeId值的名稱。這個結果集(p在查詢中)就是外部選擇的輸入,它接受列並向其輸入各種名稱。