2015-03-03 113 views
1

我試圖從不同記錄中選擇不同的值,這些值可以根據特定字段的值具有相同或不同的ID。例如:從不同行中選擇具有相同編號的值的SQL查詢

ID, Color, Shape, Weight, Height, Price 
1 blue sq  10  12  5 
1 red sq  10  13  6 
2 blue sq  10  14  7 
3 blue sq  10  15  8 
3 red sq  10  16  9 

,並試圖得到下面的輸出:

ID, Color, Shape, Weight, Height, PriceBlue, PriceRed 
1 red sq  10  12  5   6 
1 blue sq  10  12  5   6 
2 blue sq  10  14  7   NULL 
3 blue sq  10  15  8   9 
3 red sq  10  15  8   9 

所以,當顏色是紅色的,它需要拿起藍色的,但所有其他領域的高度將來自紅記錄。但是,當id相同時,它也需要提取兩個記錄的價格。 當顏色是藍色時,當兩個記錄具有相同的ID時,它需要拾取所有藍色字段記錄以及藍色和紅色的價格。 如果記錄是唯一的(沒有其他記錄具有相同的ID),那麼它必須相應地拾取所有字段。在這種情況下,第二個價格字段將爲空。 我真的很感激任何幫助。我試圖想出一個查詢,但到目前爲止,我無法找到一種方法來創建藍色記錄的高度值,而不管顏色是什麼。

+1

您需要使用自聯接行用相同的ID和不同的顏色相匹配。請顯示您的嘗試解決方案,SO不是免費的編碼服務。 – Barmar 2015-03-03 00:39:55

+0

您將需要使用'FULL OUTER JOIN'來允許不匹配其他顏色的行。 – Barmar 2015-03-03 00:40:32

+0

我明白。我無法完成我的查詢,因爲我無法弄清楚當顏色爲紅色時如何選擇不同行的值。我可以使用out join等,但不知怎的,我需要弄清楚當顏色是紅色時如何拾取藍色的高度。這是一個不好的例子,但是我正在嘗試尋找解決方案。 – user1832895 2015-03-03 00:54:03

回答

0

我認爲你可以分析函數做到這一點:

select ID, Color, Shape, Weight, 
     max(case when color = 'blue' then height end) over 
      (partition by id) as Height, 
     max(case when color = 'blue' then Price end) over 
      (partition by id) as Price_Blue, 
     max(case when color = 'red' then Price end) over 
      (partition by id) as Price_Red 
from table t; 

編輯:

那些如此傾斜可以測試該代碼:

with t(ID, Color, Shape, Weight, Height, Price) as (
     select 1, 'blue', 'sq',  10,  12,  5 from dual union all 
     select 1, 'red', 'sq', 10,  13,  6 from dual union all 
     select 2, 'blue', 'sq',  10,  14,  7 from dual union all 
     select 3, 'blue', 'sq', 10,  15,  8 from dual union all 
     select 3, 'red', 'sq',  10,  16,  9 from dual 
    ) 
select ID, Color, Shape, Weight, 
     max(case when color = 'blue' then height end) over 
      (partition by id) as Height, 
     max(case when color = 'blue' then Price end) over 
      (partition by id) as Price_Blue, 
     max(case when color = 'red' then Price end) over 
      (partition by id) as Price_Red 
from t; 

而且this是SQL小提琴。

This是SQL Server的SQL小提琴。 Postgres的here

+0

這不會將藍色高度放在紅色的行中。 – Barmar 2015-03-03 00:45:30

+0

您正在使用'Price_Blue'和'Price_Red'的分析函數。你使用什麼分析函數來處理「高度」? – Barmar 2015-03-03 00:56:55

+0

另外,他只想在'color = red'時使用「other」行來獲得'Height',而不是'color = blue'時。 – Barmar 2015-03-03 00:57:45

0

還有兩種可能的解決方案。在這兩種情況下,當id只有「紅色的行」時,我從這一行取得高度。

查詢1:

with b as (select * from test where color='blue'), 
    r as (select * from test where color='red') 
select id, b.color, b.shape, b.weight, b.height, 
    b.price price_blue, r.price price_red 
    from b left join r using (id) 
union 
select id, r.color, r.shape, r.weight, 
    nvl2(b.color, b.height, r.height) height, b.price, r.price 
    from r left join b using (id) order by id, color 

查詢2:

select id, color, shape, weight, 
    case 
     when color = 'red' then (
     case when exists (
      select 1 from test t1 where t1.id = test.id and color='blue') 
      then (select height from test t1 where t1.id = test.id and color='blue') 
      else height end) 
     else height end height, 
    case when color = 'blue' then price else (select price from test t1 
     where t1.id = test.id and color='blue') end price_blue, 
    case when color = 'red' then price else (select price from test t1 
     where t1.id = test.id and color='red') end price_red 
    from test order by id, color 
相關問題