SQL查询,用于从具有相同ID的不同行中选择值

时间:2015-03-03 00:35:58

标签: sql oracle

我正在尝试根据某个字段的值从不同的记录中选择不同的值,这些记录可以具有相同或不同的ID。例如:

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),则必须相应地拾取所有字段。在这种情况下,第二个价格字段将为空。 我非常感谢任何帮助。我试图提出一个查询,但到目前为止我无法找到一种方法来制作蓝色记录的高度值,无论颜色是什么。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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
相关问题