Solved

Sql question

Posted on 2014-11-17
5
142 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

691 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