plsql - Obtaining a value from 2 different case statements -


i have 2 tables. first, sales table:

 salesid      amount     productid      monthid    20           10         1             201307    15           25         1             201301    40           20         5             201303 

note: more 1000 rows.

second, have product table:

 productid     description       product_elemid      1            aaa                  24     2            bbb                  57                5            ccc                  23     1            aaa_ace              25 

note: 100 rows.

now, want display data these 2 tables. have:

select p.description          case ( when s.monthid between 201301 , 201307 sum(s.amount) else 0 end)         case ( when s.monthid between 201303 , 201305 sum(s.amount) else 0 end)  sales s, product p s.productid = p.productid  , p.productid in ('1, '2', '5') group p.description 

i table 3 columns:

p.description         case 1 values             case 2 values 

so far good. want have column gives me difference of values 2 cases.

can write in case statement, or there better way it?

note : performing self join here not desirable me, has lot of rows , take long display.

thanks.

you make original statement subquery:

select description, case_1, case_2, case_1 - case_2  (select p.description description     case ( when s.monthid between 201301 , 201307 sum(s.amount) else 0 end) case_1    case ( when s.monthid between 201303 , 201305 sum(s.amount) else 0 end) case_2  sales s, product p  s.productid = p.productid   , p.productid in ('1, '2', '5')  group p.description ) 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -