2015-03-03 95 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 
相关问题