2010-09-17 61 views
0
table1   table2 

col1 date  col1 date 
a d1  e  d4 
b d2  f  d5 
c d3  a  d6 

我想這有4order by dateif any entries of column 1 replicate it remove this duplication also. 新表想,如果我的日期順序d1>d6>d2>d3>d5>d4那麼結果應該是:如何解決這個特定的查詢?

col1 date 
a  d1 
b  d2 
c  d3 
e  d5 

而且我也想知道哪些數據是從tabl1未來或表2。

+0

我不明白這一點! – 2010-09-17 15:22:03

+0

爲什麼'f'排除?但是'e'不被排除? – 2010-09-17 15:24:23

+0

@邁克爾,如果它是「前4名」,那麼沒關係。等待!哦,不!它必須是'f'而不是'e'! – 2010-09-17 15:26:15

回答

1

試試這個:

select 
top 4 --you only need 4 of them? 
col1, min(date) [date] from 
(
select col1, date from table1 
union 
select col1, date from table2 
) t 
group by col1 
order by col1 
+0

,我也想知道哪些數據來自哪個表。有可能嗎? – user12345 2010-09-17 15:36:25

+0

順便說一句,我們也可以使用限制只有4個條目。 – user12345 2010-09-17 15:37:25

0
select 
     col1, 
     min(date) date 
    from 
     (select col1, date 
      from table1 
     union all 
     select col1, date 
      from table2) 
    limit 4 
    group by 
     1 
    order by 
     2 

從數據樣本中,我認爲你想要5個條目..你錯過了你的數據元素「f」。

+0

no ..我只想要四個數據。 – user12345 2010-09-17 15:18:44