所以,我知道只有足够的SQL是危险的,我正在按照一个例子从表中提取一页的记录:
SELECT TOP #arguments.perPage# * FROM (
SELECT DISTINCT ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS rownum, productdiagramparts.productdiagramid AS productdiagramid, products.id AS productid, products.title AS producttitle, totalRows = COUNT(*) OVER()
FROM manufacturers
INNER JOIN products ON manufacturers.id = products.manufacturerid
INNER JOIN productdiagramparts ON products.id = productdiagramparts.productid
INNER JOIN productdiagrams ON productdiagramparts.productdiagramid = productdiagrams.id
WHERE #whereClause#
) _tmpInlineView
WHERE rownum > #offset#
ORDER BY producttitle
围绕这个的SELECT TOP当然只拉取当前页面的记录。麻烦的是,我想删除的最里面的SELECT语句中有重复项,但使用DISTINCT不能如上所示工作,因为已经为外部查询编号了行。在对行进行编号之前,如何使我最内层的SELECT结果不同?
以下是基于以下接受答案的解决方案:
SELECT TOP#arguments.perPage#* FROM(
SELECT ROW_NUMBER()OVER(ORDER BY(SELECT 1))AS rownum, productdiagramid,productid,producttitle,totalRows = COUNT(*)OVER() FROM(
SELECT DISTINCT productdiagramparts.productdiagramid AS productdiagramid, products.id AS productid,products.title AS producttitle 来自制造商 INNER JOIN产品在manufacturer.id = products.manufacturerid INNER JOIN productdiagramparts ON products.id = productdiagramparts.productid INNER JOIN productdiagrams ON productdiagramparts.productdiagramid = productdiagrams.id 在哪里#whereClause#
)_ tmpDupRemove
)_ tmpInlineView
在哪里rownum> #偏移# ORDER BY producttitle
答案 0 :(得分:1)
这是第二次使用ROW_NUMBER
函数的方法。最内层SELECT
根据重复组分配行号。然后,仅返回行号为1的行以删除重复项。最后,完成了分配行号的过程。
SELECT TOP #arguments.perPage# *
FROM (
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS rownum,
productdiagramparts.productdiagramid AS productdiagramid,
products.id AS productid, products.title AS producttitle, totalRows = COUNT(*) OVER()
FROM (
SELECT ROW_NUMBER() OVER PARTITION BY productdiagramparts.productdiagramid, products.id ORDER BY (SELECT 1)) AS dup_sequence, *
FROM manufacturers
INNER JOIN products ON manufacturers.id = products.manufacturerid
INNER JOIN productdiagramparts ON products.id = productdiagramparts.productid
INNER JOIN productdiagrams ON productdiagramparts.productdiagramid = productdiagrams.id
WHERE #whereClause#
) _tmpDupRemove
WHERE dup_sequence = 1
) _tmpInlineView
WHERE rownum > #offset#
ORDER BY producttitle
答案 1 :(得分:0)
您可以使用GROUP BY子句。
SQLServer中的顺序:GROUP BY - > SELECT ROW_NUMBER() - > DISTINCT。因此,您需要GROUP BY而不是DISTINCT
SELECT TOP #arguments.perPage# * FROM (
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS rownum,
productdiagramparts.productdiagramid AS productdiagramid,
products.id AS productid,
products.title AS producttitle,
totalRows = COUNT(*) OVER()
FROM manufacturers
INNER JOIN products ON manufacturers.id = products.manufacturerid
INNER JOIN productdiagramparts ON products.id = productdiagramparts.productid
INNER JOIN productdiagrams ON productdiagramparts.productdiagramid = productdiagrams.id
GROUP BY productdiagramparts.productdiagramid, products.id, products.title
WHERE #whereClause#
) _tmpInlineView
WHERE rownum > #offset#
ORDER BY producttitle
答案 2 :(得分:0)
DECLARE @startrow Int = 0
DECLARE @recsperpage Int = 10
SELECT [Inner2].[productdiagramid],
[Inner2].[productid],
[Inner2].[producttitle]
FROM
(
SELECT ROW_NUMBER() OVER
(ORDER BY
[Inner1].[producttitle]) AS [ROW_NUMBER],
[Inner1].[productdiagramid],
[Inner1].[productid],
[Inner1].[producttitle]
FROM
(
SELECT DISTINCT pdp.productdiagramid, p.id as productid, p.title as producttitle
FROM manufacturers m
INNER JOIN products p ON m.id = p.manufacturerid
INNER JOIN productdiagramparts pdp ON p.id = pdp.productid
INNER JOIN productdiagrams pd ON pdp.productdiagramid = pd.id
WHERE #whereclause#
) AS Inner1
) as Inner2
WHERE Inner2.[ROW_NUMBER] BETWEEN @startrow + 1 AND @startrow + @recsperpage
ORDER BY [Inner2].[ROW_NUMBER]