2016-12-28 43 views
0

我有一個表table1加入2個表使用某些情況下

pid_x | pid_y | count 
-------|-------|------- 
a  | b  | 5 
a  | c  | 10 
b  | a  | 20 
c  | e  | 3 
d  | g  | 7 
e  | f  | 10 
e  | b  | 20 

等(超過100萬行)

帶來多列的也對我有master_table 其中包括細節

master_id | pid | rating_pid | price_pid 
-----------|-----|------------|----------- 
a   | a1 |   2 | 10 
a   | b1 |   4 | 20 
a   | c1 |   1 | 30 
b   | d1 |  3.5 | 40 
b   | e1 |  2.4 | 50 
c   | f1 |  1.5 | 60 
d   | g1 |  3.8 | 70 

(依此類推,巨大的表master_table

您可以在表1,pid_x和pid_y看看有哪些實際master_id(從master_table)的一些價值觀,我想換成最便宜的「PID」的master_id在pid_x和pid_y是master_id

我在尋找什麼爲是,一個final_table ,其具有列

pid_x pid_y count price_pid_y rating_pid_y price_diff_pidy/x

闡釋。

創建具有列的pid_xpid_y與master_table最廉價的PID替換的master_id值,然後加入pid_y的price and rating從pid_y的master_table'spid也價格差異率相對於pid_x之後的最終桌。

final_table應該是這個樣子

pid_x | pid_y | count | price_pid_y | rating_pid_y | price_diff_pid_y/x 
-------|-------|-------|-------------|--------------|-------------------- 
a1 | d1 |  5 |   40 |   3.5 |    400 
a1 | f1 | 10 |   60 |   1.5 |    600 
d1 | a1 | 20 |   10 |   2 |    25 

我有一個局部的解決方案,它給我造成的,

我只是想添加這個條件「,只要在表1 pid_x和pid_y具有來自值master_id將其替換爲master_table中最便宜的pid「。

SELECT 
    t1.pid_x 
    ,t1.pid_y 
    ,t1.count 
    ,m1.price_pid AS price_pid_y 
    ,m1.rating_pid AS rating_pid_y 
    ,100 * m1.price_pid/m2.price_pid AS price_diff_pid_y_x 
FROM table1 t1 
    INNER JOIN master_table m1 ON t1.pid_y = m1.pid 
    INNER JOIN master_table m2 ON t1.pid_x = m2.pid 
ORDER BY 
    t1.pid_x 
    ,t1.pid_y 

給予像

INNER JOIN master_table m1 
ON CASE WHEN A.pid_x = B.master_id 

一些情況下則更換最廉價的PID多爾說pid_x是mater_id ......`

+0

這個問題是很難理解。你有表格,但是你沒有打算將列名與數據值對齊。這意味着我們必須猜測你的意思。 – Hogan

+0

您必須澄清三件事讓我們知道您的意思: 首先,table1.pid_x和table1.pid_y中的值範圍與master_table.master_id中的值範圍相同? 秒,你是否想用master_table.pid的值替換pid_x,其中pid_x等於master_table.master_id,那麼劑量pid_y? 第三,你最便宜的'pid'的規則是什麼,我們如何判斷哪一個最便宜? –

回答

0

好吧,我想我明白,所以我打算這一步一步,讓你可以學習如何做到這一點,並修復我不明白的地方。

首先你想要「最便宜的價格」,當你在主表上看東西。讓我們做一個查詢來獲取該表。

首先,我們得到的唯一標識該行:

SELECT master_id, min(price_pid) as price_pid 
FROM master_table 
GROUP BY master_id 

然後我們就可以選擇這個新表只有一個1,它是最便宜的:

SELECT ma.* 
FROM master_table ma 
JOIN (
    SELECT master_id, min(price_pid) as price_pid 
    FROM master_table 
    GROUP BY master_id 
) as mi_ma on ma.master_id = mi_ma.master_id and ma.price_pid = mi_ma.price_pid 

現在我創建這個作爲一個這樣的看法:

CREATE OR REPLACE VIEW min_master_table AS 
    SELECT ma.* 
    FROM master_table ma 
    JOIN (
    SELECT master_id, min(price_pid) as price_pid 
    FROM master_table 
    GROUP BY master_id 
) as mi_ma on ma.master_id = mi_ma.master_id and ma.price_pid = mi_ma.price_pid 

有了這個新的看法你的查詢是非常接近,在這裏它是固定的:

SELECT 
    m1.pid as pid_x, 
    m2.pic as pid_y, 
    t1.count, 
    m2.price_pid AS price_pid_y, 
    m2.rating_pid AS rating_pid_y, 
    100 * m2.price_pid/m1.price_pid AS price_diff_pid_y_x 
FROM table1 t1 
JOIN min_master_table m1 ON t1.pid_x = m1.pid 
JOIN min_master_table m2 ON t1.pid_y = m2.pid 

注意:如果你不能使視圖這也將工作,但它是一個很多更容易,因爲重複的代碼錯誤:

SELECT 
    m1.pid as pid_x, 
    m2.pic as pid_y, 
    t1.count, 
    m2.price_pid AS price_pid_y, 
    m2.rating_pid AS rating_pid_y, 
    100 * m2.price_pid/m1.price_pid AS price_diff_pid_y_x 
FROM table1 t1 
JOIN (
    SELECT ma.* 
    FROM master_table ma 
    JOIN (
    SELECT master_id, min(price_pid) as price_pid 
    FROM master_table 
    GROUP BY master_id 
) as mi_ma on ma.master_id = mi_ma.master_id and ma.price_pid = mi_ma.price_pid 
) m1 ON t1.pid_x = m1.pid 
JOIN (
    SELECT ma.* 
    FROM master_table ma 
    JOIN (
    SELECT master_id, min(price_pid) as price_pid 
    FROM master_table 
    GROUP BY master_id 
) as mi_ma on ma.master_id = mi_ma.master_id and ma.price_pid = mi_ma.price_pid 
) m2 ON t1.pid_y = m2.pid