简体   繁体   中英

Oracle SQL Query - Help Required

For the below 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. SQL script for table creation and data insertion is given towards the end.

Output should be in below format as shown above 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.

Output:

MANUFACTURER    COUNTRY     ID_MAX_VAL
--------------------------------------
NISSAN          USA         10
NISSAN          UK          30
HONDA           USA         80
HONDA           UK          70

Note: This is a test data and table structure to simulate the actual business requirement.

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.

SQL Script:

CREATE TABLE TB_TEST_01 
(
  ID NUMBER(6) NOT NULL 
, PARAM_NM VARCHAR2(200) NOT NULL 
, PARAM_VAL VARCHAR2(200) 
);
/

INSERT INTO TB_TEST_01 (ID, PARAM_NM, PARAM_VAL) VALUES (10, 'MANUFACTURER', 'NISSAN');
INSERT INTO TB_TEST_01 (ID, PARAM_NM, PARAM_VAL) VALUES (10, 'COUNTRY', 'USA');
INSERT INTO TB_TEST_01 (ID, PARAM_NM, PARAM_VAL) VALUES (30, 'MANUFACTURER', 'NISSAN');
INSERT INTO TB_TEST_01 (ID, PARAM_NM, PARAM_VAL) VALUES (30, 'COUNTRY', 'UK');
INSERT INTO TB_TEST_01 (ID, PARAM_NM, PARAM_VAL) VALUES (20, 'MANUFACTURER', 'NISSAN');
INSERT INTO TB_TEST_01 (ID, PARAM_NM, PARAM_VAL) VALUES (20, 'COUNTRY', 'UK');
INSERT INTO TB_TEST_01 (ID, PARAM_NM, PARAM_VAL) VALUES (50, 'MANUFACTURER', 'HONDA');
INSERT INTO TB_TEST_01 (ID, PARAM_NM, PARAM_VAL) VALUES (50, 'COUNTRY', 'USA');
INSERT INTO TB_TEST_01 (ID, PARAM_NM, PARAM_VAL) VALUES (60, 'MANUFACTURER', 'HONDA');
INSERT INTO TB_TEST_01 (ID, PARAM_NM, PARAM_VAL) VALUES (60, 'COUNTRY', 'USA');
INSERT INTO TB_TEST_01 (ID, PARAM_NM, PARAM_VAL) VALUES (80, 'MANUFACTURER', 'HONDA');
INSERT INTO TB_TEST_01 (ID, PARAM_NM, PARAM_VAL) VALUES (80, 'COUNTRY', 'USA');
INSERT INTO TB_TEST_01 (ID, PARAM_NM, PARAM_VAL) VALUES (70, 'MANUFACTURER', 'HONDA');
INSERT INTO TB_TEST_01 (ID, PARAM_NM, PARAM_VAL) VALUES (70, 'COUNTRY', 'UK')
;
/
COMMIT;

You can use PIVOT function also:

select M_PARAM_VAL MANUFACTURER, C_PARAM_VAL COUNTRY, max(ID) ID_MAX_VAL
  from TB_TEST_01
 pivot (min(PARAM_VAL) as PARAM_VAL for (PARAM_NM) in ('MANUFACTURER' as M, 'COUNTRY' as C))
group by M_PARAM_VAL, C_PARAM_VAL;

Output:

| MANUFACTURER | COUNTRY | ID_MAX_VAL |
|--------------|---------|------------|
|        HONDA |      UK |         70 |
|       NISSAN |     USA |         10 |
|       NISSAN |      UK |         30 |
|        HONDA |     USA |         80 |

In case of not a normalized design, we can self join the table, like one below.

SELECT T1.PARAM_VAL AS MANUFACTURER,
       T2.PARAM_VAL AS COUNTRY,
       MAX(T1.ID) AS MAX_ID_VAL
FROM TB_TEST_01 T1,TB_TEST_01 T2
WHERE
       T1.ID = T2.ID
   AND T1.PARAM_NM='MANUFACTURER'
   AND T2.PARAM_NM='COUNTRY'
GROUP BY
   T1.PARAM_VAL,T2.PARAM_VAL

Try this

CREATE TABLE TB_TEST_01 
(
  ID NUMBER(6) NOT NULL 
, MANUFACTURER VARCHAR2(200) NOT NULL 
, COUNTRY VARCHAR2(200) 
);
/

INSERT INTO TB_TEST_01 (ID, MANUFACTURER, COUNTRY) VALUES (10, 'NISSAN', 'USA');
INSERT INTO TB_TEST_01 (ID, MANUFACTURER, COUNTRY) VALUES (20, 'NISSAN', 'UK');
INSERT INTO TB_TEST_01 (ID, MANUFACTURER, COUNTRY) VALUES (30, 'NISSAN', 'UK');
INSERT INTO TB_TEST_01 (ID, MANUFACTURER, COUNTRY) VALUES (50, 'HONDA', 'USA');
INSERT INTO TB_TEST_01 (ID, MANUFACTURER, COUNTRY) VALUES (60, 'HONDA', 'USA');
INSERT INTO TB_TEST_01 (ID, MANUFACTURER, COUNTRY) VALUES (70, 'HONDA', 'UK');
INSERT INTO TB_TEST_01 (ID, MANUFACTURER, COUNTRY) VALUES (80, 'HONDA', 'USA');

COMMIT;
/

and for out put

SELECT manufacturer, country,MAX(ID) id_max FROM TB_TEST_01
GROUP BY manufacturer,country;

@San @OracleUser Thanks all for the help and with different types of solutions.. PIVOT, SELF JOIN.. :) (I'm new to PIVOT, still need to understand how it works)

I used it like this to get the details of a particular manufacturer:

SELECT T1.PARAM_VAL AS MANUFACTURER,
       T2.PARAM_VAL AS COUNTRY,
       MAX(T1.ID) AS MAX_ID_VAL
FROM TB_TEST_01 T1,TB_TEST_01 T2
WHERE
       T1.ID = T2.ID
   AND T1.PARAM_NM='MANUFACTURER'
   AND T2.PARAM_NM='COUNTRY'
   AND T1.PARAM_VAL='HONDA'
GROUP BY
   T1.PARAM_VAL,T2.PARAM_VAL;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM