Solved

Display monthly report

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
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…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
Suggested Courses

617 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