0

I am trying to show oracle data in following format (need to produce in form of report- may be crystal report);

enter image description here

Table has following fields:

enter image description here

I am able to produce following results so far:

enter image description here

Query:

SELECT to_date('01/01/1999','dd/mm/yyyy') LOGDATE,
    to_date('01/01/1999','dd/mm/yyyy') MONTH ,
    ''EMPCODE,
    to_number('0') PROJECTCODE , 
    ''ACTIVITY,
    to_number('0')TOTALTIME, 
    ''ENTEREDBY, 
    ''PROJECTDESC, 
    ''SUB_SUBACT_SHORT_DESC,
    B.* 
FROM
(
    select '1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31' 
    from dual
    UNION ALL
    select to_char(TO_DATE(:MONTH,'DD/MM/YYYY'),'DY'),
        to_char(TO_DATE(:MONTH ,'DD/MM/YYYY') + 1,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+2,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+3,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+4,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+5,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+6,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+7,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+8,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+9,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+10,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+11,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+12,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+13,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+14,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+15,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+16,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+17,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+18,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+19,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+20,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+21,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+22,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+23,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+24,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+25,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+26,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+27,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+28,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+29,'DY'),
        to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+30,'DY')
    from dual

) B
UNION ALL
SELECT LOGDATE,TRUNC(LOGDATE,'MM') as period,
    EMPCODE,
    PROJECTCODE,ACTIVITYCODE || '.' || SUBACTIVITY_CODE || '.' || SUB_SUBACTIVITY_CODE AS ACTIVITY,
    TOTALTIME ,ENTEREDBY, 
    PROJECTDESC,
    SUB_SUBACT_SHORT_DESC,
    CASE WHEN to_char(logdate,'dd') = '01' THEN to_char(TOTALTIME) END d1,
    CASE WHEN to_char(logdate,'dd') = '02' THEN to_char(TOTALTIME) END d2,
    CASE WHEN to_char(logdate,'dd') = '03' THEN to_char(TOTALTIME) END d3,
    CASE WHEN to_char(logdate,'dd') = '04' THEN TO_CHAR(TOTALTIME) END d4,
    CASE WHEN to_char(logdate,'dd') = '05' THEN TO_CHAR(TOTALTIME) END d5,
    CASE WHEN to_char(logdate,'dd') = '06' THEN TO_CHAR(TOTALTIME) END d6,
    CASE WHEN to_char(logdate,'dd') = '07' THEN TO_CHAR(TOTALTIME) END d7,
    CASE WHEN to_char(logdate,'dd') = '08' THEN TO_CHAR(TOTALTIME) END d8,
    CASE WHEN to_char(logdate,'dd') = '09' THEN TO_CHAR(TOTALTIME) END d9,
    CASE WHEN to_char(logdate,'dd') = '10' THEN TO_CHAR(TOTALTIME) END d10,
    CASE WHEN to_char(logdate,'dd') = '11' THEN TO_CHAR(TOTALTIME) END d11,
    CASE WHEN to_char(logdate,'dd') = '12' THEN TO_CHAR(TOTALTIME) END d12,
    CASE WHEN to_char(logdate,'dd') = '13' THEN TO_CHAR(TOTALTIME) END d13,
    CASE WHEN to_char(logdate,'dd') = '14' THEN TO_CHAR(TOTALTIME) END d14,
    CASE WHEN to_char(logdate,'dd') = '15' THEN TO_CHAR(TOTALTIME) END d15,
    CASE WHEN to_char(logdate,'dd') = '16' THEN TO_CHAR(TOTALTIME) END d16,
    CASE WHEN to_char(logdate,'dd') = '17' THEN TO_CHAR(TOTALTIME) END d17,
    CASE WHEN to_char(logdate,'dd') = '18' THEN TO_CHAR(TOTALTIME) END d18,
    CASE WHEN to_char(logdate,'dd') = '19' THEN TO_CHAR(TOTALTIME) END d19,
    CASE WHEN to_char(logdate,'dd') = '20' THEN TO_CHAR(TOTALTIME) END d20,
    CASE WHEN to_char(logdate,'dd') = '21' THEN TO_CHAR(TOTALTIME) END d21,
    CASE WHEN to_char(logdate,'dd') = '22' THEN TO_CHAR(TOTALTIME) END d22,
    CASE WHEN to_char(logdate,'dd') = '23' THEN TO_CHAR(TOTALTIME) END d23,
    CASE WHEN to_char(logdate,'dd') = '24' THEN TO_CHAR(TOTALTIME) END d24,
    CASE WHEN to_char(logdate,'dd') = '25' THEN TO_CHAR(TOTALTIME) END d25,
    CASE WHEN to_char(logdate,'dd') = '26' THEN TO_CHAR(TOTALTIME) END d26,
    CASE WHEN to_char(logdate,'dd') = '27' THEN TO_CHAR(TOTALTIME) END d27,
    CASE WHEN to_char(logdate,'dd') = '28' THEN TO_CHAR(TOTALTIME) END d28,
    CASE WHEN to_char(logdate,'dd') = '29' THEN TO_CHAR(TOTALTIME) END d29,
    CASE WHEN to_char(logdate,'dd') = '30' THEN TO_CHAR(TOTALTIME) END d30,
    CASE WHEN to_char(logdate,'dd') = '31' THEN TO_CHAR(TOTALTIME) END d31
FROM TIMESHEET
WHERE PROJECTCODE = 2274
    AND LOGDATE >= TO_DATE('01/08/2011','dd/mm/yyyy')
    AND LOGDATE <= TO_DATE('31/08/2011','dd/mm/yyyy')
    AND TOTALTIME>0

>Is this possible to group the results by Activity? So 'pojectx' will be activity 1.11.1

4

2 回答 2

0

像这样的东西应该可以解决问题。除了 Oracle 不稳定的日期函数之外,它是非常标准的 SQL,应该适用于大多数现代 SQL 实现(只要它们支持子查询作为from子句中的虚拟表):

select p.name as project_name  ,
       a.name as activity_name ,
       t.*
from ( select extract(year,t.year)   as year        ,
              extract(month,t.month) as month       ,
              t.project              as project_id  ,
              t.activity             as activity_id ,
              sum( case extract(day,t.month) when  1 then t.time     end ) as time_01        , -- summarize time by each day of the month 
              sum( case extract(day,t.month) when  2 then t.time     end ) as time_02        , -- days with no data will have NULL
              sum( case extract(day,t.month) when  3 then t.time     end ) as time_03        , --   |
              ...                                                                              --   |
              sum( case extract(day,t.month) when 28 then t.time     end ) as time_28        , --   |
              sum( case extract(day,t.month) when 29 then t.time     end ) as time_29        , --   |
              sum( case extract(day,t.month) when 30 then t.time     end ) as time_30        , --   |
              sum( case extract(day,t.month) when 31 then t.time     end ) as time_31        , --   >
              sum( t.activity                                            ) as total_activity ,
              sum( t.time                                                ) as total_time     ,
       from some_table t
       where extract(year,t.month) = 2012
       group by extract(year  , t.month ) ,
                extract(month , t.month ) ,
                t.project ,
                t.activity
     ) summary
join project  p on p.project_id  = summary.project_id
join activity a on a.activity_id = summary.activity_id
order by t.year  ,
         t.month ,
         p.name  ,
         a.name

这涵盖了所有年份、所有月份、所有项目和所有活动:在where虚拟表的子句中添加适当的条件以过滤源数据。

于 2012-08-14T16:54:43.347 回答
0

在做了一些研究并在此链接上的旧帖子的一些指导下,我得到了我正在寻找的结果。

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:124812348063

询问:

选择'日期'活动,'1','2','3','4','5','6','7','8','9','10','11', '12','13','14','15','16','17','18','19','20','21','22','23','24 ','25','26','27','28','29','30','31' , 0 总计

从双

联合所有

选择'DAYS'活动,to_char(TO_DATE(:MONTH,'DD/MM/YYYY'),'DY'),to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+ 1,'DY'), to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+2,'DY'), to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+3,'DY'), to_char(TO_DATE( :MONTH,'DD/MM/YYYY')+4,'DY'), to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+5,'DY'), to_char(TO_DATE(:MONTH,' DD/MM/YYYY')+6,'DY'), to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+7,'DY'), to_char(TO_DATE(:MONTH,'DD/MM/ YYYY')+8,'DY'), to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+9,'DY'), to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+ 10,'DY'), to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+11,'DY'), to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+12,'DY '), to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+13,'DY'), to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+14,'DY'), to_char(TO_DATE(:MONTH,'DD /MM/YYYY')+15,'DY'), to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+16,'DY'), to_char(TO_DATE(:MONTH,'DD/MM/YYYY ')+17,'DY'), to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+18,'DY'), to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+19 ,'DY'), to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+20,'DY'), to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+21,'DY' ), to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+22,'DY'), to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+23,'DY'), to_char( TO_DATE(:MONTH,'DD/MM/YYYY')+24,'DY'), to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+25,'DY'), to_char(TO_DATE(:MONTH ,'DD/MM/YYYY')+26,'DY'), to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+27,'DY'), to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+28,'DY'), to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+29,'DY'), to_char(TO_DATE(:MONTH,'DD/MM/YYYY')+30,'DY'),

0 总计

从双

联合所有

选择活动,

MAX(解码(DT,'01',HRS,NULL))c1,MAX(解码(DT,'02',HRS,NULL))c2,MAX(解码(DT,'03',HRS,NULL))c3 ,MAX(解码(DT,'04',HRS,NULL))c4,MAX(解码(DT,'05',HRS,NULL))c5,MAX(解码(DT,'06',HRS,NULL)) c6,MAX(解码(DT,'07',HRS,NULL))c7,MAX(解码(DT,'08',HRS,NULL))c8,MAX(解码(DT,'09',HRS,NULL) )c9,MAX(解码(DT,'10',HRS,NULL))c10,MAX(解码(DT,'11',HRS,NULL))c11,MAX(解码(DT,'12',HRS,NULL) ))c12,MAX(解码(DT,'13',HRS,NULL))c13,MAX(解码(DT,'14',HRS,NULL))c14,MAX(解码(DT,'15',HRS, NULL))c15,MAX(解码(DT,'16',HRS,NULL))c16,MAX(解码(DT,'17',HRS,NULL))c17,MAX(解码(DT,'18',HRS , NULL)) c18, MAX(解码 (DT, '19',HRS,NULL))c19,MAX(解码(DT,'20',HRS,NULL))c20,MAX(解码(DT,'21',HRS,NULL))c21,MAX(解码(DT, '22',HRS,NULL))c22,MAX(解码(DT,'23',HRS,NULL))c23,MAX(解码(DT,'24',HRS,NULL))c24,MAX(解码(DT , '25', HRS, NULL)) c25, MAX(DECODE (DT, '26', HRS, NULL)) c26, MAX(DECODE (DT, '27', HRS, NULL)) c27, MAX(DECODE ( DT,'28',HRS,NULL))c28,MAX(解码(DT,'29',HRS,NULL))c29,MAX(解码(DT,'30',HRS,NULL))c30,MAX(解码(DT, '31', HRS, NULL)) c31,MAX(解码(DT,'25',HRS,NULL))c25,MAX(解码(DT,'26',HRS,NULL))c26,MAX(解码(DT,'27',HRS,NULL))c27 ,MAX(解码(DT,'28',HRS,NULL))c28,MAX(解码(DT,'29',HRS,NULL))c29,MAX(解码(DT,'30',HRS,NULL)) c30,MAX(解码(DT,'31',HRS,NULL))c31,MAX(解码(DT,'25',HRS,NULL))c25,MAX(解码(DT,'26',HRS,NULL))c26,MAX(解码(DT,'27',HRS,NULL))c27 ,MAX(解码(DT,'28',HRS,NULL))c28,MAX(解码(DT,'29',HRS,NULL))c29,MAX(解码(DT,'30',HRS,NULL)) c30,MAX(解码(DT,'31',HRS,NULL))c31,

nvl(sum(hrs),0) 总计

(

SELECT * FROM ( SELECT to_char(LOGDATE,'dd') as DT,TO_CHAR(TOTALTIME) AS HRS, ACTIVITYCODE || '.' || SUBACTIVITY_CODE || '.' || SUB_SUBACTIVITY_CODE 作为活动

从工作表中 PROJECTCODE = 2274 AND LOGDATE >= TO_DATE('01/08/2011','dd/mm/yyyy') AND LOGDATE <= TO_DATE('31/08/2011','dd/mm/yyyy')和总时间> 0)按活动分组,DT,HRS

)

按活动分组

结果:

在此处输入图像描述

于 2012-08-15T05:26:34.923 回答