'How do I view all records with data source as 1 if not 1 then use data source 2 but no duplicates
I have a table that has the below columns
sk_item_id item_code item_desc sk_data_source
1 054486900 TRIM lh hotplate Whi 1
2 054486900 TRIM lh hotplate Whi 2
3 081423698 ARC3084 DG FRIT 2
4 081744900 HANDBOOK<600G MK2>44PGX500 1
I want to display the records that are contained within data source 2 but if the record does not sit in data source 2 then display the record that is in data source 1 but I do not want any duplicates
so basically display all data from data source 2 if the item code is not in data source 2 then display the record
Solution 1:[1]
If using Postgresql, do DISTINCT ON:
select distinct on (item_code) sk_item_id, item_code, item_desc, sk_data_source
from table_name
order by item_code, sk_data_source desc;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jarlh |
