SQL:根据源优先级选择记录

时间:2016-12-30 02:06:17

标签: sql sql-server

假设有两个相同数据的来源。根据源优先级选择记录的最佳方法是什么? e.g。

Date   Value    Source
D1      V1        S1
D1      V2        S2
D2      V3        S1
D3      V4        S2

假设S1优于S2,查询应该给出:

D1 V1 S1
D2 V3 S1
D3 V4 S2

目标数据库是MS SQL Server。谢谢。

6 个答案:

答案 0 :(得分:0)

一种方法是从S1中选择所有内容,然后只选择S2中不匹配的记录:

select t.*
from t
where t.source = 'S1' or
      not exists (select 1 from t t2 where t2.source = 'S2');

答案 1 :(得分:0)

SELECT *
FROM 
 ( SELECT *
         ,ROW_NUMBER() OVER (PARTITION BY [Date] ORDER BY [Source] ASC) rn
   FROM TableName 
 )a
WHERE Rn = 1

考虑到S1优于S2,S2优于S3等等......

使用ROW_NUMBER()功能,您可以获得每个日期值的最佳可用来源。

答案 2 :(得分:0)

如果您的DBMS支持窗口正常运行,您可以找到每个日期的最佳优先级,如下所示:

SELECT DATE,
  value,
  source
FROM
  (SELECT t.*,
    row_number() over (partition BY DATE order by source) rn
  FROM TABLE t
  ) t
WHERE rn = 1;

如果没有,并且日期和源列的组合是唯一的,您可以使用:

SELECT t1.*
FROM TABLE t1
INNER JOIN
  ( SELECT DATE, MIN(source) source FROM TABLE GROUP BY DATE
  ) t2
ON t1.date    = t2.date
AND t1.source = t2.source;

答案 3 :(得分:0)

select * from t t1
where not exists (
    select 1 from t t2 
    where t2.Source > t1.Source 
          and t2.Value = t1.Value 
          and t2.Date = t1.Date
)

如果Source的优先级逻辑更复杂,您可能需要创建自己的自定义比较函数,在这种情况下查询可能如下所示:

select * from t t1
    where not exists (
        select 1 from t t2 
        where customFunc(t2.Source) > customFunc(t1.Source) 
              and t2.Value = t1.Value 
              and t2.Date = t1.Date
    )

答案 4 :(得分:0)

我可以通过使用CASE表达式来解决这个问题,该表达式的顺序反映了来源的重要性顺序:

SELECT t1.*
FROM yourTable t1
INNER JOIN
(
    SELECT Date,
           MIN(CASE WHEN Source = 'S1' THEN 1
                    WHEN Source = 'S2' THEN 2 END) AS SourceRank
    FROM yourTable
    GROUP BY Date
) t2
    ON t1.Date = t2.Date AND
       t2.SourceRank = 1

使用CASE表达式的好处是允许任意对两个或更多源的排序,而不假设重要性顺序可能是什么。

答案 5 :(得分:0)

假设有一个单独的源优先级表

create table #sp(
    Source varchar(5) primary key,
    Priority int
    );

insert #sp(Source,Priority)
values
('S1',2),
('S2',1);

可以这样使用

select top(1) with ties t.*
from myTable t
join #sp p on t.Source = p.Source
order by row_number() over (partition by t.Date order by p.priority);