I am working on a sql statement to collect the top 50 highest patient balances per provider. I am running against a MSSQL2005 db.
At present I have the following, which returns the top 50 for the whole database, not per provider, how do I change this to collect the 50 highest balances per provider?
Any help is appreciated.
MH
select sum(c.pat_amt) as PatBal,
replace(ltrim(replace(pat.med_rec_nbr, '0', ' ')), ' ', '0') as Chart#,
lp.desc_30 as Provider
into #PatBal
from charges c
inner join lrdc_providers lp on lp.provider_id = c.rendering_id
inner join patient pat on pat.person_id = c.person_id
inner join person per on per.person_id = pat.person_id
where c.pat_amt > 0.00
group by lp.desc_30,pat.med_rec_nbr
order by sum(c.pat_amt) desc
select top(50) PatBal,chart#,provider
from #PatBal
group by provider,chart#
At present I have the following, which returns the top 50 for the whole database, not per provider, how do I change this to collect the 50 highest balances per provider?
Any help is appreciated.
MH
select sum(c.pat_amt) as PatBal,
replace(ltrim(replace(pat.med_rec_nbr, '0', ' ')), ' ', '0') as Chart#,
lp.desc_30 as Provider
into #PatBal
from charges c
inner join lrdc_providers lp on lp.provider_id = c.rendering_id
inner join patient pat on pat.person_id = c.person_id
inner join person per on per.person_id = pat.person_id
where c.pat_amt > 0.00
group by lp.desc_30,pat.med_rec_nbr
order by sum(c.pat_amt) desc
select top(50) PatBal,chart#,provider
from #PatBal
group by provider,chart#