如何在不创建新表的情况下从三个表创建所需答案?

时间:2015-08-01 13:02:39

标签: sql sql-server sql-server-2008

create table survey_categories
(
    survey_cat_id int,
    survey_cat_name varchar(45),

    constraint pk_survey_cat_id primary key(survey_cat_id)
)

survey_cat_id    survey_cat_name
-------------------------------------
1                staff
2                Product Quality
3                Product Variety
4                suggestion

create table survey_question
(
    que_id int,
    questions varchar(90),
    ques_title int,

    constraint pk_que_id primary key(que_id),
    constraint fk_ques_title foreign key(ques_title) 
       references survey_categories(survey_cat_id)
)

que_id    questions                                            ques_title   
--------------------------------------------------------------------------- 
1         Please rate our staff?                               1
2         Please rate Quality of products?                     2
3         Please rate variety of our products?                 3
4         Any productswe should add or get back?               4
5         Any place you think we should open our new store?    4

create table survey_detail
(
    survey_id int,
    store_id int,
    ques_id int,
    answer varchar(45),

    constraint pk_survey_id primary key(survey_id),
    constraint fk_ques_id foreign key(ques_id) 
       references survey_question(que_id)
)

survey_id    store_id      ques_id      answer
---------------------------------------------------
1            1005          1            1
2            1005          1            1
3            1005          1            1
5            1005          3            1
6            1005          3            1
7            1005          1            1
9            1005          4            2
10           1005          5            3
11           1005          2            2
12           1005          5            2

现在我想这样回答: -

survey_cat_id   store_id   excellent   good  poor
---------------------------------------------------
1               1005       0            0    4
2               1005       0            1    0
3               1005       0            0    2
4               1005       1            2    0

此处我们对poor使用answer 1good使用answer 2excellent使用answer 3

1 个答案:

答案 0 :(得分:0)

由于您似乎只想要三个不同的类别,最简单的方法是使用这样的条件计数:

select 
    sc.survey_cat_id
    , sd.store_id
    , count(case when answer = 3 then answer end) as excellent
    , count(case when answer = 2 then answer end) as good
    , count(case when answer = 1 then answer end) as poor    
from survey_categories sc
join survey_question sq on sq.ques_title = sc.survey_cat_id
join survey_detail sd on sd.ques_id = sq.que_id
group by sc.survey_cat_id, sd.store_id

您也可以使用pivot运算符执行此操作:

select 
    survey_cat_id
    , store_id
    , [3] as Excellent
    , [2] as Good
    , [1] as Poor
from (
    select sc.survey_cat_id, sd.store_id, sd.answer
    from survey_categories sc
    join survey_question sq on sq.ques_title = sc.survey_cat_id
    join survey_detail sd on sd.ques_id = sq.que_id
) src
pivot ( 
    count(answer) for answer in ([1],[2],[3])
) p;