添加ROW_NUMBER()以查询丢失DISTINCT,失败了分页结果尝试的目的

时间:2014-01-11 01:17:52

标签: sql distinct one-to-many row-number delimited

我在使用DISTINCT并使用ROW_NUMBER()带回真正的LEFT JOIN记录集时遇到问题,其连接的结果又被连接到逗号分隔的列表中{ {1}}技巧。

目标是在Web应用程序中实现分页,因为可以返回大量记录,每次只返回一个子集,具体取决于所选的页码。

考虑以下SQL:

FOR XML PATH

它按预期带回了一个非常好的SELECT DISTINCT IndustryCode, STUFF ( (SELECT ',' + FE_LocationCode FROM tblLegacy_Codes i2 WHERE c.IndustryCode = i2.IndustryCode FOR XML PATH('')) ,1,1,'') AS LegacyList FROM Common_Clli c LEFT JOIN tblLegacy_Codes legacy ON c.IndustryCode = legacy.IndustryCode WHERE FE_LocationCode LIKE 'AUS%' 列表:

DISTINCT

但是 - 当ROW_NUMBER语法被添加到上面查询的第一行时,它看起来像这样:

IndustryCode    LegacyList
AUSTTX85        AUST.GET,AUST.LDD,AUST.UU4
AUSTTXTE        AUST.TE,AUST.TEH
AUSUTX78        AUST.AX3
SCHWAS01        AUSC01C1,AUSC01UT
SZBGASAH        AUSB01C1,AUSB01OB,AUSB01TA
SZBGASAI        AUSB01SN,AUVI01SN,AUVI02SN
SZBGASAK        AUSB03C1,AUSB03V1

结果现在为每个IndustryCode包含一行,因为LegacyList中有很多行,因为SELECT DISTINCT ROW_NUMBER() OVER(ORDER BY IndustryCode) AS rowNum, IndustryCode,... 当然现在包含rowNum。如果不包括整个结果集,则可以看到列表中的第一个IndustryCode,因为它有3个相关的子记录:

DISTINCT

当然,分页的最终实现是能够用

包装所有提到的的SQL
rowNum  IndustryCode    LegacyList
1       AUSTTX85        AUST.GET,AUST.LDD,AUST.UU4
2       AUSTTX85        AUST.GET,AUST.LDD,AUST.UU4
3       AUSTTX85        AUST.GET,AUST.LDD,AUST.UU4

目前我不得不首先执行SQL而不使用ROW_NUMBER()并将结果放入临时表中,然后从那里获取最终产品。不幸的是,通过每次只获取一部分记录,完全打败了最小化负载的意图。

我相信我只是不知道的东西。这总是一个安全的赌注。许多人都在寻求帮助。

满足所有要求的最终产品最终成为下面的SQL。虽然提供的前两个解决方案都很有帮助,但两个解决方案都没有解决查询的整个结果首先在临时样式表中,无论该表是公用表表达式还是其他表。这StackOverflow thread导致我完全解决了问题。

SELECT * FROM (
  [ the SQL you've already seen ]
WHERE rowNum BETWEEN x and y

3 个答案:

答案 0 :(得分:3)

怎么样?
SELECT ROW_NUMBER() OVER(ORDER BY IndustryCode) AS rowNum, IndustryCode, LegacyList
FROM
(
SELECT DISTINCT IndustryCode, 
STUFF ( 
    (SELECT ',' + FE_LocationCode
    FROM tblLegacy_Codes i2
    WHERE c.IndustryCode = i2.IndustryCode
    FOR XML PATH(''))
,1,1,'') AS LegacyList
FROM Common_Clli c
LEFT JOIN tblLegacy_Codes legacy ON c.IndustryCode = legacy.IndustryCode
WHERE FE_LocationCode LIKE 'AUS%'
) T
ORDER BY rowNum

答案 1 :(得分:1)

您应该使用表格表达式,例如

WITH MYTABLE AS (
SELECT DISTINCT IndustryCode, 
STUFF ( 
    (SELECT ',' + FE_LocationCode
    FROM tblLegacy_Codes i2
    WHERE c.IndustryCode = i2.IndustryCode
    FOR XML PATH(''))
,1,1,'') AS LegacyList
FROM Common_Clli c
LEFT JOIN tblLegacy_Codes legacy ON c.IndustryCode = legacy.IndustryCode
WHERE FE_LocationCode LIKE 'AUS%'
) SELECT ROW_NUMBER() OVER(ORDER BY IndustryCode) AS ROWNUM
, IndustryCode, LegacyList FROM MYTABLE

答案 2 :(得分:-1)

System.Data.dll中出现'System.Data.SqlClient.SqlException'类型的异常,但未在用户代码中处理

指定了太多参数。

在参数中解决了错误

@productCodeId varchar(15),
@productName varchar(60), 
@categoryCodeId varchar(15),
@subCategoryCodeId varchar(15),
@childSubCategoryCodeId varchar(15),
@productKeyword varchar(max),
@productCode varchar(25),
@productImage image=null,
@productDescShort varchar(max),
@ProductDescFull varchar(max),
@qualityProcessCertification varchar(max),
@userCodeId varchar(15),

 @productInfoCodeId varchar(15),
@productAdvantage varchar(max),
@productPriceInformation varchar(25),
@productAddPrice decimal(10,0),
@sampleAvailability varchar(5),
@deliveryLeadTime varchar(50),
@deliveryTerm varchar(max),
@minQuantity varchar(25),
@status varchar(15),


@id varchar(15)=null output ,
@id1 varchar(15)=null output   

AS    BEGIN

- 声明@id varchar(15)= null --declare @ id1 varchar(15)= null

从tbl_product中选择@ id = tp.productCodeId,@ id1 = tpi.productCodeId为tp    内连接tbl_ProductInformation为tpi    在tp.productCodeId = tpi.productCodeId上    其中tp.productCodeId =@productCodeId和tpi.productCodeId=@productCodeId

if @id=@productCodeId and @id1=@productCodeId

开始     更新tbl_product设置
    PRODUCTNAME = @产品名称,categoryCodeId = @ categoryCodeId,     subCategoryCodeId = @ subCategoryCodeId,childSubCategoryCodeId = @ childSubCategoryCodeId,     productKeyword = @ productKeyword,productCode = @ productCode,productImage = @ productImage,     productDescShort = @ productDescShort,     ProductDescFull = @ ProductDescFull,qualityProcessCertification = @ qualityProcessCertification,     userCodeId = @ userCodeId     从tbl_product tp,tbl_ProductInformation tpi     其中tp.productCodeId = @productCodeId和tpi.productCodeId=@productCodeId

update tbl_ProductInformation set productInfoCodeId=@productInfoCodeId, productAdvantage=@productAdvantage,
productPriceInformation=@productPriceInformation,
productAddPrice=@productAddPrice,sampleAvailability=@sampleAvailability,deliveryLeadTime=@deliveryLeadTime,
deliveryTerm=@deliveryTerm,minQuantity=@minQuantity,productCodeId=@productCodeId
FROM tbl_product tp, tbl_ProductInformation tpi

其中tp.productCodeId =@productCodeId和tpi.productCodeId =@productCodeId  结束
    其他 开始
    插入到tbl_product(productCodeId,productName,categoryCodeId,     subCategoryCodeId,childSubCategoryCodeId,productKeyword,产品代码,productImage,productDescShort,     ProductDescFull,qualityProcessCertification,userCodeId)     值(@ productCodeId,@产品名称,@ categoryCodeId,     @ subCategoryCodeId,@ childSubCategoryCodeId,@ productKeyword,@ PRODUCTCODE,@ productImage,@ productDescShort,     @ ProductDescFull,@ qualityProcessCertification,@ userCodeId)

insert into tbl_ProductInformation(productInfoCodeId,productAdvantage,
productPriceInformation,productAddPrice,sampleAvailability,deliveryLeadTime,deliveryTerm,minQuantity,productCodeId,status)
values(@productInfoCodeId,@productAdvantage,@productPriceInformation,@productAddPrice,
@sampleAvailability,@deliveryLeadTime,@deliveryTerm,@minQuantity,@productCodeId,@status)