Avatar of bfuchs
bfuchsFlag for United States of America

asked on 

Sql question

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
Microsoft AccessSQL

Avatar of undefined
Last Comment
Gustav Brock
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of bfuchs
bfuchs
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

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
Avatar of bfuchs
bfuchs
Flag of United States of America image

ASKER

thanks
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

You are welcome!

/gustav
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo