我不是SQL的專家。我有下面的查詢,我想要做的是將狀態作爲單獨的列移動並計算不同的actionid。針對列標題的數據透視表聲明
查詢:
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
請發佈樣本數據和預期結果。 –
因此,您想要計算與特定ActionTypeID關聯的所有不同ActionIDs的數量?你得到的是一輛由特定駕駛員操作的車輛,該車輛的行動次數爲整個設置狀態,我猜,索賠或檔案。所以你只需要ActionTypeID |完全不同的行動完成? – Dresden
我試圖做到這一點菲利克斯,努力尋找如何上傳文件,所以希望圖像就足夠了,它應該使用原始帖子中的鏈接工作。 –