Give priority to data from Specific Datasource

独自空忆成欢 提交于 2019-12-12 06:20:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!