2010年7月30日 星期五

多表取不重複值

兩章表...找出範圍內的所有值..並且不重複
● union all 重複
● union 不重複

group by 是決定重不重複的關鍵。


select
*
from (
select
ProductName, Money
from
TABLE_A
where
Time between '2009-08-04' and '2010-05-07'

union all

select
ProductName, Money
from
TABLE_B
where
Time between '2009-08-04' and '2010-05-07'
) as tmp
group by ProductName, Money

1 則留言:

  1. 要做到不重複用 DISTINCT 的效率會比較好
    union 本身就有不重複的指令了

    你確定要用 union,他的效率會很差,資料大的時候會很明顯
    建議你做一下壓力測試

    回覆刪除