我有2个表(本例简化)我想以特定的方式加入。
继承人提供帮助
INSERT INTO ae VALUES ('0001-0012','DRY EYE (12 OCT 2017)');
INSERT INTO ae VALUES ('0001-0012', 'DEPRESSION (7 OCT 2017)');
INSERT INTO ae VALUES ('0001-0013','SKIN ATROPHY, LEFT UPPER ARM (4 OCT 2017 )');
INSERT INTO mh VALUES ('0001-0012', 'DIABETES MELLITUS (UN JUL 2007)');
INSERT INTO mh VALUES ('0001-0012', 'GASTRO-ESOPHAGEAL REFLUX INTERMITENT GR1 (18 AUG 2017)');
INSERT INTO mh VALUES ('0001-0012', 'ESOPHAGITIS GR 1 (18 AUG 2017)');
INSERT INTO mh VALUES ('0001-0012', 'DIARRHEA INITERMITTENT GR2 (5 JUL 2017 )');
INSERT INTO mh VALUES ('0001-0012', 'FATIGUE INTERMITTENT GR1 (18 AUG 2017)');
INSERT INTO mh VALUES ('0001-0013', 'VOMITING, INTERMITTENT GR1 (6 JUL 2017 )');
那里有任何帮助吗?你可以看到我需要的一个粗糙的连接!
答案 0 :(得分:1)
您可以使用row_number()
和full join
:
select coalesce(ae.col1, mh.col1) as col1, ae.col2, mh.col2
from (select ae.*, row_number() over (partition by col1 order by col1) as seqnum
from ae
) ae full join
(select mh.*, row_number() over (partition by col1 order by col1) as seqnum
from mh
) mh
on mh.col1 = ae.col1 and mh.seqnum = ae.seqnum
order by coalesce(ae.col1, mh.col1), seqnum;
答案 1 :(得分:0)
继续我的更新代码到目前为止它现在给我正确的命令编辑只是在代码的末尾添加了DESC给我结果
select coalesce(ae.subject, mh.subject) as subject, ae.aeterms, mh.mhterms
from (select ae.*, row_number() over (partition by subject order by subject)
as seqnum
from ae) ae full join
(select mh.*, row_number() over (partition by subject order by subject) as
seqnum from mh) mh
on mh.subject = ae.subject and mh.seqnum = ae.seqnum
order by coalesce(ae.subject, mh.subject), ae.seqnum Desc;