Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

30,60,90 Days Totals in sql query

$
0
0

Hi All,

Here I  have written a query to find the total for 30Days to calculate costs....

These 30Days Amount will be populated into another table based on joins.

I know we can do by case in the final update but lot of logic is going into temptables1 and 2, so please help...

select distinct ip.membernbr,ip.patcom,ip.admitdate,ip.dischargerdate,dateadd(day,1,ip.dishargedate) as afterdishcargedatestart,

dateadd(day,30,ip.dishargedate) as afterdishcargedateend, cd.specificdateofservice as ipdos, cd.specificdateofservicethry as ipdosthru

into #inpatientdoshospice30

from tbl_inpatient ip

join claimsdetail cd on cd.membernbr=ip.membernbr

join claims c on c.claimsseqnbr=cd.claimnsseqnbr

where c.formnbr='inp'

and cd.specificdateofservice between dateadd(day,1,ip.dishargedate) and dateadd(day,30,ip.dishargedate) 

and cd.specificdateofservicethru between dateadd(day,1,ip.dishargedate)  and dateadd(day,30,ip.dishargedate) 

--find part c hospice claims lines that that are already included in the inpatientcost(c.formnbr='INP') cost;

--doing this in a separate step in case there are multiple inpatient claims which dos dates that overlap (e.g; 1/15 to 1/20 and ---1/18 to 1/20) causing us to overstate dollars

select distinct ip.patcom,cd.claimssequencenbr

into #Hospiceinpatientclaimnslines30

from #inpatientdoshospice30 ip 

join claimsdetail cd on cd.membernbr=ip.membernbr

and cd.specificdateofservice between dateadd(day,1,ip.dishargedate)  and dateadd(day,30,ip.dishargedate) 

and cd.specificdateofservicethru between dateadd(day,1,ip.dishargedate)  and dateadd(day,30,ip.dishargedate) 

join claims c on c.claimsseqnbr=cd.claimnsseqnbr

where c.formnbr='Hospice'

--sum all hospice claims excluding those already included in the inpatient

update pp

set homehospicecost30day=isnull(sub.hospicecost,0)

from inpatient pp

left join(

select ip.patcom,sum(isnull(cd.netamt,0.00)) as hospicecost

from tbl_inpatient ip

join claimsdetail cd on cd.membernbr=ip.membernbr

and cd.specificdateofservice between dateadd(day,1,ip.dishargedate)  and dateadd(day,30,ip.dishargedate) 

and cd.specificdateofservicethru between dateadd(day,1,ip.dishargedate)  and dateadd(day,30,ip.dishargedate) 

join claims c on c.claimsseqnbr=cd.claimnsseqnbr

where (c.formnbr='Hospice' or (c.formnbr='PartB' and cd.placeofservice in ('34')))

and cd.claimsseqnbr not in (select cklaimseqnbr from #hospiceinpatientclaimlines30 sub where sub.patcom=ip.patcom)

group by ip.patcom

)sub

on pp.patcom=sub.patcom

I really appreciate your help.

Thanks,

Kalyan.


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>