gpt4 book ai didi

sql - Oracle 加入 SQL 探索

转载 作者:行者123 更新时间:2023-12-04 19:13:36 28 4
gpt4 key购买 nike

我有 3 个表格如下:

CREATE TABLE USER_STATUS ("UID" varchar2(7), "STAT_ID" varchar2(11)) ;
INSERT ALL
INTO USER_STATUS ("UID", "STAT_ID") VALUES ('UID_001', 'STAT_ID_001')
INTO USER_STATUS ("UID", "STAT_ID") VALUES ('UID_001', NULL)
INTO USER_STATUS ("UID", "STAT_ID") VALUES ('UID_001', NULL)
INTO USER_STATUS ("UID", "STAT_ID") VALUES ('UID_002', 'STAT_ID_002')
INTO USER_STATUS ("UID", "STAT_ID") VALUES ('UID_002', NULL)
INTO USER_STATUS ("UID", "STAT_ID") VALUES ('UID_003', 'STAT_ID_003')
SELECT * FROM dual;


CREATE TABLE STATUS_LKUP ("LKUP_ID" varchar2(11), "STAT_CODE" varchar2(11), "STAT_ID" varchar2(11), "STATUS" varchar2(20));
INSERT ALL
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_001', 'ST_CODE_001', 'STAT_ID_001', 'Processing')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_002', 'ST_CODE_002', 'STAT_ID_001', 'Processing')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_003', 'ST_CODE_003', 'STAT_ID_001', 'Processing')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_004', 'ST_CODE_004', 'STAT_ID_001', 'Processing')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_005', 'ST_CODE_011', 'STAT_ID_001', 'Issue')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_006', 'ST_CODE_012', 'STAT_ID_001', 'Issue')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_007', 'ST_CODE_013', 'STAT_ID_001', 'Issue')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_008', 'ST_CODE_014', 'STAT_ID_001', 'Issue')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_009', 'ST_CODE_015', 'STAT_ID_001', 'Issue')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_010', 'ST_CODE_021', 'STAT_ID_001', 'Done')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_011', 'ST_CODE_022', 'STAT_ID_001', 'Done')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_012', 'ST_CODE_031', 'STAT_ID_001', 'Started')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_013', 'ST_CODE_032', 'STAT_ID_001', 'Started')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_014', 'ST_CODE_002', 'STAT_ID_002', 'Processing (Sent)')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_015', 'ST_CODE_004', 'STAT_ID_002', 'Processing (Waiting)')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_016', 'ST_CODE_014', 'STAT_ID_002', 'Issue in Prod')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_017', 'ST_CODE_012', 'STAT_ID_002', 'Issue in Prod')
INTO STATUS_LKUP ("LKUP_ID", "STAT_CODE", "STAT_ID", "STATUS") VALUES ('LKUP_ID_018', 'ST_CODE_021', 'STAT_ID_002', 'Done')
SELECT * FROM dual;


CREATE TABLE CORE ("CORE_ID" varchar2(11), "STAT_CODE" varchar2(11));

INSERT ALL
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_001', 'ST_CODE_001')
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_002', 'ST_CODE_012')
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_003', 'ST_CODE_021')
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_004', 'ST_CODE_012')
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_005', 'ST_CODE_012')
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_006', 'ST_CODE_021')
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_007', 'ST_CODE_001')
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_008', 'ST_CODE_003')
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_009', 'ST_CODE_012')
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_010', 'ST_CODE_021')
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_011', 'ST_CODE_001')
INTO CORE ("CORE_ID", "STAT_CODE") VALUES ('CORE_ID_013', 'ST_CODE_004')
SELECT * FROM dual;

检查这个 -> Oracle SQL Fiddle

该表是在 Oracle 数据库中创建的。现在根据传递的用户 UID,我需要按照以下方式检索具有状态的核心:

Click here to view Expected Results

到目前为止,我尝试检索结果但无法加入它们。

SELECT STLK.STAT_CODE, STLK.STATUS FROM STATUS_LKUP STLK WHERE STLK.STAT_ID IN (SELECT USRST.STAT_ID FROM USER_STATUS USRST WHERE USRST.UID = 'UID_001');

请帮忙。

仅供引用:这不是家庭作业。实际表格很复杂,这些只是为了更好的解释而转换。

提前谢谢你。

最佳答案

相当直接的连接;

SELECT sl."STATUS", sl."STAT_CODE", c."CORE_ID"
FROM USER_STATUS us
JOIN STATUS_LKUP sl
ON us."STAT_ID" = sl."STAT_ID"
JOIN CORE c
ON c."STAT_CODE" = sl."STAT_CODE"
WHERE "UID" = 'UID_001'
ORDER BY "STATUS", "LKUP_ID"

An SQLfiddle to test with .

关于sql - Oracle 加入 SQL 探索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16721895/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com