Solved

Sql question

Posted on 2014-11-17
5
140 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
[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
  • 3
  • 2
5 Comments
 
LVL 50

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 4

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 50

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 4

Author Closing Comment

by:bfuchs
ID: 40471415
thanks
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 40471468
You are welcome!

/gustav
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

737 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