2016-11-23 132 views
0

我正在處理一些調查數據,並想知道我是否可以重新排列數據以使其更加可用。結果被分類爲1-5,我希望首選表格按價值和問題分組來計算結果。重新排列數據集

原始表:

year | month | customer_id | survey | q1 | q2 | q3 | q4 | q5 | q6 ----> q29 
-----|-------|-------------|--------|----|----|----|----|----|--- 
2016 | Oct | ABC12345678 | 1  | 1 | 2 | 3 | 1 | 2 | 3 
2016 | Oct | DEF12345678 | 1  | 2 | 1 | 4 | 2 | 1 | 1 
2016 | Oct | GHI12345678 | 1  | 4 | 2 | 1 | 1 | 3 | 2 
2016 | Oct | JKL12345678 | 1  | 2 | 3 | 2 | 4 | 1 | 3 
2016 | Oct | MNO12345678 | 1  | 5 | 2 | 3 | 1 | 2 | 3 
2016 | Oct | PQR12345678 | 1  | 3 | 4 | 4 | 2 | 4 | 4 
2016 | Oct | STU12345678 | 1  | 1 | 5 | 3 | 1 | 2 | 5 
2016 | Oct | VWX12345678 | 1  | 2 | 2 | 4 | 2 | 1 | 1 

首選表:

Year | Month | Survey | Question | 1 | 2 | 3 | 4 | 5 | 
-----|-------|--------|----------|----|----|----|----|----| 
2016 | Oct | 1 | q1  | 80 | 45 | 25 | 63 | 89 | 
2016 | Oct | 1 | q2  | 65 | 75 | 35 | 53 | 69 | 

我可以用一個基本的選擇查詢,但這樣做對每一個問題最終將有29個工會做到這一點,必須有更快的方法。

問候,

尼爾

+1

用你正在使用的數據庫標記你的問題。但是,這種重組通常需要複雜的查詢。 –

+0

想象戈登,不知道然後:) –

+0

這可以做比29聯盟簡單得多。你首先需要解決這個問題,然後做條件聚合。對於我們真正的幫助,如果你可以發佈ddl和樣本數據,那將是非常好的。 –

回答

2

這是我會用,直到有人張貼一個更好的解決方案:

<!-- language: lang-sql --> 

use tempdb; 
create table #tempsurvey (year int, month varchar(32), customer_id varchar(32), survey int, [q1] int, [q2] int, [q3] int, [q4] int, [q5] int, [q6] int, [q7] int, [q8] int, [q9] int, [q10] int, [q11] int, [q12] int, [q13] int, [q14] int, [q15] int, [q16] int, [q17] int, [q18] int, [q19] int, [q20] int, [q21] int, [q22] int, [q23] int, [q24] int, [q25] int, [q26] int, [q27] int, [q28] int, [q29] int); 
insert into #tempsurvey values (2016,'Oct', 'ABC12345678', 1, 1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2); 
insert into #tempsurvey values (2016,'Oct', 'DEF12345678', 1, 4,5,1,4,5,1,4,5,1,4,5,1,4,5,1,4,5,1,4,5,1,4,5,1,4,5,1,4,5); 

with cte as (
    select t.[year], t.[month], t.customer_id, t.survey, x.question, x.answer 
    from #tempsurvey t 
     cross apply (values ('q1',q1) ,('q2',q2) ,('q3',q3) ,('q4',q4) ,('q5',q5) ,('q6',q6) ,('q7',q7) ,('q8',q8) ,('q9',q9) ,('q10',q10) ,('q11',q11) ,('q12',q12) ,('q13',q13) ,('q14',q14) ,('q15',q15) ,('q16',q16) ,('q17',q17) ,('q18',q18) ,('q19',q19) ,('q20',q20) ,('q21',q21) ,('q22',q22) ,('q23',q23) ,('q24',q24) ,('q25',q25) ,('q26',q26) ,('q27',q27) ,('q28',q28) ,('q29',q29)) 
     as x (Question,Answer) 
) 
    select [year], [month], [survey], question, [1]=sum(case when answer=1 then 1 else 0 end), [2]=sum(case when answer=2 then 1 else 0 end), [3]=sum(case when answer=3 then 1 else 0 end), [4]=sum(case when answer=4 then 1 else 0 end), [5]=sum(case when answer=5 then 1 else 0 end) 
    from cte 
     group by [year], [month], [survey], question; 

    drop table #tempsurvey; 

布拉德·舒爾茨跨應用:http://bradsruminations.blogspot.com/search/label/CROSS%20APPLY

+0

這是美麗的作品SqlZim和完美! –

+0

謝謝!如果你想像Joe Enos所建議的那樣重新組織表,你可以使用cte的內部來生成它。此外,如果您的答案可以爲空,並且您想要處理該問題,請切換到外部申請。 – SqlZim

+0

聽起來不錯,現在使用問題欄和調查欄我現在可以撤回問題標題!你是最好的SqlZim! –

1

肖恩是正確的。 它會這樣:

with subquery as (
    select year, month, survey, question, tempVal from #table 
    unpivot 
    (tempVal for question in (q1, q2, q3, q4, q5, q6, q7, ..., q29)) as up 
) 
select year, month, survey, question, 
    sum(case when tempVal = 1 then 1 else 0 end) as a1, 
    sum(case when tempVal = 2 then 1 else 0 end) as a2, 
    sum(case when tempVal = 3 then 1 else 0 end) as a3, 
    sum(case when tempVal = 4 then 1 else 0 end) as a4, 
    sum(case when tempVal = 5 then 1 else 0 end) as a5 
from subquery 
group by year, month, survey, question 
+0

感謝Nimdil,我在SqlZim執行交叉應用時寫了這個中途。也正確,非常感謝 –

+0

http://rextester.com/XXS27650 – McNets