简体   繁体   English

查询结果中的列顺序

[英]order of columns from the query results

thank you very much as usual support from all of you.. 非常感谢大家一如既往的支持。

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;

I want to sort the order of the columns..from result of the query 我想从查询结果中排序列的顺序。

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

from the above list i want to give the correct order of the column like below.. 从上面的列表中,我想给出如下正确的列顺序。

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

Many Thanks for your help.. 非常感谢您的帮助..

sunitha.. sunitha ..

As you are using UNION it will automatically gets sorted before it displays the results. 当您使用UNION时,它将在显示结果之前自动进行排序。 Hence by replacing UNION with UNION ALL and removing ORDER statement at the end will give you required result. 因此,通过用UNION ALL替换UNION并最后删除ORDER语句,将为您提供所需的结果。

You can define the order as part of the union construct. 您可以将订单定义为联合构造的一部分。 Add an extra column to every branch, with a value that reflects the order you want, like: 向每个分支添加一个额外的列,其值反映您想要的顺序,例如:

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

It doesn't really matter if the branches are in the final order you want within the union (and, incidentally, you should probably be using union all here as there are no duplicates to suppress?), and they don't seem to be in the order you want now. 分支在联盟中是否以您想要的最终顺序并不是很重要(顺便说一句,您可能应该在此处使用union all ,因为没有重复项可以抑制?),它们似乎并没有按照您现在想要的顺序。 Just put the right order_col value in each branch. 只需在每个分支中输入正确的order_col值即可。 The values don't have to be contiguous. 这些值不必是连续的。 It looks like you'll need another CASE to set the order_col for the noof values, based on the activity type, with the default value set higher if you want Working days to come last in that section. 似乎您需要另一个CASE来根据活动类型为noof值设置order_col ,如果您希望Working days在该部分中排在最后,则将默认值设置得更高。

Just removing the order by 1 would at least require you to change the order of the clauses in the query, but still wouldn't guarantee the result would be in that order - since there is no inherent order without an order by clause. 仅将order by 1删除order by 1至少会要求您更改查询中子句的顺序,但仍不能保证结果按该顺序排列-因为没有order by子句就没有固有的顺序。

You don't want select * for the outer query as that would show the order_col value; 您不希望为外部查询select * ,因为那样会显示order_col值; so instead specify the columns you do want, which seems to be a and "Total" . 因此,请指定您想要的列,似乎是a"Total"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM