Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Display monthly report

Posted on 2006-07-04
4
Medium Priority
?
1,239 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 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

Independent Software Vendors: 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

From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
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…
Suggested Courses

783 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