Solved

Display monthly report

Posted on 2006-07-04
4
1,231 Views
Last Modified: 2008-03-17
I have a table Monthly_activity with column Act_date, activity_detail
Date format : dd/mon/yyyy

1/Jan/2006 Activity1
2/Jan/2006 Activity2
3/Jan/2006 Activity3
1/Feb/2006 Activity1
2/Feb/2006 Activity2
1/Mar/2006 Activity1
1/May/2006 Activity1
1/Jun/2006 Activity1
... so on until 1/Dec/2006

Hence that there is NO activity in APRIL.
I want to produce a list as below.

Month     Sum_activity

January        3
February      2
March          1
April            0
May             1
... ..
December     1

In the list, activity for April still displayed even there is no record in April.
So how can I do this using SQL. My database is Oracle.

0
Comment
Question by:KG1973
  • 2
4 Comments
 
LVL 9

Assisted Solution

by:neo9414
neo9414 earned 100 total points
ID: 17040079

Try this...

select to_char(to_date(b.l,'mm'),'Month') Month, decode(a.present,null,b.cnt,a.cnt) Activities from (
select to_number(to_char(Act_date,'mm')) present,count(*) cnt from Monthly_activity
group by to_char(Act_date,'Month'),to_number(to_char(Act_date,'mm')))a,
(select level l, 0 cnt from dual connect by level <=12) b
where a.present(+) = b.l;

TEST
-----------

create table Monthly_activity(Act_date date, activity_detail varchar2(50));

insert into Monthly_activity values(to_date('01/jan/2006'),'Activity1');
insert into Monthly_activity values(to_date('02/jan/2006'),'Activity3');
insert into Monthly_activity values(to_date('03/jan/2006'),'Activity3');
insert into Monthly_activity values(to_date('01/feb/2006'),'Activity1');
insert into Monthly_activity values(to_date('03/feb/2006'),'Activity2');
insert into Monthly_activity values(to_date('01/mar/2006'),'Activity1');
insert into Monthly_activity values(to_date('01/may/2006'),'Activity1');
insert into Monthly_activity values(to_date('01/dec/2006'),'Activity1');

SQL>select to_char(to_date(b.l,'mm'),'Month') Month, decode(a.present,null,b.cnt,a.cnt) Activities from (
select to_number(to_char(Act_date,'mm')) present,count(*) cnt from Monthly_activity
group by to_char(Act_date,'Month'),to_number(to_char(Act_date,'mm')))a,
(select level l, 0 cnt from dual connect by level <=12) b
where a.present(+) = b.l;

MONTH     ACTIVITIES
--------- ----------
January            3
February           2
March              1
April              0
May                1
June               0
July               0
August             0
September          0
October            0
November           0

MONTH     ACTIVITIES
--------- ----------
December           1

12 rows selected.

cheers,
Neo
0
 
LVL 19

Accepted Solution

by:
actonwang earned 100 total points
ID: 17040085
assume your Act_date has datetype of DATE, you can use the following query:

select a.month "Month", nvl(a.sum_activity+b.sum_activity,0) "Sum_activity"
from
(
select to_char(add_months(trunc(sysdate,'YEAR'),rn-1),'Month') month,0 sum_activity
from (select rownum rn from dual connect by level <= 12)
) a left join
(
select to_char(Act_date,'Month') month, count(activity_detail) sum_activity
from Monthly_activity
group by to_char(Act_date,'Month')
) b on  a.month = b.month
order by to_date(a.month,'Month')
/
0
 
LVL 1

Author Comment

by:KG1973
ID: 17040592
I increase the value points to 200 so that I can give to all contributors.
0
 
LVL 1

Author Comment

by:KG1973
ID: 17040599
Hi neo9414 & actonwang.

Both of you gave me the right answer, but I prefer actonwang solution because it is easy for me to understand.
However neo9414 make us easy to test both scripts and his solution also right.

I am really appreciate your contributions.
Thanks guys.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Via a live example, show how to take different types of Oracle backups using RMAN.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now