Solved

Sql question

Posted on 2014-11-17
5
126 Views
Last Modified: 2014-11-29
Hi Experts,
I have the following table
TmpPatBillingCodes:

TmpPatBillingCodesID Number
EffectiveFrom Date
End Date

FYI-The first Column is not unique, its linked to another record of this table.
there are multiple records with same TmpPatBillingCodesID but different Dates.
Now I need to get a count of TmpPatBillingCodesID  grouped by datediff from effectivefrom-end 30 days, 60 days, 90 days and 90+
One more thing, All records containing the same TmpPatBillingCodesID are counted as 1 regarding the dates, meaning I need to get the date difference between min(EffectiveFrom) and Max(End) grouped by TmpPatBillingCodesID.
I would appreciate your help with this.
I am posting the sql that I have tried in Access, however it gives me an error that aggregate function in group by clause..
SELECT Count(TmpPatBillingCodes.TmpPatBillingCodesID) AS CountOfTmpPatBillingCodesID, Switch(DateDiff("d",Min([effectivefrom]),Max([end]))<31,"1-30",DateDiff("d",Min([effectivefrom]),Max([end])) Between 31 And 60,"31-60",DateDiff("d",Min([effectivefrom]),Max([end])) Between 61 And 90,"61-90",DateDiff("d",Min([effectivefrom]),Max([end])) Between 91 And 180,"91-180",DateDiff("d",Min([effectivefrom]),Max([end])) Between 181 And 365,"181-365",DateDiff("d",Min([effectivefrom]),Max([end]))>365,"365+") AS Expr2
FROM TmpPatBillingCodes
WHERE (((TmpPatBillingCodes.End) Between #1/1/2012# And #12/31/2012#))
GROUP BY Switch(DateDiff("d",Min([effectivefrom]),Max([end]))<31,"1-30",DateDiff("d",Min([effectivefrom]),Max([end])) Between 31 And 60,"31-60",DateDiff("d",Min([effectivefrom]),Max([end])) Between 61 And 90,"61-90",DateDiff("d",Min([effectivefrom]),Max([end])) Between 91 And 180,"91-180",DateDiff("d",Min([effectivefrom]),Max([end])) Between 181 And 365,"181-365",DateDiff("d",Min([effectivefrom]),Max([end]))>365,"365+");

Open in new window

 

I know it would be easier to get this done in separate queries, however since this is meant for a user to run it temporarily, I would prefer not to have to save queries therefore my goal here is to have this all in one query, so user can just copy and paste.

Thanks
0
Comment
Question by:bfuchs
  • 3
  • 2
5 Comments
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 40448567
You should study the weird and little known function Partition() here:

http://msdn.microsoft.com/en-us/library/office/gg264461(v=office.15).aspx

It might come in handy.

/gustav
0
 
LVL 3

Assisted Solution

by:bfuchs
bfuchs earned 0 total points
ID: 40449028
@Gustav
this seems a little to complicate (or weird as you called) for me to understand and implement..
however I managed to get this work with the following
select count(TmpPatBillingCodesID) ,expr2
from
(SELECT TmpPatBillingCodes.TmpPatBillingCodesID, Switch(DateDiff("d",Min([effectivefrom]),Max([end]))<31,"1-30",DateDiff("d",Min([effectivefrom]),Max([end])) Between 31 And 60,"31-60",DateDiff("d",Min([effectivefrom]),Max([end])) Between 61 And 90,"61-90",DateDiff("d",Min([effectivefrom]),Max([end])) Between 91 And 180,"91-180",DateDiff("d",Min([effectivefrom]),Max([end])) Between 181 And 365,"181-365",DateDiff("d",Min([effectivefrom]),Max([end]))>365,"365+") AS Expr2
FROM TmpPatBillingCodes
WHERE (((TmpPatBillingCodes.End) Between #1/1/2012# And #12/31/2012#))
GROUP BY TmpPatBillingCodes.TmpPatBillingCodesID) as t
group by expr2

Open in new window


Thanks,
Ben
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40449308
OK, but basically it does what your subquery does and probably a bit faster.

Partition is not difficult to use, it's just that you do it so rarely that you forget how. I think I have use it twice - and I have used Access since version 1.0.

/gustav
0
 
LVL 3

Author Closing Comment

by:bfuchs
ID: 40471415
thanks
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40471468
You are welcome!

/gustav
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

744 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

10 Experts available now in Live!

Get 1:1 Help Now