简体   繁体   中英

Netezza SQL comparing two records in the same table

I am trying to do a basic insert from one table (#1) into another (#2), however I created a flag column in table #2 which in order to correctly set, I need to be able to determine the difference between 2 records in the source table (#1). This second table is sort of a verisoning table. It indicates there are multiple records of something from table #1, but that they are different versions (different version being that only 1 or two supplementary fields in table #1 are different) and table#2 will show what was different between the two/three versions.

in table #2 I will have a "Why was this added" flag which will either be "P", "B", or "J" depending on the source records in table #1.

If the difference between the two records in table #1 were in column date, then the flag will be "P". If the difference between the two records was in column amount, then the flag will be "B". If the difference was both date and amount, then the flag will be set to "J".

So how would I go about during my insert statement comparing two records' fields from the same table??

Any advice would be appreciated. I believe in Netezza SQL I cannot create virtual tables which is why google hasn't served me well.

select t1.id, 'j'
from table t1 
join table t2 
  on t2.id  t1.id 
 and t2.date <> t1.date 
 and t2.amount <> t2.amount;
select t1.id, 'p'
from table t1 
join table t2 
  on t2.id == t1.id 
 and t2.date <> t1.date 
 and t2.amount = t2.amount;
select t1.id, 'b'
from table t1 
join table t2 
  on t2.id = t1.id 
 and t2.date = t1.date 
 and t2.amount <> t2.amount;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM