将表中的行划分为4个或更多特定行的组?

时间:2014-10-31 19:24:52

标签: sql sql-server tsql sql-server-2000

我有两张桌子:

其中包含我所在地区客户的所有信息,名为[CFT1409](约有100万行);另一个拥有公司债务的所有客户,名为[Bcodebt](它有大约20万行)。

使用此查询:

  

选择c.LocationCode,c.CustomerId,c.CustomerCode,c.Status,d.DebtDate来自CFT1409作为c   Left Join Bcodebt d On Convert(int,c.CustomerId)= Convert(int,d.CustomerId)

     

按c.CustomerCode排序

LocationCode CustomerId CustomerCode             Status DebtDate 
------------ ---------- ------------------------ ------ -------- 
921          08086082   011100280142015500000001 A      201409
921          08086084   011100280142015500000002 I      201409
921          01646607   011100280142016000000000 A      NULL
921          01646608   011100280142017000000000 A      NULL
921          01646609   011100280142017500000000 I      201212
921          01646609   011100280142017500000000 I      201301
921          01646609   011100280142017500000000 I      201303
921          01646609   011100280142017500000000 I      201304
921          01646609   011100280142017500000000 I      201302
921          01646610   011100280142018200000000 A      NULL
921          01646611   011100280142018500000001 A      201409
921          06226345   011100280142018500000002 A      201409
921          01646612   011100280142019500000000 A      201409
921          01646612   011100280142019500000000 A      201408

我需要做的是:

  • 将结果分为4个或更多顺序组(按CustomerCode排序)无效(状态='我')客户。

  • 添加一个新列,确定这些组的哪些客户有债务(我使用[DebtDate]列来帮助这一点,因此“没有债务的人”是

所以结果应该是这样的(只有4个或更多顺序无效(' I')在组中,其他可以忽略):

LocationCode CustomerId CustomerCode             Status DebtStatus DebtGroup
------------ ---------- ------------------------ ------ ---------- ---------
921          08086082   011100280142015500000001 A      1                   
921          08086084   011100280142015500000002 I      1                   
921          01646607   011100280142016000000000 A      0                   
921          01646608   011100280142017000000000 A      0                   
921          01646609   011100280142017500000000 I      1          1        
921          01646609   011100280142017500000000 I      1          1        
921          01646609   011100280142017500000000 I      1          1        
921          01646609   011100280142017500000000 I      1          1        
921          01646609   011100280142017500000000 I      1          1        
921          01646610   011100280142018200000000 A      0                   
921          01646611   011100280142018500000001 A      1                   
921          06226345   011100280142018500000002 I      1          2        
921          01646612   011100280142019500000000 I      1          2        
921          01646612   011100280142019500000000 I      1          2        
921          01646613   011100280142019500000001 I      0          2        
921          01646614   011100280142019500000002 A      0                   

有没有办法在SQL Server 2000上执行此操作?

0 个答案:

没有答案