Home » SQL & PL/SQL » SQL & PL/SQL » Help required for Query (Oracle 11g)
Help required for Query [message #615894] Tue, 10 June 2014 19:15 Go to next message
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 #615895 is a reply to message #615894] Tue, 10 June 2014 19:16 Go to previous messageGo to next message
nickz
Messages: 39
Registered: September 2013
Location: US
Member
Attached SQL Script for Table creation and Data Insertion
  • Attachment: Script.sql
    (Size: 1.29KB, Downloaded 1155 times)
Re: Help required for Query [message #615896 is a reply to message #615895] Tue, 10 June 2014 19:24 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
ever hear about normalizing data or Third Normal Form?

Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/ and please read http://www.orafaq.com/forum/t/174502/102589/
Re: Help required for Query [message #615897 is a reply to message #615896] Tue, 10 June 2014 19:33 Go to previous messageGo to next message
nickz
Messages: 39
Registered: September 2013
Location: US
Member
Hi BlackSwan,

I am aware of that, but unfortunately, can't help it. Sad A view similar to the table I have mentioned is the only thing that we have access to and that works as our sole source. Have to work with that only.
Thanks...
Re: Help required for Query [message #615902 is a reply to message #615897] Tue, 10 June 2014 22:52 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
  1  with manufacturer
  2  as (select id mid, param_val manf from tb_test_01 where param_nm = 'MANUFACTURER'),
  3      COUNTRY
  4  AS (SELECT id cid, PARAM_VAL CNTY FROM tb_test_01 WHERE PARAM_NM = 'COUNTRY')
  5* select max(mid), m.manf, c.cnty from manufacturer m, country c where mid = cid group by m.manf, c.cnty
SQL> /

  MAX(MID) MANF                     CNTY
---------- ------------------------ ------------------------
        70 HONDA                    UK
        10 NISSAN                   USA
        30 NISSAN                   UK
        80 HONDA                    USA
Re: Help required for Query [message #616040 is a reply to message #615902] Wed, 11 June 2014 21:15 Go to previous messageGo to next message
nickz
Messages: 39
Registered: September 2013
Location: US
Member
Thanks a lot BlackSwan! Smile 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;

Re: Help required for Query [message #616041 is a reply to message #616040] Wed, 11 June 2014 21:57 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Since NOBODY can optimize SQL just by looking at it, we need a few more details.
http://www.orafaq.com/forum/m/433888/#msg_433888
Please refer to URL above & be sure to provide the details requested:
1) DDL for all tables & indexes
2) EXPLAIN PLAN
3) output from SQL_TRACE & tkprof
Re: Help required for Query [message #616652 is a reply to message #616041] Thu, 19 June 2014 05:27 Go to previous messageGo to next message
Jack14
Messages: 497
Registered: December 2011
Location: INDIA
Senior Member
select manu_val, country_val, max(id) from
(SELECT m1.id id, m1.PARAM_NM manu, m1.PARAM_VAL manu_val, m2.PARAM_NM country, m2.PARAM_VAL country_val
from manu_parameter m1,manu_parameter m2
where m1.ID=m2.ID
and m1.PARAM_NM ='MANUFACTURER'
and m2.PARAM_NM ='COUNTRY')
group by manu_val, country_val
Re: Help required for Query [message #616661 is a reply to message #616652] Thu, 19 June 2014 05:50 Go to previous message
Michel Cadot
Messages: 68643
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Balaji/Ajit/Jack/Mohan/Mathew
Please read How to use [code] tags and make your code easier to read and apply it.

Previous Topic: How to add new partition on a table if Default partition is existing
Next Topic: how to add mautiple partition on a table.
Goto Forum:
  


Current Time: Tue Apr 23 08:36:56 CDT 2024