Solved

Oracle Query Count by Week Start on Tuesday

Posted on 2015-02-19
3
347 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

920 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

16 Experts available now in Live!

Get 1:1 Help Now