问题
I have data as below
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
In my case, I would like to give priority to source_B because it is more reliable. So if there is a data from Soure_B, I would like to display that row for the specific ID and ignore others. If data from source_B is not present, only then I want to display data from other sources. Also, I would like to display just one row of most recent data.
In above example, result should look like
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
回答1:
You can do the priorities using row_number + case, like this:
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
The partition by will choose new ID for each ID, and order by selects in which order the rows will get the number. The outer select then filters out others than rows with number 1.
You can try this in SQL Fiddle
回答2:
Use row_number()
with customized 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
来源:https://stackoverflow.com/questions/31248960/give-priority-to-data-from-specific-datasource