-1

我在 Oracle 数据库中有如下所示的数据表。

emp_num  person_name  organization  earned_date  sum(hours)
-------  -----------  ------------  -----------  ----------
36372    Name1        Test1         23-MAR-11      3.17
36372    Name1        Test1         15-MAR-11      6.70
40208    Name2        Test2         08-APR-11     13.50
40208    Name2        Test2         06-APR-11     12.07

我需要更改查询输出,如下所示。我怎样才能做到这一点?

emp_num  person_name  organization  23-MAR-11  15-MAR-11  08-APR-11  06-APR-11
-------  -----------  ------------  ---------  ---------  ---------  ---------
36372     Name1       Test1           3.17        6.70      
40208     Name2       Test2                                 13.50      12.70     
4

2 回答 2

1

您不能动态命名表中的列,除非您使用某种形式的动态 SQL。但是,您可以使用通用日期列获得所需的内容:

select emp_num, person_name, organization, 
       sum(decode(datenum, 1, hours, 0)) as date1hours,
       sum(decode(datenum, 2, hours, 0)) as date2hours,
       ...
       min(decode(datenum, 1, earned_date) as date1,
       min(decode(datenum, 2, earned_date) as date2,
       ...
from 
(
  select t.*, 
     dense_rank() over (partition by NULL order by earned_date) as datenum
  from the_table t
) t
group by emp_num, person_name, organization 

顺便说一句,Oracle 10g 支持该CASE语法,我建议您使用它而不是decode.

于 2012-05-08T13:27:10.840 回答
0
select
  emp_num,
  person_name,
  organization,
  sum(decode(earned_date,to_date('23/03/2011','dd/mm/yyyy'),hours,0)) 23mar11,
  sum(decode(earned_date,to_date('15/03/2011','dd/mm/yyyy'),hours,0)) 15mar11,
  sum(decode(earned_date,to_date('08/04/2011','dd/mm/yyyy'),hours,0)) 08apr11,
  sum(decode(earned_date,to_date('06/04/2011','dd/mm/yyyy'),hours,0)) 06apr11
from
  the_table //don't know the name
group by
  emp_num,
  person_name,
  organization

始终使用 to_date 函数将日期与字符串进行比较,我在这里使用了常见的英国格式。

于 2012-05-08T12:24:59.733 回答