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