如何修改此select语句

时间:2014-03-31 03:48:07

标签: sql select

如何修改下面的union select语句到任何连接select语句方法,它来自表1和表2的总和价格,我也尝试了,

select CODE, SUM(PRICE) as PRICE 
from (SELECT CODE, SUM(PRICE) as PRICE 
       FROM TABLE1 
       where ID='IBK' and cast(TMSTAMP_UPDATE as DATE)='2014-03-31' 
       GROUP BY CODE 
      union 
      SELECT CODE, SUM(PRICE) as PRICE 
       FROM TABLE2 
       where ID='IBK' and cast(TMSTAMP_UPDATE as DATE)='2014-03-31' 
       GROUP BY CODE) GROUP BY CODE;

提前致谢

1 个答案:

答案 0 :(得分:0)

尝试这样

select CODE, SUM(PRICE) as PRICE 
from (SELECT CODE, SUM(PRICE) as PRICE 
       FROM TABLE1 t1 INNER JOIN TABLE2 t2 ON
      t1.ID=t2.ID and 
      t1. cast(TMSTAMP_UPDATE as DATE)='2014-03-31' 
      = t2.cast(TMSTAMP_UPDATE as DATE)='2014-03-31' and
      t1.ID='IBK' and t1.cast(TMSTAMP_UPDATE as DATE)='2014-03-31'  
GROUP BY CODE) GROUP BY CODE;