存储过程返回重复记录

时间:2017-04-10 14:23:42

标签: sql-server stored-procedures

我有以下表格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)TitleThemeAssociation

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

6)TitleSubjectAssociaton

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

7)TitleGradeAssociaton

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

我有一个存储过程如下:

CREATE PROCEDURE [dbo].[GetTitlesPageWise]
       @PageIndex INT = 1
      ,@PageSize INT = 10
      ,@searchText NVARCHAR(250) = ''
      ,@PageCount INT OUTPUT
AS
BEGIN
      SET NOCOUNT ON;
      SET FMTONLY OFF;

select ROW_NUMBER() over 
       (
         ORDER BY [id] ASC
       ) as RowNumber,
        T.Id As [Title ID],
        T.Title,
        H.Theme,
        S.Subject,
        G.Grade
into #Results
From    Titles  T
Outer Apply
(
    Select  Stuff(( Select ', ' + Name 
                    From    Themes                  H
                    Join    TitleThemeAssociaton   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    TitleSubjectAssociation  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    TitleGradeAssociation    TG  On  G.Id = TG.GradeId
                    Where   TG.TitleId = T.Id 
                    For Xml Path('')), 1, 2, '') As Grade
    From    Grades
) G
WHERE
    t.title Like @searchText + '%'
    AND
    (
        H.Theme Is Null
        Or      S.Subject Is Null
        Or      G.Grade Is Null
    )

      DECLARE @RecordCount INT
      SELECT @RecordCount = COUNT(*) FROM #Results

      SET @PageCount = CEILING(CAST(@RecordCount AS DECIMAL(10, 2)) / CAST(@PageSize AS DECIMAL(10, 2)))
      PRINT       @PageCount

      SELECT * FROM #Results
      WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1

      DROP TABLE #Results
END

我想要不同的标题ID,输出如下所示,但查询给出了重复的结果。如果分配了主题,主题和成绩所有值,则应从结果中排除该记录。在上述情况下,应排除标题ID 1 ,因为那里存在所有三个值。我需要帮助来解决问题。

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

.............

1 个答案:

答案 0 :(得分:0)

<强> SQL DEMO

SELECT 
       ROW_NUMBER() OVER  (ORDER BY [ID]) rn, 
       [ID],
       [Title],
       Subject = STUFF ((
                      SELECT ',' + S.[Name] 
                      FROM TitleSubjectAssociation TS
                      JOIN Subjects S
                        ON TS.SubjectId = S.Id 
                      WHERE TS.[TitleID] = T.[ID]
                      FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, ''
                    ),
       Theme = STUFF ((
                      SELECT ',' + TH.[Name] 
                      FROM TitleThemeAssociation TA
                      JOIN Themes TH
                        ON TA.[ThemeID] = TH.[ID]
                      WHERE TA.[TitleID] = T.[ID]
                      FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, ''
                    ),
       Grade = STUFF ((
                      SELECT ',' + G.[Name] 
                      FROM TitleGradeAssociation TG
                      JOIN Grades G
                        ON TG.[GradeID] = G.[ID]
                      WHERE TG.[TitleID] = T.[ID]
                      FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, ''
                    )                                        
FROM Titles T;

如果您需要过滤器,可以尝试检查结果中有多少,。两个,表示三个元素。

WHERE len(Subject) - len(replace(Subject,',','')) != 2
  AND len(Theme) - len(replace(Theme,',','')) != 2
  AND len(Grade) - len(replace(Grade,',','')) != 2

<强>输出

enter image description here