2010-01-15 117 views
2

檢索數據我有兩個表:創建一個SQL查詢從兩個表中

T_STOCK:主鍵是idseller,和其他一些領域,讓說abT_FLOW:主鍵是(id + startdate)以及其他一些字段,例如cd

我想返回從T_STOCK關於特定seller每個記錄的所有列的查詢,但在T_FLOW表中的列(startDatecd)完成。

T_STOCKT_FLOW之間的關係基於id屬性。 每當T_STOCK中存在具有特定ID的記錄時,此ID至少有一條記錄存在於T_FLOW中。

然而,可能發生不止一個紀錄T_FLOW存在。在這種情況下,我只能考慮最近的(即max(startDate))。

在別人的話,如果我們有以下的表格內容:

+---------------------+ 
|  T_STOCK  | 
+----+--------+---+---+ 
| ID | SELLER | a | b | 
+----+--------+---+---+ 
| 01 | foobar | 1 | 2 | 
+----+--------+---+---+ 
| 02 | foobar | 3 | 4 | 
+----+--------+---+---+ 
| 03 | foobar | 5 | 6 | 
+----+--------+---+---+ 

+---------------------------+ 
|   T_FLOW   | 
+----+------------+----+----+ 
| ID | StartDate | c | d | 
+----+------------+----+----+ 
| 01 | 01/01/2010 | 7 | 8 | 
+----+------------+----+----+ 
| 02 | 01/01/2010 | 9 | 10 | 
+----+------------+----+----+ 
| 02 | 07/01/2010 | 11 | 12 | 
+----+------------+----+----+ 
| 03 | 03/01/2010 | 13 | 14 | 
+----+------------+----+----+ 
| 03 | 05/01/2010 | 15 | 16 | 
+----+------------+----+----+ 

查詢的結果一定是:

+----+--------+---+---+------------+----+----+ 
| ID | SELLER | a | b | startDate | c | d | 
+----+--------+---+---+------------+----+----+ 
| 01 | foobar | 1 | 2 | 01/01/2010 | 7 | 8 | 
+----+--------+---+---+------------+----+----+ 
| 02 | foobar | 3 | 4 | 03/01/2010 | 11 | 12 | 
+----+--------+---+---+------------+----+----+ 
| 03 | foobar | 5 | 6 | 01/01/2010 | 15 | 16 | 
+----+--------+---+---+------------+----+----+ 

我怎樣寫我的查詢呢?

+0

你的關係似乎有點怪我。你的股票'PK'是'(id,seller)',你可以擁有兩個具有相同'id'的股票,但流量只與'id'相關。這是故意的嗎? – Quassnoi 2010-01-15 11:46:46

+1

+1爲您的ASCII藝術! – 2010-01-15 11:49:14

+0

@Quassnoi,是的,這是故意的:) – romaintaz 2010-01-15 12:38:47

回答

3
SELECT * 
FROM t_stock s 
JOIN (
     SELECT f.*, ROW_NUMBER() OVER (PARTITION BY id ORDER BY startDate DESC) AS rn 
     FROM t_flow f 
     ) f 
ON  f.id = s.id 
     AND f.rn = 1 

下面是不使用分析功能的解決方案:

SELECT * 
FROM t_stock s 
JOIN t_flow f 
ON  (f.id, f.startDate) = 
     (
     SELECT id, MAX(startDate) 
     FROM t_flow fi 
     WHERE fi.id = s.id 
     GROUP BY 
       id 
     ) 
+0

是否有任何解決方案來寫這個查詢,而無需使用'ROW_NUMBER()以上(分區...'? – romaintaz 2010-01-15 13:22:39

0
SELECT DISTINCT 
     s.* 
     ,FIRST_VALUE(f.startdate) 
     OVER (PARTITION BY f.id ORDER BY f.startdate DESC) startdate 
     ,FIRST_VALUE(f.c) 
     OVER (PARTITION BY f.id ORDER BY f.startdate DESC) c 
     ,FIRST_VALUE(f.d) 
     OVER (PARTITION BY f.id ORDER BY f.startdate DESC) d 
FROM t_stock s, t_flow f 
WHERE f.id = s.id 
1

您使用分析,as shown by Quassnoi,或使用獲得最新的T_FLOW記錄:

select id, max(startdate) last_start_date from t_flow group by id; 

然後你可以加入你的T_STOCK表格 - 例如:

select 
    s.*, 
    f.* 
from 
    t_stock s 
     inner join t_flow f on 
       f.id = s.id 
      and (f.id, f.startdate) in 
       (
       select 
        id, 
        max(startdate) laststartdate 
       from 
        t_flow 
       group by 
        id 
       ) 
+0

你需要一個更加入從't_flow'得到其他列。 – Quassnoi 2010-01-15 13:48:36

+0

我作爲mid-edit :) - 並且仍然不起作用 – 2010-01-15 13:51:39

+0

這應該是'(f.id,f.startdate)IN ...' – Quassnoi 2010-01-15 13:55:13

0
select id, max(startdate) last_start_date from t_flow group by id; 

然後你就可以像這樣與你T_STOCK表,什麼加入這個:

select s.*, f.* from t_stock s inner join t_flow f on f.id = s.id 
and (f.id, f.startdate) in (select id, max(startdate) laststartdate 
       from t_flow group by id)