|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, February 22, 2013 11:43 AM
Points: 4,
Visits: 8
|
|
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;
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 11:00 AM
Points: 2,543,
Visits: 4,384
|
|
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
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, February 22, 2013 11:43 AM
Points: 4,
Visits: 8
|
|
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?
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 3:56 PM
Points: 1,324,
Visits: 1,778
|
|
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) One man with courage makes a majority. Andrew Jackson
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 5:13 AM
Points: 32,906,
Visits: 26,793
|
|
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."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Today @ 7:57 AM
Points: 68,
Visits: 315
|
|
select icn, DateDiff(dd,min(fromdt),isNull(max(todt),GETDATE())) daycnt from icns group by icn
|
|
|
|