2016-07-04 92 views
0

嗯,只有得到正確的標題花了我10分鐘,我不確定我是否覆蓋了我的問題。一些背景信息:我的表格包含來自多個服務器的備份結果。爲簡單起見某些行只是一個服務器:SQL Server:多列透視

hostname type_id result_id received 
---------------------------------------- 
SBS2011  5  1  2016-06-28 
SBS2011  5  1  2016-06-28 
SBS2011  5  1  2016-06-29 
SBS2011  5  1  2016-06-29 
SBS2011  5  1  2016-06-30 
SBS2011  6  1  2016-06-30 
SBS2011  5  2  2016-07-01 
SBS2011  6  2  2016-07-01 
SBS2011  6  2  2016-07-01 
SBS2011  5  1  2016-07-02 
SBS2011  6  1  2016-07-02 
SBS2011  5  1  2016-07-03 
SBS2011  6  1  2016-07-03 
SBS2011  5  1  2016-07-04 
SBS2011  6  1  2016-07-04 

使用PIVOT我可以得到備份量爲每個工作日的概述:

select * from 
(
    select [hostname], [type_id], datepart(w, received) as workday from [backups] 
) TEMP 
pivot (
    count([type_id]) 
    for workday in 
    ([1], [2], [3], [4], [5], [6], [7]) 
) as pvt; 

結果:

hostname 1 2 3 4 5 6 7 
-------------------------------------- 
SBS2011  2 2 2 2 2 3 2 

但是這個結果錯過了一些關鍵信息。作爲result_id等於「成功」和result_id等於「失敗」,我想有結果看起來像這樣:

hostname 1:1 1:2 2:1 2:2 3:1 3:2 4:1 4:2 5:1 5:2 6:1 6:2 7:1 7:2 
------------------------------------------------------------------- 
SBS2011  2 0 2 0 2 0 2 0 2 0 0 3 2 0 

,其中列名1:1是星期日簡寫:成功,1:2等於星期日:失敗。對於某些備份type_id,星期日可能會有1:3的列:retry。

當我環顧四周,我發現動態樞軸可能是解決這個難題的關鍵。其他人提出了PARTITION BY,但我還沒有發現如何。 DYNAMIC PIVOT似乎是最有前途的,但我不知道如何。請幫我創建這個 - 對我來說很複雜 - 查詢?

回答

0

如果你不想使用動態SQL擺弄你可以只保持它的靜態和改變被樞接在列。

喜歡的查詢:

select * 
from 
(
    select 
     [hostname], 
     [type_id], 
     concat(datepart(w, received),':',result_id) as workday 
    from [backups] 
) TEMP 
pivot (
    count([type_id]) 
    for workday in (
     -- maybe you don't want the :3 option for all days? Adjust as needed 
     [1:1],[1:2],[1:3], 
     [2:1],[2:2],[2:3], 
     [3:1],[3:2],[3:3], 
     [4:1],[4:2],[4:3], 
     [5:1],[5:2],[5:3], 
     [6:1],[6:2],[6:3], 
     [7:1],[7:2],[7:3] 
    ) 
) as pvt; 

可能會給類似於你想要什麼結果呢?

0

檢查一下,看看它是否解決了你的問題......非常確定我可以進一步簡化它,但需要知道type_id(5和6)的解碼是什麼。會有更多type_id的?

也result_id = 1 =成功和2 =失敗的權利?

問題當備份在特定日期沒有運行時會發生什麼:)?你如何處理這種情況,因爲那一天沒有記錄。 :)

/* Create table and populate with sample data. 
create table Backups (hostname varchar(10), type_id int , result_id int , received datetime) 

insert into Backups (hostname , type_id ,result_id , received) values 
('SBS2011',  5 ,  1  ,'2016-06-28'), 
('SBS2011',  5 ,  1  ,'2016-06-28'), 
('SBS2011',  5 ,  1  ,'2016-06-29'), 
('SBS2011',  5 ,  1  ,'2016-06-29'), 
('SBS2011',  5 ,  1  ,'2016-06-30'), 
('SBS2011',  6 ,  1  ,'2016-06-30'), 
('SBS2011',  5 ,  2  ,'2016-07-01'), 
('SBS2011',  6 ,  2  ,'2016-07-01'), 
('SBS2011',  6 ,  2  ,'2016-07-01'), 
('SBS2011',  5 ,  1  ,'2016-07-02'), 
('SBS2011',  6 ,  1  ,'2016-07-02'), 
('SBS2011',  5 ,  1  ,'2016-07-03'), 
('SBS2011',  6 ,  1  ,'2016-07-03'), 
('SBS2011',  5 ,  1  ,'2016-07-04'), 
('SBS2011',  6 ,  1  ,'2016-07-04') 

select * , DatePart(w, received) from dbo.Backups b 

*/ 

查詢:

SELECT -- S.*, F.* 
     S.[hostname], 
     S.[1] as [1:1], 
     F.[1] as [1:2], 
     S.[2] as [2:1], 
     F.[2] as [2:2], 
     S.[3] as [3:1], 
     F.[3] as [3:2], 
     S.[4] as [4:1], 
     F.[4] as [4:2], 
     S.[5] as [5:1], 
     F.[5] as [5:2], 
     S.[6] as [6:1], 
     F.[6] as [6:2], 
     S.[7] as [7:1], 
     F.[7] as [7:2] 

FROM 
(
    select * from 
    (
     select [hostname], [type_id], datepart(w, received) as workday , result_id as res_type from [backups] where result_id = 1 

    ) TEMP 
    pivot 
    (
     count([type_id]) 
     for workday in 
     ([1], [2], [3], [4], [5], [6], [7]) 
    ) as pvt 

) S 
LEFT OUTER JOIN 
(
    select * from 
    (
     select [hostname], [type_id], datepart(w, received) as workday , result_id as res_type from [backups] where result_id = 2 

    ) TEMP 
    pivot 
    (
     count([type_id]) 
     for workday in 
     ([1], [2], [3], [4], [5], [6], [7]) 
    ) as pvt 

) F ON S.res_type = F.res_type - 1 
WHERE F.hostname IS NOT NULL 
+0

是的,根據主機,type_id的1到4也存在。還有更多的這兩個result_id,但這不應該太難以擴展。無法運行的備份確實是另一個問題和另一個查詢。現在我會堅持不同type_id的總和。如果總數少於其他日子,則出現問題。 – mokum

+0

Type_id代表源軟件和備份類型。例如Type_id = 1等於Acronis VMprotect,type_id = 2等於Acronis VMprotect雙目的地,type_id = 3等於Symantec BackupExec等 – mokum