选择语句Oracle with tree

时间:2017-05-20 13:13:58

标签: sql oracle

我有2个表区域社区

REGIONS

ID        REGION_NAME
1         Erevan
2         Tavush

群落

ID        REGION_ID       COMMUNITY_NAME
1             1             SHENGAVIT
2             1             DAVTASHEN
3             1             EREBUNI
4             2              ANUN
5             2             AZGANUN

我想选择并得到这样的结果:

REGION_NAME         COMMUNITY_NAME
------------------------------------
EREVAN
                    SHENGAVIT
                    DAVTASHEN
                    EREBUNI
TAVUSH
                    ANUN
                    AZGANUN

请帮忙解决问题

4 个答案:

答案 0 :(得分:1)

这样的事情:

select REGION_NAME, COMMUNITY_NAME from (
    select REGION_NAME, ' ' as COMMUNITY_NAME, id as regid, 0 as ord from REGIONS
    union all
    select ' ' as REGION_NAME, COMUNITIES.COMMUNITY_NAME, COMUNITIES.REGION_ID as regid, COMUNITIES.ID as ord
    from REGIONS
    inner join COMUNITIES
    on REGIONS.id = COMUNITIES.REGION_ID
) t
order by regid, ord

答案 1 :(得分:0)

SELECT c.community_name 
FROM communities c 
INNER JOIN regions r ON r.id = c.region_id 
GROUP BY r.region_name;

答案 2 :(得分:0)

这种要求应该在报告层解决,而不是在SQL中解决。但是,如果您绝对必须在SQL中执行此操作,则可以执行此类操作。请注意,由于您不需要在每个“社区”行中显示区域名称,因此不需要连接;一个union all就足够了。该解决方案假设社区ID始终为正(因此,在订购时,人工值0总是首先出现 - 见下文)。

with
     regions ( id, region_name ) as (
       select 1, 'Erevan' from dual union all
       select 2, 'Tavush' from dual
     ),
     communities ( id, region_id, community_name ) as (
       select 1, 1, 'shengavit' from dual union all
       select 2, 1, 'davtashen' from dual union all
       select 3, 1, 'erebuni'   from dual union all
       select 4, 2, 'anun'      from dual union all
       select 5, 2, 'azganun'   from dual
     )
-- End of test data (not part of the solution). Query begins below this line.
select   region_name, community_name
from     (
           select id as r_id, 0 as c_id, region_name, null as community_name
             from regions
           union all
           select region_id as r_id, id as c_id, null as region_name, community_name
             from communities
         )
order by r_id, c_id
;

REGION_NAME  COMMUNITY_NAME
-----------  --------------
Erevan
             shengavit
             davtashen
             erebuni
Tavush
             anun
             azganun

答案 3 :(得分:0)

我假设您使用SQL*Plus。我无法准确生成您想要的输出,但只能生成类似的输出。

这是主/从或父/子关系,但不是树结构。

select R.REGION_NAME, C.COMMUNITY_NAME
from REGIONS R, COMMUNITIES C
where R.ID = C.REGION_ID
order by R.ID, C.ID
/

这将导致

REGION_NAME          COMMUNITY_NAME
-------------------- ----------------
EREVAN               SHENGAVIT
EREVAN               DAVTASHEN
EREVAN               EREBUNI
TAVUSH               ANUN
TAVUSH               AZGANUN

您可以使用SQL * Plus命令格式化输出。使用break on REGION_NAME命令来抑制重复打印REGION_NAME值并获取

REGION_NAME          COMMUNITY_NAME
-------------------- ----------------
EREVAN               SHENGAVIT
                     DAVTASHEN
                     EREBUNI
TAVUSH               ANUN
                     AZGANUN

或使用break on REGION_NAME skip 1命令获取

REGION_NAME          COMMUNITY_NAME
-------------------- ----------------
EREVAN               SHENGAVIT
                     DAVTASHEN
                     EREBUNI

TAVUSH               ANUN
                     AZGANUN

因此,要使用SQL * Plus生成此输出,您必须执行

break on REGION_NAME skip 1

select R.REGION_NAME, C.COMMUNITY_NAME
from REGIONS R, COMMUNITIES C
where R.ID = C.REGION_ID
order by R.ID, C.ID
/

请注意,这些SQL * Plus格式命令在SQL * Plus中可用,但在其他客户端中不可用。它们不是SQL语言的一部分。