Sql server 显示三个表中的数据,不重复

Sql server 显示三个表中的数据,不重复,sql-server,tsql,Sql Server,Tsql,我有三个分层设置的表。上表: Group ID|Title RiskFactor GroupID|Question Choices RiskFactorID|Label|Score 存在一对多关系,其中一组有多个风险因素,每个风险因素有多个答案,每个答案都有一个分数。因此,具有内部联接或左联接的查询会给出相同的结果 查询: SELECT Groups.Title, R

我有三个分层设置的表。上表:

Group             ID|Title
RiskFactor        GroupID|Question
Choices           RiskFactorID|Label|Score
存在一对多关系,其中一组有多个风险因素,每个风险因素有多个答案,每个答案都有一个分数。因此,具有内部联接或左联接的查询会给出相同的结果

查询:

    SELECT        
           Groups.Title, 
           RiskFactors.Question,  
           Choices.Label, 
           Choices.Score
    FROM            
           Groups Inner JOIN
           RiskFactors ON Groups.GroupId = RiskFactors.GroupId inner JOIN
           Choices ON RiskFactors.RiskFactorId = Choices.RiskFactorId
    ORDER BY 
           Groups.groupid
将产生如下结果:

Title                       Question                                                   Label  Score
4.1 - Employment & Finance  Is your current job in JEOPARDY?  (Describe why in notes)   Yes 3
4.1 - Employment & Finance  Is your current job in JEOPARDY?  (Describe why in notes)   Maybe   2
4.1 - Employment & Finance  Is your current job in JEOPARDY?  (Describe why in notes)   NA  1
4.1 - Employment & Finance  Is your current job in JEOPARDY?  (Describe why in notes)   No  1
4.1 - Employment & Finance  How LONG was (is) your longest FULL TIME job?   NA  1
4.1 - Employment & Finance  How LONG was (is) your longest FULL TIME job?   0-6 Months  1
4.1 - Employment & Finance  How LONG was (is) your longest FULL TIME job?   1-3 Years   1
4.1 - Employment & Finance  How LONG was (is) your longest FULL TIME job?   6-12 Months 1
4.1 - Employment & Finance  How LONG was (is) your longest FULL TIME job?   Only Part Time  1
4.1 - Employment & Finance  How LONG was (is) your longest FULL TIME job?   More than 3 Years   1
TITLE                       QUESTION                                                    Label SCORE
4.1 - Employment & Finance  Is your current job in JEOPARDY?  (Describe why in notes)   Yes 3
                                                                                        Maybe   2
                                                                                        NA  1
                                                                                        No  1
                            How LONG was (is) your longest FULL TIME job?   NA  1
                                                                            0-6 Months  1
                                                                            1-3 Years   1
                                                                            6-12 Months 1
                                                                            Only Part Time  1
                                                                            More than 3 Years   1
我想删除title和question字段中的重复项,并将它们替换为NULL或空值,这样看起来更像这样:

Title                       Question                                                   Label  Score
4.1 - Employment & Finance  Is your current job in JEOPARDY?  (Describe why in notes)   Yes 3
4.1 - Employment & Finance  Is your current job in JEOPARDY?  (Describe why in notes)   Maybe   2
4.1 - Employment & Finance  Is your current job in JEOPARDY?  (Describe why in notes)   NA  1
4.1 - Employment & Finance  Is your current job in JEOPARDY?  (Describe why in notes)   No  1
4.1 - Employment & Finance  How LONG was (is) your longest FULL TIME job?   NA  1
4.1 - Employment & Finance  How LONG was (is) your longest FULL TIME job?   0-6 Months  1
4.1 - Employment & Finance  How LONG was (is) your longest FULL TIME job?   1-3 Years   1
4.1 - Employment & Finance  How LONG was (is) your longest FULL TIME job?   6-12 Months 1
4.1 - Employment & Finance  How LONG was (is) your longest FULL TIME job?   Only Part Time  1
4.1 - Employment & Finance  How LONG was (is) your longest FULL TIME job?   More than 3 Years   1
TITLE                       QUESTION                                                    Label SCORE
4.1 - Employment & Finance  Is your current job in JEOPARDY?  (Describe why in notes)   Yes 3
                                                                                        Maybe   2
                                                                                        NA  1
                                                                                        No  1
                            How LONG was (is) your longest FULL TIME job?   NA  1
                                                                            0-6 Months  1
                                                                            1-3 Years   1
                                                                            6-12 Months 1
                                                                            Only Part Time  1
                                                                            More than 3 Years   1

我尝试过使用CTE和透视表,但都没有成功。我不是SQL开发人员,我相信这不是一个独特的问题。如何以分层的方式保存这些数据?在我将其放入网格之前,请设置网格控件以删除列中的重复项。

这里有一个可能的攻击,您可以尝试-您尚未提供任何示例数据-免责声明!未测试-只需使用
row\u number
在每个组内进行排序,如果不是组的第一行,则使用空字符串(或null或其他内容):

SELECT        
  Iif(Row_Number() over(partition by groups.title order by Groups.groupid)=1,Groups.Title,'') Title, 
  Iif(Row_Number() over(partition by groups.title, RiskFactors.question order by Groups.groupid)=1,RiskFactors.Question,'') Question, 
  Choices.Label, 
  Choices.Score
FROM            
  Groups Inner JOIN
  RiskFactors ON Groups.GroupId = RiskFactors.GroupId inner JOIN
  Choices ON RiskFactors.RiskFactorId = Choices.RiskFactorId
ORDER BY 
  Groups.groupid

这类任务应该是渲染应用程序的一项功能-尝试将其作为查询的一部分只会让您的生活变得艰难。您要求的任务很容易完成,但看起来您正在尝试完成演示层的工作?通常我会将其作为两个独立的
选择
来完成,将它们映射到客户端的dictionary/hashmap中,并在大多数情况下使用它来将其推送到表示层,但这一次,用户只需要将数据转储到excel文件中,而不需要重复。谢谢,我将尝试一下,看看会发生什么。