Col1 Col2 Col3 ABC Product Digital ABC SubProduct XF BCD Product Non-Digital BCD SubProduct <White Space> CDE Product Digital CDE SubProduct Null DEF Product Non-Digital DEF SubProduct FR
Desired Output:
Col1 Product SubProduct ABC Digital XF DEF Non-Digital FR
Query:
SELECT * FROM TABLE WHERE (Col2 IN ('Product','SubProduct') AND NOT Col3 LIKE '')
Any help is greatly appreciated
Advertisement
Answer
Try this:
WITH TAB (Col1, Col2, Col3) AS ( VALUES ('ABC', 'Product', 'Digital') , ('ABC', 'SubProduct', 'XF') , ('BCD', 'Product', 'Non-Digital') , ('BCD', 'SubProduct', ' ') , ('CDE', 'Product', 'Digital') , ('CDE', 'SubProduct', Null) , ('DEF', 'Product', 'Non-Digital') , ('DEF', 'SubProduct', 'FR') ) SELECT A.Col1, A.Col3 AS Product, B.Col3 AS SubProduct FROM TAB A JOIN TAB B ON B.Col1 = A.Col1 AND B.Col2 = 'SubProduct' WHERE A.Col2 = 'Product' AND NULLIF(B.Col3, '') IS NOT NULL; |COL1|PRODUCT |SUBPRODUCT | |----|-----------|-----------| |ABC |Digital |XF | |DEF |Non-Digital|FR |