我有一个名为CASE_PERSON
的表。DDL是
CREATE TABLE CASE_PERSON
(
ID VARCHAR2(50) NOT NULL PRIMARY KEY,
p_id VARCHAR2(50) DEFAULT NULL NOT NULL,
id_card VARCHAR2(18) DEFAULT NULL NOT NULL,
p_name VARCHAR2(50) NOT NULL,
case_no VARCHAR2(60) NOT NULL,
case_name VARCHAR2(200) NOT NULL,
)
还有一些DML
insert into CASE_PERSON(ID, P_ID, ID_CARD, P_NAME, CASE_NO, CASE_NAME)
values ('001', '123', '123-123123', 'tom', 'c-001', 'case-one');
insert into CASE_PERSON(ID, P_ID, ID_CARD, P_NAME, CASE_NO, CASE_NAME)
values ('002', '234', '123-123234', 'rose', 'c-001', 'case-one');
insert into CASE_PERSON(ID, P_ID, ID_CARD, P_NAME, CASE_NO, CASE_NAME)
values ('003', '345', '123-123345', 'jack', 'c-001', 'case-one');
insert into CASE_PERSON(ID, P_ID, ID_CARD, P_NAME, CASE_NO, CASE_NAME)
values ('005', '555', '123-123555', 'roman', 'c-002', 'case-two');
insert into CASE_PERSON(ID, P_ID, ID_CARD, P_NAME, CASE_NO, CASE_NAME)
values ('006', '666', '123-123666', 'jim', 'c-002', 'case-two');
该表是根据Person
表和case
表构建的。 p的前缀来自PERSON
,大小写来自CASE
。首先,我从PERSON
获取所有身份证,并从CASE_PERSON
查询以查找具有相同大小写的人匹配给定的身份证。
现在,我想得到的结果称为“相同病例的人”。输出应为以下格式:
id p_id id_card case_no case_name same_case_id_card same_case_p_name
1 123 123-123123 c-001 case-one 123-123234 rose
2 123 123-123123 c-001 case-one 123-123345 jack
3 555 123-123555 c-002 case-two 123-123666 jim
如何查询?
答案 0 :(得分:2)
我认为您要查找的查询是:
SELECT * FROM case a, case b
WHERE a.case_no = b.case_no;
答案 1 :(得分:1)
您似乎想要第一人称的案件详细信息,以及非第一人称的详细信息:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER(PARTITION BY case_no ORDER BY p_id) as rown FROM case_person
)
SELECT
ROW_NUMBER() OVER(ORDER BY two.p_id) as id,
one.p_id,
one.id_card,
one.case_no,
one.case_name,
two.id_card as same_case_id_card,
two.case_name as same_case_p_name
FROM
(SELECT * FROM cte WHERE rown = 1) one
INNER JOIN
(SELECT * FROM cte WHERE rown > 1) two
ON one.case_no = two.case_no