我有以下查询
select count(trade_sid), shortcode
from trade
where
trade.trade_date <= sysdate and
trade.trade_date>= add_months(sysdate, -11)
group by shortcode
UNION ALL
select count(trade_sid), shortcode
from trade_archive
where
trade_archive.trade_date <= sysdate and
trade_archive.trade_date>= add_months(sysdate, -11)
group by shortcode
order by shortcode
这会导致像这样的重复输出
23 abc
24 abc
56 def
87 def
这是因为使用了联合运算符,不知道如何更改此查询以便我得到
47 abc
143 def
作为输出
我最终在 java 中执行此查询的用户没有足够的权限来创建临时表,还有其他方法可以解决这个问题吗?
最佳答案
像这样的东西:
select count(trade_sid), shortcode
from
(
select trade_sid, shortcode
from trade
where
trade.trade_date <= sysdate and
trade.trade_date>= add_months(sysdate, -11)
UNION ALL
select trade_sid, shortcode
from trade_archive
where
trade_archive.trade_date <= sysdate and
trade_archive.trade_date>= add_months(sysdate, -11)
) tt
group by shortcode
order by shortcode
关于sql union 返回重复结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6273928/