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

Sql 如何在不创建新表的情况下从三个表中创建所需答案?,sql,sql-server,sql-server-2008,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

现在我想这样回答:-

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

这里我们使用
poor
来表示
答案1
good
来表示
答案2
excellent
来表示
答案3
,因为您似乎只需要三个不同的类别。实现这一点的最简单方法是使用如下条件计数:

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
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
操作符执行此操作,如下所示:

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
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

似乎您需要使用
count
pivot
。您能解释一下如何使用吗@佐哈尔Peled@jpw你的答案正确…谢谢你的帮助。。。。