February 21, 2013 at 1:21 pm
Hi,
I am trying to count distinct days for medical claims using the icn, from date of service and to date of service.
Im using the sql below to create a temp table/calendar.
When the select statement runs, it omits about 39 icns in my list. is there something in the join that could be counting these records out?
CREATE TABLE #Calendar2(Dt DATETIME NOT NULL PRIMARY KEY);
;WITH cte AS
(
SELECT CAST('20120101' AS DATETIME) AS c
UNION ALL
SELECT DATEADD(dd,1,c) FROM cte
WHERE c < '20131231'
)
INSERT INTO #Calendar2 SELECT c FROM cte OPTION (MAXRECURSION 0);
SELECT
ICN,
COUNT(DISTINCT Dt) AS Days
FROM
AllOPBYDOS s
INNER JOIN #Calendar2 c ON
s.[from Date of service] >= c.Dt AND s.[to Date of service] < c.Dt+1
Where [Detail Status Code] = 'p'
GROUP BY
ICN;
February 21, 2013 at 2:46 pm
Is it possible that they have multiple per one day? You are counting distinct days. Why not just do COUNT(*).
What duplicates are you trying to rid yourself of?
February 22, 2013 at 12:41 am
montecarlo2079 (2/21/2013)
When the select statement runs, it omits about 39 icns in my list. is there something in the join that could be counting these records out?
Is it possible that [from Date of service] or [to Date of service] is NULL for the missing icns?
montecarlo2079 (2/21/2013)
CREATE TABLE #Calendar2(Dt DATETIME NOT NULL PRIMARY KEY);;WITH cte AS
(
SELECT CAST('20120101' AS DATETIME) AS c
UNION ALL
SELECT DATEADD(dd,1,c) FROM cte
WHERE c < '20131231'
)
INSERT INTO #Calendar2 SELECT c FROM cte OPTION (MAXRECURSION 0);
Oh my! A recursive counting CTE to create your calendar table? Consider using a Tally table instead! Much faster.
;WITH Tally (n) AS (
SELECT TOP 731 ROW_NUMBER() OVER (ORDER BY (SELECT NULL))-0
FROM sys.all_columns a CROSS JOIN sys.all_columns b)
SELECT DATEADD(d, n, '2012-01-01')
FROM Tally
My thought question: Have you ever been told that your query runs too fast?
My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.
Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
February 22, 2013 at 6:42 am
Dwain,
I noticed the problem when I did a
select distinct icn count.
my total came to 264500
when I ran this query, I got 264461 total results. I went back and looked at the ones that are not in the list, the data looks exactly like the other claims. no difference and no null values in the fields.
So if I created a calendar using that tally, how do I query that calendar in the same way to see if I get better results?
my goal is to count the number of distinct days per ICN.
Here is how my data is set up.
ICNFrom Date of ServiceTo Date of ServiceDetail Status Code
1234520120810 12:00:0020120811 12:00:00P
1234520120801 12:00:0020120805 12:00:00P
1234520120810 12:00:0020120810 12:00:00P
1234520120813 12:00:0020120815 12:00:00P
The total distinct days on this ICN is 10 if you count correctly.
How can I use your tally table to count distinct Dates of Service in both from date of service and to date of service ?
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply