sqlpivothana

SQl query to join a table with Attributes in different rows and filter values


I am pretty new to SQL and actually a C# programmer. I have been given a table with PartNumbers that have different attribute Names and Values. The objects would have a class like car, truck, jeep etc... & each class would have different attributes like Milage for CAR & tonnage for Trucks. Each Partnumber may have multiple revisions and the attribute values for these revisons can change.

I would like to query the table for latest revision of the class.

Could someone please help. I have tried multiple options by googleing. May be my dumb brain could not get it right.

ORIGINAL DATABASE TABLE:

Part Number REV AttributeName AttributeValue
256223 A Class CAR
256223 A Milage 15000
256223 A Seats 5
256223 B Class CAR
256223 B Milage 30000
256223 B Seats 5
256223 C Class CAR
256223 C Milage 45000
256223 C Seats 5
256224 A Class JEEP
256224 A Color RED
256224 A Seats 5
256224 B Class JEEP
256224 B Color BLUE
256224 B Seats 4
256224 C Class JEEP
256224 C Color GREEN
256224 C Seats 2
256225 1 Classi TRUCK
256225 1 Milage 30000
256225 1 Tonnage 7
256225 A Class TRUCK
256225 A Milage 60000
256225 A Tonnage 6
256225 B Class TRUCK
256225 B Milage 70000
256225 B Tonnage 12
256226 A Class CAR
256226 A Milage 5000
256226 A Seats 5
256226 B Class CAR
256226 B Milage 8000
256226 B Seats 5
256226 C Class CAR
256226 C Milage 12000
256226 C Seats 5
256227 A Class JEEP
256227 A Color YELLOW
256227 A Seats 5
256227 B Class JEEP
256227 B Color BROWN
256227 B Seats 41
256227 C Class JEEP
256227 C Color TEAL
256227 C Seats 2
256228 1 Class TRUCK

NEED TO QUERY FOR CLASS, SAY EG. 'TRUCK' & GET ONLY THE LATEST REVISION OF ALL TRUCKS

Part Number REV Class Milage Tonnage
256225 B TRUCK 70000 12
256228 B TRUCK 20000 20

SAY I QUERY FOR CLASS 'CAR'

Part Number REV Class Milage Seats
256223 C CAR 45000 5
256226 C CAR 12000 5

PLEASE NOTE THAT THE ATTRIBUTES DIFFER FROM CLASS TO CLASS

Thanks a lot in advance.

SQL Query to get latest revision from Table with different attributes

Here's what I have tried. Using SQL I first queried the attributes of a single class i needed to get. for eg. CAR using :

SELECT distinct attr_name FROM data WHERE partnumber =(SELECT partnumber FROM data WHERE attr_val  = 'CAR' GROUP BY partnumber LIMIT 1);

then using the list of attributes i Got, I create an SQL string programmatically to query like :

SELECT distinct A0.partnumber,A0.rev,A0.attr_val as "Class",A1.attr_val as "Milage",A2.attr_val AS "Seats"
FROM data as A0,data as A1,data as A2
WHERE A0.attr_name = 'Class'
AND A0.attr_val = 'CAR'
AND (A1.attr_name = 'Milage'
and A1.partnumber = A0.partnumber
AND A1.rev =A0.rev)
AND (A2.attr_name = 'Seats'
AND A2.partnumber = A0.partnumber
AND A2.rev = A0.rev);

I get the following Result:

PartNumber REV Class Milage Seats
256223 A CAR 15000 5
256223 B CAR 30000 5
256223 C CAR 45000 5
256226 A CAR 5000 5
256226 B CAR 8000 5
256226 C CAR 12000 5

Solution

  • For anyone looking for a similar Issue...Here is How I achieved my results.

    select partnumber,max(rev),Milage, Seats from (
    select partnumber,
           rev,
           'CAR' as class,
           max(case when attr_name = 'Milage' then attr_val end) as Milage,
           max(case when attr_name = 'Seats' then attr_val end) as Seats
     FROM data t
     WHERE exists (select 1 from data tt 
                   where t.partnumber = tt.partnumber
                     and tt.attr_name = 'Class'
                     and tt.attr_val = 'CAR'
               )
    GROUP BY partnumber, rev)
    GROUP BY partnumber;
    

    Thank You @ORA-01017 for guiding me in the right Direction.