SQl 查询将具有不同行中的属性和过滤器值的表连接起来

问题描述 投票:0回答:1

我对 SQL 还很陌生,实际上是一名 C# 程序员。我得到了一张包含具有不同属性名称和值的 PartNUMers 的表。这些对象将有一个类,如汽车、卡车、吉普车等……每个类都有不同的属性,如汽车的里程和卡车的吨位。每个零件号可能有多个修订版,并且这些修订版的属性值可能会更改。

我想查询该课程的最新修订表。

有人可以帮忙吗?我通过谷歌搜索尝试了多种选择。可能是我脑子笨,搞不清楚。

提前非常感谢。

SQL Query to get latest revision from Table with different attributes

尝试过数据透视、最大修订等。我能够按类别进行过滤并将值放在同一行中,但无法按修订添加过滤器

sql pivot hana
1个回答
0
投票

您可以将

conditional aggregation
EXISTS
子句一起使用,如下所示:

select partnumber,
       rev,
       'YOUR_INPUT' as class,
       max(case when attribute_name = 'Milage' then attribute_value end) as Milage,
       max(case when attribute_name = 'Seats' then attribute_value end) as Seats
  FROM your_Table t
 WHERE exists (select 1 from your_Table tt 
               where t.part_number = tt.part_number
                 and tt.attribute_name = 'Class'
                 and tt.attribute_value = 'YOUR_INPUT')
GROUP BY partnumber, rev;

YOUR_INPUT
替换为 CAR、JEEP 等实际值

© www.soinside.com 2019 - 2024. All rights reserved.