SQL - 按组

时间:2018-05-15 17:30:44

标签: sql duplicates

我们提前感谢您的帮助。我们应该根据CustLastname,CustFirstName,DealerNumber,ProductCode和Vin编号对每组发现的重复记录进行计算。

这是表格内容。

sCustLastName sCustFirstName sDealerNumber sProductCode  sVIN
    Ln1             Fn1         TWS00967        GAP     1HGCR2F5XHA151050
    Ln1             Fn1         TWS00967        GAP     1HGCR2F5XHA151050
    Ln1             Fn1         TWS00967        GAP     1HGCR2F5XHA151050
    Ln2             Fn2         TWS00968        GAP     WVWEV7AJXAW197040
    Ln2             Fn2         TWS00968        GAP     WVWEV7AJXAW197040
    Ln3             Fn3         TWS00967        GAP     2HGFC2F56GH536572
    Ln3             Fn3         TWS00967        GAP     2HGFC2F56GH536572
    Ln4             Fn4         TWS00967        PPM     19XFC2F76JE003513
    Ln4             Fn4         TWS00967        PPM     19XFC2F76JE003513
    Ln4             Fn4         TWS00967        PPM     19XFC2F76JE003513 

结果所需:

Count sCustLastName sCustFirstName sDealerNumber sProductCode  sVIN
1   Ln1             Fn1         TWS00967        GAP     1HGCR2F5XHA151050
2   Ln1             Fn1         TWS00967        GAP     1HGCR2F5XHA151050
3   Ln1             Fn1         TWS00967        GAP     1HGCR2F5XHA151050
1   Ln2             Fn2         TWS00968        GAP     WVWEV7AJXAW197040
2   Ln2             Fn2         TWS00968        GAP     WVWEV7AJXAW197040
1   Ln3             Fn3         TWS00967        GAP     2HGFC2F56GH536572
2   Ln3             Fn3         TWS00967        GAP     2HGFC2F56GH536572
1   Ln4             Fn4         TWS00967        PPM     19XFC2F76JE003513
2   Ln4             Fn4         TWS00967        PPM     19XFC2F76JE003513
3   Ln4             Fn4         TWS00967        PPM     19XFC2F76JE003513

以下是创建表格的代码:

Create Table Contract_Header
(
sCustLastName   varchar(20),
sCustFirstName  varchar(20),
sDealerNumber   nvarchar(20),
sProductCode    varchar(3),
sVIN    nvarchar(20)
)

Here is the code to insert the data:
Insert into Contract_Header (sCustLastName,sCustFirstName,sDealerNumber,sProductCode,sVIN) Values ('Ln1','Fn1','TWS00967','GAP','1HGCR2F5XHA151050')
Insert into Contract_Header (sCustLastName,sCustFirstName,sDealerNumber,sProductCode,sVIN) Values ('Ln1','Fn1','TWS00967','GAP','1HGCR2F5XHA151050')
Insert into Contract_Header (sCustLastName,sCustFirstName,sDealerNumber,sProductCode,sVIN) Values ('Ln1','Fn1','TWS00967','GAP','1HGCR2F5XHA151050')
Insert into Contract_Header (sCustLastName,sCustFirstName,sDealerNumber,sProductCode,sVIN) Values ('Ln2','Fn2','TWS00968','GAP','WVWEV7AJXAW197040')
Insert into Contract_Header (sCustLastName,sCustFirstName,sDealerNumber,sProductCode,sVIN) Values ('Ln2','Fn2','TWS00968','GAP','WVWEV7AJXAW197040')
Insert into Contract_Header (sCustLastName,sCustFirstName,sDealerNumber,sProductCode,sVIN) Values ('Ln3','Fn3','TWS00967','GAP','2HGFC2F56GH536572')
Insert into Contract_Header (sCustLastName,sCustFirstName,sDealerNumber,sProductCode,sVIN) Values ('Ln3','Fn3','TWS00967','GAP','2HGFC2F56GH536572')
Insert into Contract_Header (sCustLastName,sCustFirstName,sDealerNumber,sProductCode,sVIN) Values ('Ln4','Fn4','TWS00967','PPM','19XFC2F76JE003513')
Insert into Contract_Header (sCustLastName,sCustFirstName,sDealerNumber,sProductCode,sVIN) Values ('Ln4','Fn4','TWS00967','PPM','19XFC2F76JE003513')
Insert into Contract_Header (sCustLastName,sCustFirstName,sDealerNumber,sProductCode,sVIN) Values ('Ln4','Fn4','TWS00967','PPM','19XFC2F76JE003513')

抱歉,我不知道如何让文字更具可读性......

非常感谢,

玛丽

1 个答案:

答案 0 :(得分:1)

您正在寻找row_number()

select t.*
       row_number() over (partition by CustLastname, CustFirstName, DealerNumber, ProductCode, Vin order by vin) as seqnum
from t;