Solved

Oracle Query Count by Week Start on Tuesday

Posted on 2015-02-19
3
334 Views
Last Modified: 2015-02-19
I have a table that contains information on files received over time. One of the columns is FILE_DT.
I also have a query that returns the count of files received per week. Right now, it is set up to count files received (using FILE_DT) by a "standard" week - Sunday to Saturday. I would like to modify the query to count by a "non-standard" week - Tuesday to Monday.


Here is the query:

SELECT TRUNC (FILE_DT, 'W') FILE_DT,  COUNT ( * ) AllFiles
FROM files_received
WHERE  TRUNC (FILE_DT) between
    to_date('30/08/2011', 'dd/mm/yyyy') AND
   to_date('28/12/2015', 'dd/mm/yyyy')
GROUP BY TRUNC (FILE_DT, 'W')
ORDER BY TRUNC (FILE_DT, 'W')


Here is sample output:

WK_START       FILES
12/15/2014        80
12/22/2014        76
12/29/2014        45
1/1/2015        76
1/8/2015        83
1/15/2015        96
1/22/2015        93
1/29/2015        19
2/1/2015        97
2/8/2015        100
2/15/2015        83

As you can see, the group is identified using the week start date, which is currently a Sunday. I would like to change the week start to Tuesday, but if possible, KEEP the group label as Sunday, for convenience (so if the group starts on Tuesday 2/10/15, the associated group label would be Sunday 2/8/15). If that is not possible, then using the Tuesday date as a label is perfectly acceptable. The important thing is that the counts reflect a Tuesday to Monday grouping, instead of Sunday to Saturday.
0
Comment
Question by:bassman592
3 Comments
 
LVL 12

Expert Comment

by:FarWest
ID: 40619969
I think it is better to create a table that contains     3 columns week lable,week start . week end and use it in you query. You can easily create rows for all required years with a prober week labels
0
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 40620060
See if this works for you:
SELECT next_day(trunc(col1)-7,'TUESDAY')
	FILE_DT,  COUNT ( * ) AllFiles
FROM files_received
WHERE  FILE_DT >= to_date('30/08/2011', 'dd/mm/yyyy') AND
   file_dt < to_date('28/12/2015', 'dd/mm/yyyy')+1
GROUP BY next_day(trunc(col1)-7,'TUESDAY')
ORDER BY next_day(trunc(col1)-7,'TUESDAY')

Open in new window


Notice I removed the TRUNC in the where clause.  Unless you have a Function-Based Index in it, it will not use any index.  Assuming there is an index on FILE_DT.

>> but if possible, KEEP the group label as Sunday
I'm not sure why you want to call Tuesday Sunday.  Anyway, there are no labels in the query.
0
 

Author Closing Comment

by:bassman592
ID: 40620215
Perfect! Thank you.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
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…
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
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now