Oracle Order通过不同的列相同的select语句

时间:2013-10-23 21:08:47

标签: sql oracle sql-order-by

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

我有一张有消费者的桌子,他们可以是持有人或家属,这种关系被描述为a 家庭。每个家庭只能拥有一个持有人和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与关系连接创建一个父亲太阳关系。 使用带有over子句的row_count按关系desc和family id排序,但这样我丢失了名称顺序。

2 个答案:

答案 0 :(得分: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

Fiddle here

答案 1 :(得分:2)

尝试:

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

链接到 Fiddle

对于非自然的顺序,您可以使用“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)

链接到 Fiddle