0

我在这里有一个有趣的问题。但这只是为了知识,因为我已经以一种非优雅的方式解决了它。

我有一张有客户的桌子,他们可以是持有人或家属,这种关系被描述为一个家庭。每个家庭只能有一个持有者和 0-n 个受抚养人。持有人由 H 标识,受抚养人由 D 标识。

我需要的是一种按持有人及其家属姓名排序数据的方法。所以下面的示例数据

idcostumer      name                 idfamily        relation
    1         Natalie Portman           1               H
    2         Mark Twain                3               D
    3         Carl Sagan                2               D
    4         Bob Burnquist             2               H
    5         Sheldon Cooper            1               D
    6         Anakin Skywalker          4               H
    7         Luke Skywalker            4               D
    8         Leia Skywalker            4               D
    9         Burnquist Jr.             2               D
    10        Micheal Jackson           3               H
    11        Sharon Stone              1               H
    12        Michelle Pfeiffer         3               D

是否有可能在一次查询中获得上述结果?如您所见,订单是名称(仅适用于持有人)

idcostumer      name                 idfamily        relation
    6         Anakin Skywalker          4               H
    8         Leia Skywalker            4               D
    7         Luke Skywalker            4               D
    4         Bob Burnquist             2               H
    9         Burnquist Jr.             2               D
    3         Carl Sagan                2               D
    10        Micheal Jackson           3               H
    2         Mark Twain                3               D
    12        Michelle Pfeiffer         3               D
    11        Sharon Stone              1               H
    1         Natalie Portman           1               D
    5         Sheldon Cooper            1               D

此示例的测试用例数据。

create table costumer (
    idcostumer integer primary key,
    name varchar2(20),
    idfamily integer,
    relation varchar2(1)
);

这是该表的插入语句:

insert into costumer values ( 1 , 'Natalie Portman'  , 1, 'D');
insert into costumer values ( 2 , 'Mark Twain'       , 3, 'D');
insert into costumer values ( 3 , 'Carl Sagan'       , 2, 'D');
insert into costumer values ( 4 , 'Bob Burnquist'    , 2, 'H');
insert into costumer values ( 5 , 'Sheldon Cooper'   , 1, 'D');
insert into costumer values ( 6 , 'Anakin Skywalker' , 4, 'H');
insert into costumer values ( 7 , 'Luke Skywalker'   , 4, 'D');
insert into costumer values ( 8 , 'Leia Skywalker'   , 4, 'D');
insert into costumer values ( 9 , 'Burnquist Jr.'    , 2, 'D');
insert into costumer values ( 10, 'Micheal Jackson'  , 3, 'H');
insert into costumer values ( 11, 'Sharon Stone'     , 1, 'H');
insert into costumer values ( 12, 'Michelle Pfeiffer', 3, 'D');

我已经尝试了一些事情,通过语句连接创建父太阳关系,并将familyid与关系连接起来。使用了一个 row_count 和一个按关系 desc 和家庭 id 排序的 over 子句,但是这样我就丢失了名称顺序。

4

2 回答 2

3

如果我理解正确,您想先按持有人的姓名排序家庭,然后按家属的姓名。以下是这样做的。

with family_order as (
  select  idfamily, rownum r from (
    select idfamily from costumer where relation = 'H' order by name
  ) 
)
select c.* from costumer c 
inner join family_order fo on c.idfamily = fo.idfamily
order by fo.r, relation desc, name

在这里提琴

于 2013-10-23T21:30:30.343 回答
2

尝试:

select * from table order by idfamily desc, relation desc, name asc    

链接到小提琴

对于非自然顺序,您可以使用“union all”:

select * from (select idcostumer, name, idfamily, relation from costumer 
  where idfamily > 3
  order by idfamily desc, relation desc, name asc)
union all
select * from (
  select idcostumer, name, idfamily, relation from costumer 
  where idfamily = 2
  order by idfamily desc, relation desc, name asc)
union all
select * from (
  select idcostumer, name, idfamily, relation from costumer 
  where idfamily != 2 and idfamily < 4
  order by idfamily desc, relation desc, name asc)

链接到小提琴

于 2013-10-23T21:14:03.937 回答