对于下面的表结构,我无法构建所需的输出,不确定是否需要应用转置,我不具备丰富的知识。 用于表创建和数据插入的SQL脚本将在最后给出。
输出应采用以下格式,如上所示,输出结果的ID_MAX_VAL应为主源表中每个制造商 - 国家/地区组合的最大ID。我需要获取每个制造商 - 国家/地区组合的最大ID,并显示/使用它们发送报告。
输出:
MANUFACTURER COUNTRY ID_MAX_VAL
--------------------------------------
NISSAN USA 10
NISSAN UK 30
HONDA USA 80
HONDA UK 70
注意:这是一个测试数据和表格结构,用于模拟实际的业务需求。
类似于我提到的表的视图是我们唯一可以访问的视图,它是我们唯一的来源。只能与之合作。
SQL脚本:
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;
答案 0 :(得分:2)
您也可以使用PIVOT
功能:
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;
输出:
| MANUFACTURER | COUNTRY | ID_MAX_VAL |
|--------------|---------|------------|
| HONDA | UK | 70 |
| NISSAN | USA | 10 |
| NISSAN | UK | 30 |
| HONDA | USA | 80 |
答案 1 :(得分:1)
如果没有标准化设计,我们可以自行加入表格,如下图所示。
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
答案 2 :(得分:0)
试试这个
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;
/
和出局
SELECT manufacturer, country,MAX(ID) id_max FROM TB_TEST_01
GROUP BY manufacturer,country;
答案 3 :(得分:0)
@San @OracleUser 感谢所有人的帮助和不同类型的解决方案.. PIVOT,SELF JOIN .. :) (我是PIVOT的新手,还需要了解它是如何工作的)
我这样使用它来获取特定制造商的详细信息:
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;