kanmani

kanmani

  • NA
  • 1
  • 671

i want paymentamount column in desc order

Aug 28 2015 6:52 AM
Before Fetching 500 records only i want paymentamount to be sorted by descending order 
 
 query :
 
 
DECLARE @EffectivedtFrom AS DATETIME;
DECLARE @EffectivedtTo AS DATETIME;
SELECT @EffectivedtTo = MAX(EffectiveDate)
FROM claimsheader;
SELECT @EffectivedtFrom = DATEADD(mm, DATEDIFF(mm, 0, DATEADD(mm, -11, @EffectivedtTo)), 0);
--,Dense_Rank() OVER ( ORDER BY (select 1 )) AS Number
select HICN,max(PatientName) as PatientName,MAX(practicename) practicename,MAX(Deceased) Deceased,MAX(ESDR) ESDR
,MAX(Cancer) Cancer,MAX(Hospice) Hospice,SUM(PaymentAmount) PaymentAmount,MAX(EffectiveDate) EffectiveDate,MAX(Number) Number
from(select vw.HICN,vw.Firstname+' '+vw.lastname as PatientName,vw.practice practicename,
case when (d.DateOfDeath is null or d.DateOfDeath ='') then 'No'
else 'Yes' end as Deceased,
case when d.MedicareStatusCode in (11,21,31) then 'Yes'
else 'No' end as ESDR,
case when b.HCC in (11, 9, 10, 12, 8) then 'Yes'
else 'No' end as Cancer,
--case when (d.DateBenEnrolledInHospice is null or d.DateOfDeath ='') then 'No'
case when (d.DateBenEnrolledInHospice is null or d.DateBenEnrolledInHospice ='' or cast(DateBenEnrolledInHospice as datetime) not between @EffectivedtFrom and @EffectivedtTo) then 'No'
else 'Yes' end as Hospice,cast(vw.PaymentAmount as float) as PaymentAmount,vw.EffectiveDate ,
Dense_Rank() OVER ( ORDER BY (SELECT vw.HICN)) AS Number
from vwExpenditureDetails vw
inner join (select * from demographics aa where aa.DemographicsID in(select max(DemographicsID) from demographics group by hicn)) d on vw.HICN=d.HICN
left outer join icdhcc b on vw.PrincipalDiagnosisCode = b.ICD
where cast(vw.EffectiveDate as datetime) BETWEEN @EffectivedtFrom AND @EffectivedtTo AND vw.TaxID=@TIN
) aa where aa.Number between 1 and 500
group by HICN

Answers (1)