2012-10-23 128 views
1

我的查詢返回的數據集,看起來像這樣:如何從樞軸聚合值?

+-----------+--------+-----------+-------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+---------+------------+--------+---------+------------+---------+---------+------------+--------+ 
| CLIENT_ID | count1 | TestFreq1 | stdv1 | count2 | TestFreq2 | stdv2 | count3 | TestFreq3 | stdv3 | count4 | TestFreq4 | stdv4 | count5 | TestFreq5 | stdv5 | count6 | TestFreq6 | stdv6 | count7 | TestFreq7 | stdv7 | count8 | TestFreq8 | stdv8 | count9 | TestFreq9 | stdv9 | count10 | TestFreq10 | stdv10 | count11 | TestFreq11 | stdv11 | count12 | TestFreq12 | stdv12 | 
+-----------+--------+-----------+-------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+---------+------------+--------+---------+------------+---------+---------+------------+--------+ 
| 210893 | 136 |   0 |  0 |  81 |  41 | 79.2685 |  19 |  63 | 58.321 |  24 |  21 | 20.4896 |  5 |  25 | 8.228 |  6 |  24 | 24.0638 |  4 |  25 | 24.6103 | 2  | 25  | 2.12132 |  2 |  23 | 21.9203 | 1  | 33   | NULL |  2 |   29 | 7.77817 | 1  | 38   | NULL | 
| 123321 |  50 |   0 |  0 |  5 |  26 | 7.87401 |  14 |  45 | 51.8002 |  3 |  25 | 14.7422 |  2 |  22 | 17.6777 |  4 |  36 | 21.4942 |  3 |  36 | 22.2711 | NULL | NULL  | NULL |  4 |  35 | 9.30949 | NULL | NULL  | NULL |  1 |   31 | NULL | NULL | NULL  | NULL | 
| 454322 | 232 |   0 |  0 | 173 |  10 | 33.8487 |  36 |  36 | 36.6602 |  32 |  15 | 17.485 |  10 |  38 | 22.4809 |  13 |  23 | 20.0477 |  7 |  18 | 11.4143 | 3  | 32  | 24.5425 |  6 |  25 | 16.8602 | 3  | 28   | 21.166 |  2 |   25 | 4.94975 | 1  | 34   | NULL | 
+-----------+--------+-----------+-------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+---------+------------+--------+---------+------------+---------+---------+------------+--------+ 

,而不是強迫的數據出去count13, stdv13, testfreq13, ..14..14..14, 15.15.15我怎麼能在同一領域聚集所有的值12及以上?

這裏是我的查詢,非常感謝你的指點:

;WITH counted AS (
    SELECT 
    client_id, 
    COUNT(*) AS TimesTested, 
    (datediff(day,MIN(received_date),max(received_date))) 
    /COUNT(*) as TestFreq 
    FROM f_accession_daily 
    GROUP BY 
    client_id, 
    patient_id 
), 
counted2 as (
    SELECT 
    client_id, 
    TimesTested, 
    CAST(COUNT(*) AS varchar(30)) AS count, 
    CAST(AVG(testfreq) as varchar(30)) as TestFreq, 
    CAST(STDEV(TestFreq) as varchar(30)) Stdv 
    FROM counted 
    GROUP BY 
    client_id, 
    TimesTested 
    ) 
    , 
unpivoted AS (
    SELECT 
    client_id, 
    ColumnName + CAST(TimesTested AS varchar(10)) AS ColumnName, 
    ColumnValue 
    FROM counted2 
    UNPIVOT (
    ColumnValue FOR ColumnName IN (count, TestFreq,stdv) 
) u 
), 
pivoted AS (
    SELECT 
    client_id clientid, 
    count1, TestFreq1,stdv1, 
    count2, TestFreq2,stdv2, 
    count3, TestFreq3,stdv3, 
    count4, TestFreq4,stdv4, 
    count5, TestFreq5,stdv5, 
    count6, TestFreq6,stdv6, 
    count7, TestFreq7,stdv7, 
    count8, TestFreq8,stdv8, 
    count9, TestFreq9,stdv9, 
    count10, TestFreq10,stdv10, 
    count11, TestFreq11,stdv11, 
    count12, TestFreq12,stdv12 
    FROM unpivoted 
    PIVOT (
    MAX(ColumnValue) FOR ColumnName IN (
     count1,TestFreq1,stdv1, 
     count2,TestFreq2,stdv2, 
     count3,TestFreq3,stdv3, 
     count4,TestFreq4,stdv4, 
     count5,TestFreq5,stdv5, 
     count6,TestFreq6,stdv6, 
     count7,TestFreq7,stdv7, 
    count8, TestFreq8, stdv8, 
    count9, TestFreq9, stdv9, 
    count10, TestFreq10,stdv10, 
    count11, TestFreq11,stdv11, 
    count12, TestFreq12,stdv12 
    ) 
) p 
) 
select * from pivoted 

只是爲了澄清我想回到同一個確切的結果,它只是在過去的專欄中,我想要聚合的所有值那落入12+ bucket。所有的領域都將是除了最後三個相同的,這將是:

+----------+-------------+---------+ 
| count12+ | TestFreq12+ | stdv12+ | 
+----------+-------------+---------+ 
| 353  | 32423  | NULL | 
| NULL  | NULL  | NULL | 
| 342  | 25324  | NULL | 
+----------+-------------+---------+ 

請注意相較於其他,因爲12+已經彙總上面的更大的數字。

非常感謝你的指導!

+3

難道你只需要改變你的CTE'counted2'所以列'TimesTested'是這樣的:'CASE WHEN TimesTested> = 12 THEN ELSE 12 TimesTested END'? – Lamak

+0

謝謝!我是否需要對主鍵進行更改? –

+3

@АртёмЦарионов-你爲什麼不嘗試;-) – Lamak

回答

3

看起來最快的方法就是改變你的CTE,所以TimesTested這一列將你的邏輯考慮在內。因此,它應該是:

counted2 as (
    SELECT 
    client_id, 
    CASE WHEN TimesTested >= 12 THEN 12 ELSE TimesTested END TimesTested, 
    CAST(COUNT(*) AS varchar(30)) AS count, 
    CAST(AVG(testfreq) as varchar(30)) as TestFreq, 
    CAST(STDEV(TestFreq) as varchar(30)) Stdv 
    FROM counted 
    GROUP BY 
    client_id, 
    CASE WHEN TimesTested >= 12 THEN 12 ELSE TimesTested END 
    ) 
+0

謝謝!你不同意@andriy? –

+0

@АртёмЦарионов - 並非如此。由於這些是CTE,而不是物理表格,我不認爲改變第一個CTE會帶來好處。我懶惰的人喜歡改變第二個CTE,因爲我不必使用'CASE當COUNT(*)..',我可以直接使用colum的名字。 (請注意,如果這些是物理表或臨時表,這可能會有所不同) – Lamak

+0

@Lamak:有趣的是,我的推理可能與您的理由非常相似,除了我認爲將COUNT(*)'寫爲TimesTested'比較容易,因爲前者較短。 :)但是,嚴肅地說,在這種情況下我看不出什麼區別,你的選擇絕不比我的更糟。 –