我有以下表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
...
我有一个存储procudure如下:
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
.............
什么是您预期的结果? –