Help required for Query [message #615894] |
Tue, 10 June 2014 19:15 |
|
nickz
Messages: 39 Registered: September 2013 Location: US
|
Member |
|
|
For the attached Table structure, I am not able to build the required output, not sure if I need to apply transpose, of which I don't have a strong knowledge.
Please have a look at the jpg attachment. Also, attached the SQL script for table creation and data insertion.
Output should be in the format as shown in the jpg attachment and ID_MAX_VAL of output result should be the max of ID for each manufacturer-country combination in main source table. I need to fetch the max id for each manufacturer-country combination and display/use them to send out a report.
Note: This is a test data and table structure to simulate the actual business requirement.
|
|
|
|
|
|
|
Re: Help required for Query [message #616040 is a reply to message #615902] |
Wed, 11 June 2014 21:15 |
|
nickz
Messages: 39 Registered: September 2013 Location: US
|
Member |
|
|
Thanks a lot BlackSwan! For this test table and data, it works fine.
But, when I want to find out the maximum ID for a particular manufacturer-country combination, it takes a lot of time, considering my real table has lots of data in it. Any way to tune it further?
I have put the query as below to get the max ID for Honda USA.
WITH
manufacturer AS ( SELECT ID mid, param_val manf
FROM tb_test_01
WHERE param_nm = 'MANUFACTURER'
AND param_val = 'HONDA'
),
country AS ( SELECT ID cid, param_val cnty
FROM tb_test_01
WHERE param_nm = 'COUNTRY'
AND param_val = 'USA'
)
SELECT MAX(mid), m.manf, c.cnty
FROM manufacturer m, country c
WHERE mid = cid
GROUP BY m.manf, c.cnty;
|
|
|
|
|
|