查询结果中的列顺序

时间:2014-09-18 09:28:01

标签: sql oracle

非常感谢你们所有人的支持..

select t.* from (

select * from (
select   'Total#Days in a month : ' as a,
        count(1) as "Total"
       from (
 select trunc(sysdate,'MON')+level-1 as my_date
  from dual
  connect by level <= to_char(last_day(sysdate),'DD')
) a
union
select  'Sundays :' as a,

count(decode(TO_CHAR(my_date,'DY'),'SUN',1)) as sunday
from (
 select trunc(sysdate,'MON')+level-1 as my_date
  from dual
  connect by level <= to_char(last_day(sysdate),'DD')
  )
)
union
select
nvl(NoofDays,'Working Days'),decode(NoofDays,'', to_number(to_char(last_day(sysdate),'dd'))-count(*),nvl(count(*),0))
FROM (SELECT (CASE WHEN activity_type IN( 'SL','AL','2','OL') then 'Leave'
                   WHEN activity_type = 'BH' then 'BankHoliday'
                   WHEN activity_type = 'H'  then 'Holidays'
                   WHEN activity_type = 'IM'  then 'Internal Meeting'
                   WHEN activity_type = 'TR'  then 'Training'
                   WHEN activity_type = 'ISM'  then 'Office Work'
                   WHEN activity_type IN ('CS', '10', 'SAV','CME','9','19','15','20') then 'External Meeting'
                   WHEN activity_type = '12'  then 'Transit'
                   WHEN activity_type IN ('11','H')  then 'Others'
                   WHEN activity_type = '13'  then 'Flu Action day'
                   WHEN activity_type = '14'  then 'Corp. Immunization'
                   WHEN activity_type = '16'  then 'Inclinic Meeting'
                   WHEN activity_type = '17'  then 'UC'
                   WHEN activity_type = '18'  then 'CSD'
                   WHEN activity_type = '19'  then 'ESM'
                   ELSE activity_type   END) as NoofDays
      FROM planner_activity pa
      INNER JOIN employee e ON pa.employee_id=e.employee_id
      INNER JOIN  alignment a ON a.employee_id=e.employee_id
      WHERE a.tenant_id = 500020 and  a.alignment_id in(select alignment_id from alignment where manager_alignment_id=1006387678 )
      and  trunc(start_date_time) between trunc(to_date(&d1,'mm/dd/yyyy')) and trunc(to_date(&d2,'mm/dd/yyyy'))
     ) t
GROUP BY cube(NoofDays)
union
select 'No of Working days as for Vacci MI : 'as a,
 count(*)
from coaching c
inner join employee e on c.employee_id=e.employee_id
inner join alignment a on e.employee_id=a.employee_id
where a.tenant_id=500020 and a.alignment_id in(select alignment_id from alignment where manager_alignment_id=1006387678 )
and c.status='CLOS'
and  trunc(create_date) between trunc(to_date(&&d1,'mm/dd/yyyy')) and trunc(to_date(&&d2,'mm/dd/yyyy'))
union
select 'No Of Calls As Per Vacci MI :' as a,
        count(1)
from    event ev
inner join employee e on e.employee_id=ev.employee_id
inner join alignment a on e.employee_id=a.employee_id
where a.tenant_id=500020 and a.alignment_id in(select alignment_id from alignment where manager_alignment_id=1006387678 )
and  ev.ACCOMPANIED_BY='DM'
and  trunc(start_date_time) between trunc(to_date(&&d1,'mm/dd/yyyy')) and trunc(to_date(&&d2,'mm/dd/yyyy'))
union
select 'No Of Working Days As Per Vacc MI :' as a,
        count(distinct ev.start_date_time)
from    event ev
inner join employee e on e.employee_id=ev.employee_id
inner join alignment a on e.employee_id=a.employee_id
where a.tenant_id=500020 and a.alignment_id in(select alignment_id from alignment where manager_alignment_id=1006387678 )
and  ev.ACCOMPANIED_BY='DM'
and  trunc(start_date_time) between trunc(to_date(&&d1,'mm/dd/yyyy')) and trunc(to_date(&&d2,'mm/dd/yyyy'))
) t
order by 1;

我想根据查询结果对列的顺序进行排序

External Meeting    2
Internal Meeting    1
Leave   3
No Of Calls As Per Vacci MI :   32
No Of Working Days As Per Vacc MI : 32
No of Working days as for Vacci MI :    1
Sundays :   4
Total#Days in a month :     30
Training    4
Working Days    20

从上面的列表我想给出正确的列顺序,如下所示..

Total#Days in a month :     30
Sundays :   4
Leave   3
Internal Meeting    1
External Meeting    2
Training    4
Working Days    20
No Of Working Days As Per Vacc MI : 32
No of Working days as for Vacci MI :    1
No Of Calls As Per Vacci MI :   32

非常感谢您的帮助..

sunitha ..

2 个答案:

答案 0 :(得分:2)

当您使用UNION时,它会在显示结果之前自动排序。因此,通过用UNION ALL替换UNION并在最后删除ORDER语句将为您提供所需的结果。

答案 1 :(得分:0)

您可以将订单定义为union构造的一部分。为每个分支添加一个额外的列,其值反映您想要的顺序,如:

select a, "Total" from (
select   1 as order_col,
         'Total#Days in a month : ' as a,
...
union
select   2 as order_col,
         'Sundays :' as a,
...
)
order by order_col

如果分支在联合中处于你想要的最终顺序中并不重要(顺便说一句,你应该在这里使用union all,因为没有重复的东西可以抑制?),而且他们似乎没有你想要的顺序。只需在每个分支中添加正确的order_col值即可。价值观不必是连续的。看起来您需要另一个CASE根据活动类型为order_col值设置noof,如果您需要{{1,则默认值设置得更高在该部分中最后一步。

删除Working days至少会要求您更改查询中子句的顺序,但仍然不能保证结果会按顺序排列 - 因为没有固有顺序而没有一个order by 1条款。

您不希望order by用于外部查询,因为它会显示select *值;因此,请指定您想要的列,似乎是order_cola