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 being dropped in query Expand / Collapse
Author
Message
Posted Thursday, February 21, 2013 1:23 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 #1422774
Posted Friday, February 22, 2013 5:28 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, July 4, 2014 3:55 AM
Points: 2,836, Visits: 5,062
We have no idea what data you have in your tables, so only the guess:
You join on dates, so it may be that some dates in your expected data are such that they are not covered by the join.


_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1422998
Posted Friday, February 22, 2013 6:39 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, February 28, 2014 7:01 AM
Points: 10, Visits: 15
The table has 4 columns

ICN, From Date of service, To date of service, and detail status code
123,1/1/12 ,1/5/12 ,P
123,1/6/12 ,1/6/12 ,p
123,1/1/12 ,1/10/12 ,p


For each ICn I need to count only the distinct days per icn. in this example above, the answer would be 10. I dont want to count each day, but want the count of distinct days per ICN.

does that help?
Post #1423022
Posted Friday, February 22, 2013 11:33 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 2:20 PM
Points: 1,959, Visits: 2,893

is there something in the join that could be counting these records out?


Maybe in some cases "s.[to Date of service]" is NULL to indicate "no end date"?


SQL DBA,SQL Server MVP('07, '08, '09)
"In America, every man is innocent until proven broke!" Brant Parker
Post #1423187
Posted Friday, February 22, 2013 1:54 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:19 PM
Points: 36,724, Visits: 31,173
montecarlo2079 (2/22/2013)
The table has 4 columns

ICN, From Date of service, To date of service, and detail status code
123,1/1/12 ,1/5/12 ,P
123,1/6/12 ,1/6/12 ,p
123,1/1/12 ,1/10/12 ,p


For each ICn I need to count only the distinct days per icn. in this example above, the answer would be 10. I dont want to count each day, but want the count of distinct days per ICN.

does that help?


Not really. We don't know what the datatyes are, etc.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1423253
Posted Friday, February 22, 2013 7:09 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: 2 days ago @ 1:16 PM
Points: 107, Visits: 511
select icn, DateDiff(dd,min(fromdt),isNull(max(todt),GETDATE())) daycnt from icns
group by icn
Post #1423309
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse