Solved

Display monthly report

Posted on 2006-07-04
4
1,230 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
Comment Utility

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
Comment Utility
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
Comment Utility
I increase the value points to 200 so that I can give to all contributors.
0
 
LVL 1

Author Comment

by:KG1973
Comment Utility
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.

Join & Write a Comment

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

744 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

16 Experts available now in Live!

Get 1:1 Help Now