Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Rows not being accounted for in temp table results


Rows not being accounted for in temp table results

Author
Message
montecarlo2079
montecarlo2079
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 18
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;
dkschill
dkschill
SSC-Addicted
SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)

Group: General Forum Members
Points: 459 Visits: 430
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?
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4253 Visits: 6431
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 mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
montecarlo2079
montecarlo2079
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 18
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.

ICN From Date of Service To Date of Service Detail Status Code
12345 20120810 12:00:00 20120811 12:00:00 P
12345 20120801 12:00:00 20120805 12:00:00 P
12345 20120810 12:00:00 20120810 12:00:00 P
12345 20120813 12:00:00 20120815 12:00:00 P


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 ?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search