SQL Server row_number()按查询分区

时间:2017-10-01 20:38:49

标签: sql-server sql-server-2008 tsql window-functions

我的数据采用以下格式,尝试使用row_number()和使用row_number()进行独特的customerIDCount和分区,但是有下面的查询问题,是否有人可以帮忙?

with cte as 
(
    select 
        *,
        accountCategoryCount = row_number() over (partition by phoneNUmber, ID, accountCategory Order by phoneNumber) 
    from 
        (select distinct * from myTable) base
),
cet2 as 
(
    select 
        *,
        customerIDCount = row_number() over (partition by phoneNumber, ID order by phoneNumber 
    from 
        cte A
)
select * from cte2

MyTable数据:

phoneNumber ID   name  dob      accountCategory accountNumber balance
123456789   456  sam   10061970 checking        4567          10
123456789   456  sam   10061970 checking        4568          200
123456789   456  sam   10061970 Savings         4569          12
123456789   456  sam   10061970 Savings         4570          13
123456789   789  John  10101970 CreditCard      4571          400
123456789   789  John  10101970 CreditCard      4572          600
123456789   789  John  10101970 Savings         4573          50

预期结果

phoneNumber ID   name  dob      accountCategory accountNumber balance accountCategoryCount customerIDCount 
123456789   456  sam   10061970 checking        4567          10      1                    1
123456789   456  sam   10061970 checking        4568          200     2                    1
123456789   456  sam   10061970 Savings         4569          12      1                    1
123456789   456  sam   10061970 Savings         4570          13      2                    1
123456789   789  John  10101970 CreditCard      4571          400     1                    2
123456789   789  John  10101970 CreditCard      4572          600     2                    2
123456789   789  John  10101970 Savings         4573          50      1                    2

1 个答案:

答案 0 :(得分:4)

问题是ROW_NUMBER()总是返回不同的数字,并且您希望获得相同数字的相同数字" phoneNumber"和#34; ID",为此您需要使用DENSE_RANK()返回相同的tie值:

with cte as (
    select *,
        row_number() over (partition by phoneNumber, ID, accountCategory Order by phoneNumber) as accountCategoryCount,
        dense_rank() over (order by phoneNumber, ID) as customerIDCount
    from 
        (select distinct * from myTable) base
)
select * from cte

结果:

| phoneNumber |  ID | accountCategory | ... | accountCategoryCount | customerIDCount |
|-------------|-----|-----------------|-----|----------------------|-----------------|
|   123456789 | 456 |        checking |     |                    1 |               1 |
|   123456789 | 456 |        checking |     |                    2 |               1 |
|   123456789 | 456 |         Savings |     |                    1 |               1 |
|   123456789 | 456 |         Savings |     |                    2 |               1 |
|   123456789 | 789 |      CreditCard |     |                    1 |               2 |
|   123456789 | 789 |      CreditCard |     |                    2 |               2 |
|   123456789 | 789 |         Savings |     |                    1 |               2 |