sql - 显示部分或完全缺少关联值的标题

标签 sql sql-server t-sql stored-procedures

我在为以下场景编写 SQL 查询时遇到问题。我需要有人帮助我编写查询。

我有以下7个表:

1) 标题

ID    Title                                                        Author         
-------------------------------------------------------------------------
1     The Hidden Language of Computer Hardware and Software   Charles Petzold
2     Paths, Dangers, Strategies                              Nick Bostrom
3     The Smart Girl's Guide to Privacy                       Violet Blue
4     Introduction to Algorithms                              Thomas H. Cormen
5     Machine Learning in Action                              Peter Harrington
...

2) 主题

ID         Name
------------------------------------------
1          Science Fiction
2          Biography
3          Painting
...

3) 主题

ID           Name
-----------------------------------
1            Science 
2            Technology
3            Music
4            Geography
...

4) 成绩

ID            Name
------------------------------------
1             Grade 1
2             Grade 2
3             Grade 3
4             Grade 4
5             Grade 5
...

5) 标题主题关联

TitleID         ThemeID
------------------------------------------
1               1
1               3
4               2
4               3
...

6) TitleSubjectAssociation

TitleID          SubjectID
---------------------------------
1                1
1                3
2                1
2                3 
4                1
4                2  
...

7) 职称等级协会

TitleID              GradeID
1                    1
1                    2
1                    3
2                    1
2                    2
...

我需要编写一个查询来仅显示缺少三个值(主题、主题和成绩)中任何一个或未完全分配值的标题。如果分配了所有三个值(主题、科目、成绩),我不应该显示标题。在上面的数据集中,由于 TitleID 1 具有所有三个值,因此它不应出现在列表中。 TitleID 2 仅分配了科目和成绩,但没有分配主题,因此应在输出中显示。列出标题时,如果标题有多个值,则应使用逗号 (,) 分隔符将它们联系起来。

所以上述数据集的最终输出应该如下:

输出:

Title ID      Title                      Theme        Subject           Grade
-------------------------------------------------------------------------------------------
2      Paths, Dangers, Strategies          -          Science, Music    Grade 1, Grade 2
3      The Smart Girl's Guide to Privacy   -           -                   -
4      Introduction to Algorithms         Biography, Painting    Science, Technology    -
5      Machine Learning in Action          -              -              -

最佳答案

您基本上要问两个问题。第一个是当 ThemeSubjectGrade 缺失时如何进行过滤。另一个是询问如何将这些项目连接到逗号分隔的列表中。

以下查询应该是您要查找的内容:

Select  Distinct
        T.Id As [Title ID],
        T.Title,
        H.Theme,
        S.Subject,
        G.Grade
From    Titles  T
Outer Apply
(
    Select  Stuff(( Select ', ' + Name 
                    From    Themes                  H
                    Join    TitleThemeAssociation   TH  On  H.Id = TH.ThemeId
                    Where   TH.TitleId = T.Id 
                    For Xml Path('')), 1, 2, '') As Theme
    From    Themes
) H
Outer Apply
(
    Select  Stuff(( Select ', ' + Name 
                    From    Subjects                S
                    Join    TitleSubjectAssociaton  TS  On  S.Id = TS.SubjectId
                    Where   TS.TitleId = T.Id 
                    For Xml Path('')), 1, 2, '') As Subject
    From    Subjects
) S
Outer Apply
(
    Select  Stuff(( Select ', ' + Name 
                    From    Grades                  G
                    Join    TitleGradeAssociaton    TG  On  G.Id = TG.GradeId
                    Where   TG.TitleId = T.Id 
                    For Xml Path('')), 1, 2, '') As Grade
    From    Grades
) G
Where   H.Theme Is Null
Or      S.Subject Is Null
Or      G.Grade Is Null

关于sql - 显示部分或完全缺少关联值的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43120310/

相关文章:

sql-server - SQL Server 体系结构指南

sql-server - 当没有数据时tsql填充xml标签

sql-server - SQL 从 row_number 中获取 2 位数字 Over() 函数

sql - 从 SQL 中的另一个表更新表给出了意外结果

mysql - 加入一张 table 2 次

mysql - 创建一个事件并插入

c# - 处理大量数据的任何好方法?

sql - postgresql - 将字符串转换为时间

sql-server - MSSQL有子句,我不知道查询出了什么问题

sql-server - 通过电子邮件发送 infopath 表单(作为附件)以供 SQL Server 2005 解析?