Home » SQL & PL/SQL » SQL & PL/SQL » Frequency Distribution with future date place holders (SQL)
Frequency Distribution with future date place holders [message #636763] Fri, 01 May 2015 14:50 Go to next message
fluffycone
Messages: 27
Registered: June 2011
Junior Member
I need to create a resultset like the following. It is a two column result set of the date and the number of people who completed a task on that date.

Date, Count
Mon Apr-27,3
Tue Apr-28,58
Wed Apr-29,40
Thu Apr-30,0
Fri May-1,14
Sat May-2,0
...
Sat Jun-27,0
Sun Jun-28,0
Mon Jun-29,0
Tue Jun-30,0

My existing query looks like this:
  select to_char(completion_date, 'Dy, Mon-DD') as "Date",
       count(*) as "Count"
  from sat2015_imp_v
 where completion_date is not null
 group by completion_date
 order by completion_date
;


My table looks like this:
Emp_ID, completed (Y,N), Completion_date
1001, N, (null)
1002, Y, April 28, 2015
1003, N, (null)
1004, N, (null)
1005, N, (null)
1005, Y, April 29, 2015
...


The problem is that the result set only includes dates where an employee actually completed the task. So instead of getting rows with future dates as place holders and a value of 0, I get something like the following. Notice that Thursday is missing because no one completed the task on that date and the value should be 0.

Date, Count
Mon Apr-27,3
Tue Apr-28,58
Wed Apr-29,40
Fri May-1,14


The result set is used to generate a Frequency Distribution Chart/Graph.

Let me know if I need to provide more information.
Re: Frequency Distribution with future date place holders [message #636767 is a reply to message #636763] Fri, 01 May 2015 15:13 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
create or simulate a "calendar" table which contains all the dates you desire.
Re: Frequency Distribution with future date place holders [message #636768 is a reply to message #636763] Fri, 01 May 2015 15:31 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You did post your Oracle version and a test case in your previous topics, why didn't you do it in this one?


Re: Frequency Distribution with future date place holders [message #637029 is a reply to message #636768] Thu, 07 May 2015 15:24 Go to previous messageGo to next message
fluffycone
Messages: 27
Registered: June 2011
Junior Member
Sorry, my bad. Been away for a while performing "Other duties as assigned".

Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
PL/SQL Release 11.2.0.3.0 - Production
"CORE	11.2.0.3.0	Production"
TNS for Linux: Version 11.2.0.3.0 - Production
NLSRTL Version 11.2.0.3.0 - Production


See attachment for create Table Statement and Insert Statements. I left out the emp_id column.

CREATE TABLE "SAT2015" 
   (	"MODULE_1" VARCHAR2(255 BYTE), 
	"MODULE_1_COMPLETION_DATE" DATE, 
	"MODULE_2" VARCHAR2(255 BYTE), 
	"MODULE_2_COMPLETION_DATE" DATE
   ) ;


Column Descriptions:
Module_1: values Y OR N
Module_1_Completion_date: Date module was completed
Module_2: values Y OR N
Module_2_Completeion_date: Date module was completed

For staff members to be finished, column Module_1 must be 'Y' and column Module_2 must be 'Y'.

The only solution that I can think of is to create a table with one date column and generate a row from the start date to the end date. Is there a clear solution to do this dynamically, without having to physically create the table?

I did find this solution, but I can't figure it out past "select to_char()..." The "...connect by level..." stuff has me confused about how it works.
SQL> def date_start = '13/11/2010'
SQL> def date_end   = '22/11/2010'
SQL> with 
  2    data as (
  3      select to_date('&date_start', 'DD/MM/YYYY') date1,
  4             to_date('&date_end',   'DD/MM/YYYY') date2
  5      from dual
  6    )
  7  select to_char(date1+level-1, 'DD/MM/YYYY') the_date
  8  from data
  9  connect by level <= date2-date1+1
 10  /
THE_DATE
----------
13/11/2010
14/11/2010
15/11/2010
16/11/2010
17/11/2010
18/11/2010
19/11/2010
20/11/2010
21/11/2010
22/11/2010


I hope this clears it up a little better.
Re: Frequency Distribution with future date place holders [message #637032 is a reply to message #637029] Thu, 07 May 2015 15:38 Go to previous messageGo to next message
fluffycone
Messages: 27
Registered: June 2011
Junior Member
Found this (looks clean and simple):
http://stackoverflow.com/questions/418318/generate-a-range-of-dates-using-sql

SELECT TRUNC (SYSDATE - ROWNUM) dt
  FROM DUAL CONNECT BY ROWNUM < 366


So I modified it to look like this:

SELECT 
  rownum, 
  to_date('27-04-2015','DD-MM-YYYY') + rownum-1 as dt
FROM 
  DUAL 
CONNECT BY ROWNUM < 66


I still do not understand the "connect by" clause.
Can someone help explain this to me (if possible)?
Re: Frequency Distribution with future date place holders [message #637035 is a reply to message #637032] Thu, 07 May 2015 16:04 Go to previous messageGo to next message
fluffycone
Messages: 27
Registered: June 2011
Junior Member
FYI: More information about using the CONNECT BY Clause that I have found:

Generate random rows using SQL
https://cpaterson.wordpress.com/tag/connect-by-clause-with-rownum/
Re: Frequency Distribution with future date place holders [message #637039 is a reply to message #637035] Fri, 08 May 2015 01:15 Go to previous message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Have a look at row generator topic.
And Hierarchical Queries in Database SQL Reference; follow the links in the page, especially Hierarchical Query Examples.


Previous Topic: Processing Records in Cursor
Next Topic: Wrong number of arguments being passed into child element
Goto Forum:
  


Current Time: Fri Apr 19 10:15:52 CDT 2024