Where子句,如果有多个相同的ID

时间:2016-12-14 13:55:21

标签: sql-server tsql

我有以下表格:

ID | source | Name | Age | ... | ...
1  | SQL    | John | 18  | ... | ...
2  | SAP    | Mike | 21  | ... | ...
2  | SQL    | Mike | 20  | ... | ...
3  | SAP    | Jill | 25  | ... | ...

我希望每个ID都有一条记录。这背后的想法是,如果ID只出现一次(无论来源),那么该记录将被采用。但是,如果一个ID有2条记录,那么包含SQL作为源的记录将是此处使用的记录。

所以,在这种情况下,结果将是:

ID | source | Name | Age | ... | ...
1  | SQL    | John | 18  | ... | ...
2  | SQL    | Mike | 20  | ... | ...
3  | SAP    | Jill | 25  | ... | ...

我使用了一个分区(由Source desc命令),但是如果有一天会添加第三个源,那么这将无法正常工作。

还有其他选择/想法吗?

4 个答案:

答案 0 :(得分:4)

您可以使用ROW_NUMBER

WITH CTE AS
(
    SELECT  *,
            RN = ROW_NUMBER() OVER( PARTITION BY ID
                                    ORDER BY CASE WHEN [Source] = 'SQL' THEN 1 ELSE 2 END)
    FROM dbo.YourTable
)
SELECT *
FROM CTE
WHERE RN = 1;

答案 1 :(得分:4)

您可以使用WITH TIES子句和窗口函数Row_Number()

Select Top 1 With Ties *
  From  YourTable
  Order By Row_Number() over (Partition By ID Order By Case When Source = 'SQL' Then 0 Else 1 End)

答案 2 :(得分:4)

最简单的方法(在我看来)是使用具有排名功能的CTE:

with cte as
(
   select ID, source, Name, Age, ... , 
          rn = row_number() over (partition by ID order by case when source = 'sql'
                                                           then 0 else 1 end asc)
   from dbo.tablename
)
select ID, source, Name, Age, ...
from cte
where rn = 1

答案 3 :(得分:3)

怎么样

SELECT * 
FROM table 
WHERE ID in (
   SELECT ID FROM test
   group by ID
   having count(ID) = 1)
OR source = 'SQL'