Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

sql - Oracle - explanation of hierarchical query

I have a question about hierarchical SQL.

sample data:

create table group_ (
group_id number);

create table set_(
set_id number);

insert into group_ values(1);
insert into group_ values(2);
insert into group_ values(3);
insert into set_ values(1);
insert into set_ values(2);

SQL:

SELECT  group_id,
        sys_connect_by_path(set_id, ', '), LEVEL
FROM    set_
CROSS   JOIN group_
CONNECT BY prior set_id < set_id
ORDER   BY group_id, combo nulls first,lvl;

My goal is to get all possible combinations of 2 tables, and it's working, but I've got repeated values so i have to use distinct/unique caluse. The question is - why they are appearing?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You're doing a UNION ALL. Effectively you're selecting everything from group_ and then selecting it again after joining with the set_ table.

Change to UNION and the duplicates will be removed.

See: http://sqlfiddle.com/#!4/7d079/3


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...