Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Rows not being accounted for in temp table results Expand / Collapse
Author
Message
Posted Thursday, February 21, 2013 1:21 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, February 28, 2014 7:01 AM
Points: 10, Visits: 15
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;
Post #1422771
Posted Thursday, February 21, 2013 2:46 PM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Monday, August 4, 2014 9:29 PM
Points: 459, Visits: 428
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?
Post #1422801
Posted Friday, February 22, 2013 12:41 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 3:30 AM
Points: 3,420, Visits: 5,348
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!
Post #1422896
Posted Friday, February 22, 2013 6:42 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, February 28, 2014 7:01 AM
Points: 10, Visits: 15
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 ?


Post #1423024
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse