2016-11-23 4 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

関連する問題