【问题标题】:Joining one table twice and counting records两次加入一个表并计算记录
【发布时间】:2020-04-26 18:19:15
【问题描述】:

我有 2 张桌子。一个是主人,另一个是查找。 TblMstr 和 Cmaster

Tblmstr

Id  QuestionId   ChoiceId ChoiceMaster
1    2             1       Strongly Agree
1    3             2       Disagree
1    4             3       Agree
1    5             11       null
2    2             2       Disagree
2    3             5       Disagree
2    4             9       Agree
2    5             12       null

Cmaster

ChoiceId  QuestionId    ChoiceName
1             2         Strongly Agree
2             2         Disagree
3             2         Agree
4             3         Strongly Agree
5             3         Disagree
6             3         Agree     
7             4         Strongly Agree
8             4         Disagree
9             4         Agree  
10            5         Registered Nurse
11            5         Nurse Practitioner
12            5         Pharmacist

我需要让每个问题有多少注册护士有强烈同意、不同意和同意。执业护士和药剂师也是如此。

【问题讨论】:

    标签: sql sql-server database join


    【解决方案1】:

    嗯。 . .您可以join 两次。一次获得职业,一次获得其他问题的答复:

    select co.choicename as occupation,
           sum(case when c.choicename = 'Strongly Agree' then 1 else 0 end) as cnt_strongly_agree,
           sum(case when c.choicename = 'Agree' then 1 else 0 end) as cnt_agree,
           sum(case when c.choicename = 'Disagree' then 1 else 0 end) as cnt_disagree
    from Tblmstr m join
         Cmaster co
         on co.id = m.choiceid and co.questionid = 5 join
         Cmaster c
         on co.id = m.choiceid and co.questionid <> 5
    group by co.choicename
    

    【讨论】:

      猜你喜欢
      • 2020-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-24
      • 2011-10-23
      • 1970-01-01
      • 1970-01-01
      • 2015-10-09
      相关资源
      最近更新 更多