Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Display monthly report

Posted on 2006-07-04
4
Medium Priority
?
1,238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 9

Assisted Solution

by:neo9414
neo9414 earned 400 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 400 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
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
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

705 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