Quantcast
Channel: Teradata Forums - Database
Viewing all articles
Browse latest Browse all 14773

union and filter - response (1) by dnoeth

$
0
0

A direct translation of should be:

select item
 , location
 , date
 , quantity
from
 (
   select 1 as x, item
    , location
    , date
    , quantity
   from t1
   union
   select 2 as x, item
    , location
    , date
    , quantity
   from t2
 ) as dt
qualify 
   row_number()
   over (partition by item, location, date order by x) = 1 

But changing it to a Full Outer Join might be more efficient, this should return the same result:

select coalesce(t1.item, t2.item),
 , coalesce(t1.location, t2.location
 , coalesce(t1.date, t2.date
 , coalesce(t1.quantity, t2.quantity)
from
 (
   select item
    , location
    , date
    , quantity
   from t1
 ) as t1
full outer join
 (
   select item
    , location
    , date
    , quantity
   from t2
 ) as t2
on t1.item = t2.item
and t1.location = t2.location
and t1.date = t2.date

Dieter


Viewing all articles
Browse latest Browse all 14773

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>