Home » SQL & PL/SQL » SQL & PL/SQL » To display count for each day in a month (10g)
To display count for each day in a month [message #604947] |
Mon, 06 January 2014 07:02 |
|
367364
Messages: 10 Registered: July 2013 Location: Hyderabad
|
Junior Member |
|
|
HI,
I have a table audits
select * from audits;
date module application
------ ------- ------------
01-JAN-2014 X abc
01-JAN-2014 X abc
02-JAN-2014 Y cde
03-JAN-2014 X ghY
..........
I need a query like to display output as below. i mean the count for each day in a month
Please help me.
Module application 01-JAN-2013 02-jan-2013 03-jan-2013
------ ---------- ---------- ------------ -------------
X abc 2 0 0
X ghy 0 1 0
Y cde 0 0 1
|
|
|
|
|
Re: To display count for each day in a month [message #604953 is a reply to message #604952] |
Mon, 06 January 2014 08:34 |
|
367364
Messages: 10 Registered: July 2013 Location: Hyderabad
|
Junior Member |
|
|
select MODULE,
APPlication,
to_number(to_char(last_day(to_date(sysdate,'dd-mm-yyyy')),'dd')) ,
X.*
from ( select MODULE,APPlication, to_char( audit_date, 'dd' ) day, audit_date
from audit_1
where audit_date between to_char(trunc (sysdate, 'month'),'dd-mon-yyyy')
and TO_CHAR(LAST_DAY(sysdate),'dd-mon-yyyy')
pivot (sum(to_char(audit_date,'dd')) for day in
('01','02', '03','04', '05','06','07','08','09','10',
'11','12','13','14','15','16','17','18','19','20',
'21','22','23','24','25','26','27', '28','29','30','31')
) X
;
More over i need to get columns in date format like 01-jan-2014 02-jan-2014 ........
i need to get these dates dynamically
but with the above i am getting 01 02 03......
Please help me
[Updated on: Mon, 06 January 2014 08:53] Report message to a moderator
|
|
|
|
|
|
Re: To display count for each day in a month [message #604959 is a reply to message #604953] |
Mon, 06 January 2014 09:24 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
367364 wrote on Mon, 06 January 2014 09:34More over i need to get columns in date format like 01-jan-2014 02-jan-2014 ........
i need to get these dates dynamically
And what if you have data for several months/years? Keep in mind, table can have no more than 1000 columns. And, in any case, you can't do it dynamically. Usually "dynamic" pivot is used in reporting and most reporting tools have that capability. Are you using SQL*Plus? If so, you could use SQL*Plus substitution variables to construct and execute pivot statement.
SY.
|
|
|
|
|
|
|
|
|
|
|
Re: To display count for each day in a month [message #605043 is a reply to message #605041] |
Tue, 07 January 2014 02:29 |
pablolee
Messages: 2882 Registered: May 2007 Location: Scotland
|
Senior Member |
|
|
367364 wrote on Tue, 07 January 2014 08:14It doesn't have solution for dynamically populating of dates.
You realise that there were dozens of topics there? I will guarantee you that several of them contain the information that you need.
|
|
|
Re: To display count for each day in a month [message #605133 is a reply to message #605043] |
Tue, 07 January 2014 09:03 |
|
367364
Messages: 10 Registered: July 2013 Location: Hyderabad
|
Junior Member |
|
|
I created a function using ref cursor as below
create or replace function pivot_func
return SYS_REFCURSOR as
v_sql long;
v_refcur sys_refcursor;
v_firstdate date;
v_days number;
begin
select to_date(to_char(trunc (sysdate, 'month'),'dd-mon-yyyy'))
into v_firstdate
from dual;
V_DAYS := TRUNC(TO_CHAR(SYSDATE,'DD'));
v_sql :=
' select *
from (select module_name,
app_name,
audit_date
from audit_trail
where audit_date between to_char(trunc (sysdate, '||''''||'month'||''''||'),'||''''||'dd-mon-yyyy'||''''||')
and TO_CHAR(LAST_DAY(sysdate),'||''''||'dd-mon-yyyy'||''''||'))
pivot (sum(to_char(audit_date,'||''''||'dd-mm-yy'||''''||')) for audit_date in (';
for i in 1..v_days
LOOP
v_sql := v_sql ||''''||v_firstdate ||''''||',';
V_FIRSTDATE := V_FIRSTDATE + 1;
End Loop ;
V_SQL := RTRIM (V_SQL, ',') || ')) ';
dbms_output.put_line(V_SQL);
open v_refcur for v_sql;
Return V_Refcur;
close v_refcur;
end pivot_func;
i will get the sql as
select *
from (select module_name,
app_name,
audit_date
from METRICS_AUDIT_TRAIL1
where audit_date between to_char(trunc (sysdate, 'month'),'dd-mon-yyyy')
and TO_CHAR(LAST_DAY(sysdate),'dd-mon-yyyy'))
pivot (sum(to_char(audit_date,'dd')) for audit_date in ('01-JAN-14','02-JAN-14','03-JAN-14','04-JAN-14','05-JAN-14','06-JAN-14','07-JAN-14'))
But my problem is how can i sum up the count of dates here for each day in a month
the query above is giving wrong results
How can i use decode in pivot to get the count of dates for each day
I all the examples they have taken the count of some number....but not with the date
please help me.
*BlackSwan added {code} tags. Please do so yourself in the future.
[Updated on: Tue, 07 January 2014 09:22] Report message to a moderator
|
|
|
|
Re: To display count for each day in a month [message #605431 is a reply to message #605140] |
Thu, 09 January 2014 06:23 |
|
367364
Messages: 10 Registered: July 2013 Location: Hyderabad
|
Junior Member |
|
|
Hi,
i am able to get the count for each day but the issue is i am getting columns in sinqle quotes, how can i replace it.
select *
from (select module_name,
app_name,
audit_date
from audits
where audit_date between to_char(trunc (sysdate, 'month'),'dd-mon-yyyy')
and TO_CHAR(LAST_DAY(sysdate),'dd-mon-yyyy'))
pivot (count(APP_NAME) for audit_date in ('01-JAN-14','02-JAN-14','03-JAN-14','04-JAN-14','05-JAN-14','06-JAN-14','07-JAN-14')) ;
I am getting output as
Module_name '01-jan-14' '02-jan-14' '03-jan-14' '04-jan-14' '05-jan-14'
------------ ----------- ----------- ------------ ----------- ------------
abc 0 2 3 5 6
def 2 3 0 0 0
But i want output as below removing quotes in the columns
Module_name 01-jan-14 02-jan-14 03-jan-14 04-jan-14 05-jan-14
--------- ----------- -------- ------------ ----------- ------------
abc 0 2 3 5 6
def 2 3 0 0 0
Please help me.
[Updated on: Thu, 09 January 2014 06:24] Report message to a moderator
|
|
|
|
Re: To display count for each day in a month [message #605474 is a reply to message #605431] |
Thu, 09 January 2014 12:43 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
367364 wrote on Thu, 09 January 2014 07:23But i want output as below removing quotes in the columns
Just read documentation - you can alias pivot FOR list values:
for audit_date in ('01-JAN-14' "01-JAN-14",'02-JAN-14' "02-JAN-14",'03-JAN-14' "03-JAN-14",'04-JAN-14' "04-JAN-14",'05-JAN-14' "05-JAN-14",'06-JAN-14' "06-JAN-14",'07-JAN-14' "07-JAN-14")) ;
SY.
|
|
|
Goto Forum:
Current Time: Wed Apr 24 08:42:11 CDT 2024
|