如何过滤群组?

时间:2016-08-12 15:31:55

标签: sql-server database

我有一张这样的表:

Address                    AccountName        AccountId
-------------------------------------------------------
10007 Cougar Country       Smith              107
90026 Hunters Pond         Scott              106
10008 Indigo Run           Mary               108
70023 Kopplin Road         John               102
70023 Kopplin Road         John               103
70023 Kopplin Road         Peter              104
70023 Kopplin Road         Steve              105
70018 Oaks Drive           Joe                100
70018 Oaks Drive           Lisa               101

这是实际连接两个表的结果 使用OrderBy地址。表有记录在哪里 地址和/或AccountName列 可以在多行中具有相同的值, 而AccountId列总是不同。

如何获取记录组的位置: a)地址相同,AccountName不同 b)地址相同,AccountName相同 此外,组中的记录数> 1。 我需要桌子上的所有字段。

以下是我需要的输出:

a)地址相同且AccountName不同:

Address                    AccountName         AccountId
-------------------------------------------------------
70023 Kopplin Road         Peter              104
70023 Kopplin Road         Steve              105
70018 Oaks Drive           Joe                100
70018 Oaks Drive           Lisa               101

b)地址相同且AccountName相同:

Address                    AccountName         AccountId
-------------------------------------------------------
70023 Kopplin Road         John               102
70023 Kopplin Road         John               103

非常感谢

2 个答案:

答案 0 :(得分:0)

仍然不完全清楚你想要什么,但这应该让你接近。

with SortedResults as
(
    select Address
        , AccountName
        , AccountId
        , ROW_NUMBER() over (partition by Address, AccountName order by AccountId) as RowNum
    from SomeTable
)
select *
from SortedResults
where RowNum > 1

答案 1 :(得分:0)

Declare @YourTable table (Address varchar(150),AccountName varchar(50),AccountID varchar(50))
Insert Into @YourTable values
('10007 Cougar Country','Smith','107'),
('90026 Hunters Pond'  ,'Scott','106'),
('10008 Indigo Run'    ,'Mary' ,'108'),
('70023 Kopplin Road'  ,'John' ,'102'),
('70023 Kopplin Road'  ,'John' ,'103'),
('70023 Kopplin Road'  ,'Peter','104'),
('70023 Kopplin Road'  ,'Steve','105'),
('70018 Oaks Drive'    ,'Joe'  ,'100'),
('70018 Oaks Drive'    ,'Lisa' ,'101')

;with cteBase as (Select *,RowNr=Row_Number() over (Partition By Address,AccountName Order by AccountID) from @YourTable)
     ,cteLvl1 as (Select Address,GrpLvl1=IIF(count(*)>1,1,0) From cteBase Group by Address)
     ,cteLvl2 as (Select Address,AccountName,GrpLvl2=IIF(max(RowNr)>1,1,0) From cteBase Group by Address,AccountName)
Select Class=IIF(GrpLvl1+GrpLvl2=0,'None',IIF(GrpLvl1+GrpLvl2=1,'Group','Sub-Group'))
      ,A.Address
      ,A.AccountName
      ,A.AccountID
 From cteBase A
 Join cteLvl1 B on (A.Address=B.Address)
 Join cteLvl2 C on (A.Address=B.Address and A.AccountName=C.AccountName)
 Where GrpLvl1+GrpLvl2>=1
 Order By GrpLvl1+GrpLvl2,2,3,4

返回

Class       Address             AccountName AccountID
Group       70018 Oaks Drive    Joe         100
Group       70018 Oaks Drive    Lisa        101
Group       70023 Kopplin Road  Peter       104
Group       70023 Kopplin Road  Steve       105

Sub-Group   70023 Kopplin Road  John        102
Sub-Group   70023 Kopplin Road  John        103