2014-03-31 116 views
0

我怎麼能修改下面工會SELECT語句中的任意連接select語句的方法,其從表1和表2總結的價格,我想還有,怎樣才能修改此select語句

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; 

謝謝提前

+2

我不明白你的問題在所有。你可以嘗試改寫它嗎? –

+0

你能否解釋一下爲什麼一個連接會成爲一個問題,還有一個商業目的是從一個連接改變它 – skv

回答

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;