Solved

oracle sql - occurance of day in month

Posted on 2014-10-30
4
242 Views
Last Modified: 2014-10-30
Please see the attached data which has two columns (dt and dy) which are generated through the query:

select
to_char(dt, 'Day') dy,
dt
from
(select 
(sysdate - 100) + rownum -1 dt 
from 
dual 
connect by level <= sysdate - (sysdate - 100) + 1)

Open in new window


I need to add into the query another column (calc_field) which gives the count of the number of times that day has happened in that month (up to that date).

 any help is appreciated
ee-example.xlsx
0
Comment
Question by:tonMachine100
[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
  • 2
4 Comments
 
LVL 16

Expert Comment

by:Wasim Akram Shaik
ID: 40413065
Try this..

select
to_char(dt, 'Day') dy,
dt,count(1) over(partition by to_char(dt,'Day'),to_char(dt,'Mon')) as Tot
from
(select 
(sysdate - 100) + rownum -1 dt 
from 
dual 
connect by level <= sysdate - (sysdate - 100) + 1);

Open in new window

0
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 40413068
select
to_char(dt, 'Day') dy,
dt,
ceil(to_number(to_char(dt,'dd'))/7) calc_field
from
(select
(sysdate - 100) + rownum -1 dt
from
dual
connect by level <= sysdate - (sysdate - 100) + 1)
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 40413073
40413065 doesn't work

The COUNT function will only be able to return counts of data in the results, which does not necessarily reflect the calendar.

For example, if you run the query today (Oct 30, 2014)  then there will only be one week of July data in the results thus producing

Friday         2014-07-25 08:11:05      1  

when it should be

Friday         2014-07-25 08:09:50      4


also, the partitioning isn't correct so even if you have a full month, the counts are for the entire month so you get results like this...

Friday         2014-09-12 08:11:05      4

which should be

Friday         2014-09-12 08:09:50      2


the simple division and CEIL function will work regardless of the result set.
0
 
LVL 16

Expert Comment

by:Wasim Akram Shaik
ID: 40413079
True Sdstuber. . forgot that scenario..!! Thanks for pointing.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
Suggested Courses

630 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