?
Solved

Sql question

Posted on 2014-11-17
5
Medium Priority
?
147 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 51

Accepted Solution

by:
Gustav Brock earned 2000 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 51

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 51

Expert Comment

by:Gustav Brock
ID: 40471468
You are welcome!

/gustav
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

752 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