我试图以下列格式显示oracle数据(需要以报告的形式生成 - 可能是水晶报告);
表格包含以下字段:
到目前为止,我能够产生以下结果:
查询:
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
&gt;是否可以按活动对结果进行分组? 所以'pojectx'将是活动1.11.1
答案 0 :(得分: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
子句中添加适当的条件以过滤源数据。
答案 1 :(得分:0)
经过一些研究,并通过此链接上的旧帖子的一些指导,我得到了我正在寻找的结果。
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:124812348063
<强> QUERY:强>
选择 “DATE'ACTIVITY, '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 AS TOTAL
来自双重
UNION ALL
选择 “DAYS'ACTIVITY, TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY'), 'DY'), to_char(TO_DATE(:MONTH,'DD / MM / YYYY')+ 1,'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 2, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 3, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 4, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 5, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 6, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 7, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 8, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 9, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 10, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 11, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 12, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 13, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 14, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 15, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 16, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 17, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 18, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 19, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 20, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 21, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 22, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 23, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 24, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 25, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 26, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 27, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 28, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 29, 'DY'), TO_CHAR(TO_DATE(:本月 'DD / MM / YYYY')+ 30, 'DY'),
0 TOTAL
FROM DUAL
UNION ALL
选择活动,
MAX(DECODE(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(解码(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 ( 选择 to_char(LOGDATE,'dd')为DT,TO_CHAR(TOTALTIME)为HRS, ACTIVITYCODE || '' || SUBACTIVITY_CODE || '' || SUB_SUBACTIVITY_CODE AS ACTIVITY
FROM TIMSHEET PROJECTCODE = 2274 AND LOGDATE&gt; = TO_DATE('01 / 08/2011','dd / mm / yyyy') AND LOGDATE&lt; = TO_DATE('31 / 08/2011','dd / mm / yyyy') 和总时间> 0 ) 按活动分组,DT,HRS
)
GROUP BY ACTIVITY
<强>结果:强>