SQL服务器 - 按逻辑分组

时间:2016-06-13 15:24:09

标签: sql sql-server group-by

这是我的样本数据:

Autonumber   ProductSKU    Quantity
---------    ----------    --------
ABCD123      00001597      42
ABCD123      00001600      42
ABCD124      00001597      35
ABCD124      00001600      35
ABCD125      00001597      39
ABCD125      00001600      39

我必须根据AutonumberQuantity派生ProductSKU列值。 Autonumber& Quantity时应更改ProductSKU Autonumber更改。 我怎样才能做到这一点?我有逻辑来增加IF @p_channelname ='ABCD' BEGIN ;WITH CTE_AUTONUMBER_ABCD_AMER AS (SELECT RECORDSEQ ,'ABCD'+CAST(@PrevOrderID+DENSE_RANK()OVER(ORDER BY (RECORDSEQ )) AS NVARCHAR(MAX)) AS AUTONUMBER_UPDATE , AUTONUMBER , LINENUMBER , DENSE_RANK()OVER(ORDER BY (RECORDSEQ )) AS LINENUMBER_UPDATE FROM TABLE1 TGT WHERE TGT.[INTERFACE NAME] =@p_interface AND TGT.[FILENAME]= @p_sourcefilename AND TGT.CHANNEL = @p_channelname AND TGT.GEO='America' ) UPDATE CTE_AUTONUMBER_ABCD_AMER SET AUTONUMBER = AUTONUMBER_UPDATE, LINENUMBER=LINENUMBER_UPDATE ,但逻辑上与组混淆。请帮助我。

编辑(从答案部分的OP帖子中复制)

我有一个计算自动编号的逻辑,每行增加一个。

查询:

Autonumber   ProductSKU    Quantity
---------    ----------    --------
ABCD123      00001597      42
ABCD124      00001600      42
ABCD125      00001597      35
ABCD126      00001600      35
ABCD127      00001597      39
ABCD128      00001600      39

目前的自动编号值为:

Autonumber   ProductSKU    Quantity
---------    ----------    --------
ABCD123      00001597      42
ABCD123      00001600      42
ABCD124      00001597      35
ABCD124      00001600      35
ABCD125      00001597      39
ABCD125      00001600      39

我希望将自动编号值生成为:

{{1}}

这是当前的查询: ; WITH CTE_AUTONUMBER_EPIC_AMER AS             (选择RECORDSEQ                     ,情况@P_CHANNELNAME ='Epic'                                 然后'Epic'+ CAST(CAST(AUTONUMBER AS INT)AS NVARCHAR(MAX))
                                结束为AUTONUMBER_UPDATE                     ,AUTONUMBER                     , 电话号码                     ,DENSE_RANK()OVER(ORDER BY(RECORDSEQ))AS LINENUMBER_UPDATE             来自IC_CTXS_TRANSACTION_SOURCEFEED TGT             在哪里TGT。[接口名称] = @ p_interface                                 AND TGT。[FILENAME] = @p_sourcefilename                                 AND TGT.CHANNEL = @p_channelname                                         和TGT.GEO ='美国'                                         AND ISNUMERIC(CAST(AUTONUMBER AS INT))= 1                                         和AUTONUMBER不是空的             )更新CTE_AUTONUMBER_EPIC_AMER                     SET AUTONUMBER = AUTONUMBER_UPDATE,                     LINENUMBER = LINENUMBER_UPDATE

如何在不干扰查询结构的情况下包含逻辑?

1 个答案:

答案 0 :(得分:-1)

这是一个新的查询,带有你想要的逻辑,但对我来说似乎很疯狂:

SELECT * INTO TABLE1 FROM (VALUES
(00001597,42,1),(00001600,42,2),(00001597,35,3),(00001600,35,4),(00001597,39,5),
(00001600,39,6)) as x(ProductSKU, Quantity, RECORDSEQ);
GO
;WITH Change as (
SELECT t1.*, 
    CASE WHEN IsNull(t1.ProductSKU,-1) != IsNull(t2.ProductSKU,-1) 
        and IsNull(t1.Quantity,-1) != IsNull(t2.Quantity,-1) 
    THEN 1 ELSE 0 END as Change
FROM TABLE1 as t1
LEFT JOIN TABLE1 as t2 ON t1.RECORDSEQ = t2.RECORDSEQ + 1
)
SELECT 'ABCD' + CAST(122 + RANK() 
    OVER(PARTITION BY Change ORDER BY RECORDSEQ) as VARCHAR) as Autonumber
    , ProductSKU, Quantity
FROM Change
ORDER BY RECORDSEQ;
GO

它会准确地返回您想要的内容:

Autonumber  ProductSKU  Quantity
ABCD123     1597        42
ABCD123     1600        42
ABCD124     1597        35
ABCD124     1600        35
ABCD125     1597        39
ABCD125     1600        39