从具有相同id的不同行中选择值的SQL查询
我试图从不同的记录中选择不同的值,这些记录可以基于某个字段的值具有相同或不同的ID。例如:从具有相同id的不同行中选择值的SQL查询,sql,oracle,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
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的记录),那么它必须相应地拾取所有字段。在这种情况下,第二个价格字段将为空。
我真的很感谢你的帮助。我试图提出一个查询,但到目前为止,我找不到一种方法使高度的值与蓝色记录的值相同,无论颜色是什么。我想你可以用分析函数来实现这一点:
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;
这就是SQL小提琴
是SQL Server的SQL小提琴。对于博士后来说。还有两种可能的解决方案。在这两种情况下,当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
您需要使用自连接来匹配具有相同ID和不同颜色的行。请显示您尝试的解决方案,因此这不是免费的编码服务。您需要使用
完全外部连接
,以允许不匹配其他颜色的行。我理解。我无法完成查询,因为当颜色为红色时,我无法确定如何选择不同行的值。我可以使用out join等,但不知何故,我需要弄清楚当颜色为红色时,如何拾取蓝色的高度。这是一个糟糕的示例,但我正试图找到解决方案。我需要从所有记录中收集所有信息,但棘手的是,无论给定行的颜色如何,始终选择蓝色值作为高度。使用仅返回蓝色行高度的子查询,和ID上的外部连接。这不会将蓝色高度放在红色行中。您使用的是Price\u blue
和Price\u red
的分析函数。你用什么分析函数来计算高度呢?而且,他只想在颜色=红色时用“其他”行来计算高度,而不是在颜色=蓝色时。你放错了SQLFIDLE链接,我只得到一个空白的FIDLE。@Barmar。非常感谢。我错过了问题中关于高度的部分。