我正在开发一个应用程序,允许用户从许多“页面”构建“书”,然后按照他们喜欢的顺序放置它们。多人可以构建同一本书(相同顺序的相同页面)。这些书是在用户处理和打印之前由用户构建的,因此我需要将具有相同布局的书籍组合在一起(相同页面的顺序相同)。我一生中写了一百万条查询,但由于某种原因,我无法理解如何做到这一点。
我可以简单地编写一个大的SELECT查询,然后遍历结果并构建具有相同序列中相同页面的对象数组,但我试图弄清楚如何使用一个查询执行此操作。
这是我的数据布局:
dbo.Books
BookId
Quantity
dbo.BookPages
BookId
PageId
Sequence
dbo.Pages
PageId
DocName
答案 0 :(得分:0)
所以,我需要对一些事情做一些澄清:
通过这些问题的答案,我可以更好地衡量您的需求。
回答我的问题,我也认为:
答案 1 :(得分:0)
只有当页数=匹配计数时,这些书才相同。
我开始时标记为TSQL。这可能与SQL上的语法不同。
;WITH BookPageCount
AS
(
select b1.bookID, COUNT(*) as [individualCount]
from book b1 with (nolock)
group by b1.bookID
),
BookCombinedCount
AS
(
select b1.bookID as [book1ID], b2.bookID as [book2ID], COUNT(*) as [combindCount]
from book b1 with (nolock)
join book b2 with (nolock)
on b1.bookID < b2.bookID
and b1.squence = b2.squence
and b1.page = b2.page
group by b1.bookID, b2.bookID
)
select BookCombinedCount.book1ID, BookCombinedCount.book2ID
from BookCombinedCount
join BookPageCount as book1 on book1.bookID = BookCombinedCount.book1ID
join BookPageCount as book2 on book2.bookID = BookCombinedCount.book2ID
where BookCombinedCount.combindCount = book1.individualCount
and BookCombinedCount.combindCount = book2.individualCount.PageCount