优先处理来自特定数据源的数据
Give priority to data from Specific Datasource
我有如下数据
ID Name Description DataSource Year
1 Apple Sweet & Tasty Source_A 2016
1 Apple Red and Sweet & Tasty Source_B 2015
2 Apple Delicious Source_A 2016
2 Apple Delicious and Red Source_C 2015
3 Apple Source_C 2013
3 Apple Green and Large Source_B 2016
就我而言,我想优先考虑source_B,因为它更可靠。因此,如果有来自 Soure_B 的数据,我想为特定 ID 显示该行并忽略其他行。如果 source_B 的数据不存在,那么我只想显示其他来源的数据。另外,我想只显示一行最新数据。
在上面的例子中,结果应该是这样的
ID Name Description DataSource Year
1 Apple Red and Sweet & Tasty Source_B 2015
2 Apple Delicious Source_A 2016
3 Apple Green and Large Source_B 2016
您可以使用 row_number + 大小写来确定优先级,如下所示:
select
id,
name,
description,
datasource,
year
from (
select
id,
name,
description,
datasource,
year,
row_number () over (
partition by ID
order by case when DataSource = 'Source_B' then 1 else 2 end,
Year desc
) as RN
from
table1
) X
where
RN = 1
分区依据将为每个 ID 选择新的 ID,并按 selects 排序,行将获得编号。外部 select 然后过滤掉除编号为 1 的行之外的其他行。
您可以在 SQL Fiddle
中尝试
使用 row_number()
自定义 order by
:
DECLARE @table TABLE(ID INT ,Name VARCHAR(30),Description VARCHAR(30),DataSource VARCHAR(30),YEAR VARCHAR(30))
INSERT INTO @table(ID,Name,Description,DataSource,YEAR) VALUES
(1,'Apple','Sweet & Tasty','Source_A',2016),
(1,'Apple','Red and Sweet & Tasty','Source_B',2015),
(2,'Apple','Delicious','Source_A',2016),
(2,'Apple','Delicious and Red','Source_C',2015),
(3,'Apple',NULL,'Source_C',2013),
(3,'Apple','Green and Large','Source_B',2016)
SELECT * FROM
(
SELECT *,ROW_NUMBER() OVER(PARTITION BY ID ORDER BY CASE WHEN DataSource = 'Source_B' THEN 0 ELSE 1 END) rn FROM @table
) t
WHERE rn = 1
我有如下数据
ID Name Description DataSource Year
1 Apple Sweet & Tasty Source_A 2016
1 Apple Red and Sweet & Tasty Source_B 2015
2 Apple Delicious Source_A 2016
2 Apple Delicious and Red Source_C 2015
3 Apple Source_C 2013
3 Apple Green and Large Source_B 2016
就我而言,我想优先考虑source_B,因为它更可靠。因此,如果有来自 Soure_B 的数据,我想为特定 ID 显示该行并忽略其他行。如果 source_B 的数据不存在,那么我只想显示其他来源的数据。另外,我想只显示一行最新数据。
在上面的例子中,结果应该是这样的
ID Name Description DataSource Year
1 Apple Red and Sweet & Tasty Source_B 2015
2 Apple Delicious Source_A 2016
3 Apple Green and Large Source_B 2016
您可以使用 row_number + 大小写来确定优先级,如下所示:
select
id,
name,
description,
datasource,
year
from (
select
id,
name,
description,
datasource,
year,
row_number () over (
partition by ID
order by case when DataSource = 'Source_B' then 1 else 2 end,
Year desc
) as RN
from
table1
) X
where
RN = 1
分区依据将为每个 ID 选择新的 ID,并按 selects 排序,行将获得编号。外部 select 然后过滤掉除编号为 1 的行之外的其他行。
您可以在 SQL Fiddle
中尝试使用 row_number()
自定义 order by
:
DECLARE @table TABLE(ID INT ,Name VARCHAR(30),Description VARCHAR(30),DataSource VARCHAR(30),YEAR VARCHAR(30))
INSERT INTO @table(ID,Name,Description,DataSource,YEAR) VALUES
(1,'Apple','Sweet & Tasty','Source_A',2016),
(1,'Apple','Red and Sweet & Tasty','Source_B',2015),
(2,'Apple','Delicious','Source_A',2016),
(2,'Apple','Delicious and Red','Source_C',2015),
(3,'Apple',NULL,'Source_C',2013),
(3,'Apple','Green and Large','Source_B',2016)
SELECT * FROM
(
SELECT *,ROW_NUMBER() OVER(PARTITION BY ID ORDER BY CASE WHEN DataSource = 'Source_B' THEN 0 ELSE 1 END) rn FROM @table
) t
WHERE rn = 1