sql server - Query inside CASE statement with SUM -


i have nestled query need use in case statement. need use max(agedate) correctly retrieved in select statement in case statement not see max(agedate) in select statement in case statement. please help...

select         inv.invoicenumber,        fact_fininvoice.invoiceid,        dbo.dim_date.fulldate invoicedate,        pra.practicename,        pra.practicelogonname,        pat.accno,        pat.fileno,        pmai.medicalaidnumber,        per.title + ' ' + per.initials + ' ' + per.surname patientname,        sch.schemes5,        sch.schemeoption, (select max(b.agedate) fact_fininvoice b fact_fininvoice.invoiceid = b.invoiceid , fact_fininvoice.practiceidkey = b.practiceidkey)  maxagedate,         sum(fact_fininvoice.amount) amount,        sum(fact_fininvoice.amountfunder) amountfunder,        sum(fact_fininvoice.amountpatient) amountpatient,          sum(case when datediff(dd, maxagedate, getdate()) between 0 , 29 , amountfunder != 0 amountfunder else 0 end) fundercurrent,        sum(case when datediff(dd, maxagedate, getdate()) between 30 , 59 , amountfunder != 0 amountfunder else 0 end) funder30days,         sum(case when datediff(dd, maxagedate, getdate()) between 60 , 89 , amountfunder != 0 amountfunder else 0 end) funder60days,        sum(case when datediff(dd, maxagedate, getdate()) between 90 , 119 , amountfunder != 0 amountfunder else 0 end) funder90days,        sum(case when datediff(dd, maxagedate, getdate()) >= 120 , amountfunder != 0 amountfunder else 0 end) funder120days,          sum(case when datediff(dd, maxagedate, getdate()) between 0 , 29 , amountpatient != 0 amountpatient else 0 end) patientcurrent,        sum(case when datediff(dd, maxagedate, getdate()) between 30 , 59 , amountpatient != 0 amountpatient else 0 end) patient30days,         sum(case when datediff(dd, maxagedate, getdate()) between 60 , 89 , amountpatient != 0 amountpatient else 0 end) patient60days,        sum(case when datediff(dd, maxagedate, getdate()) between 90 , 119 , amountpatient != 0 amountpatient else 0 end) patient90days,        sum(case when datediff(dd, maxagedate, getdate()) >= 120 , amountpatient != 0 amountpatient else 0 end) patient120days,          mcs.name bureaumanager,        mcso.name bureauofficer            dbo.fact_fininvoice left outer join                 dbo.dim_practice pra on dbo.fact_fininvoice.practiceidkey = pra.practiceidcode left outer join                 dbo.fact_invoice inv on dbo.fact_fininvoice.invoiceid = inv.id left outer join                 dbo.dim_personmainfo pmai  inner join                 dbo.dim_scheme sch  on sch.codeno = pmai.codeno on inv.personmainfocode = pmai.personmainfoid  left outer join                 dbo.dim_bureaustaffprovider bsp  inner join                 dbo.dim_medichargestaff mcs on mcs.id = bsp.bureaustaffid on bsp.providerid = inv.providerid inner join                 dbo.dim_medichargestaff mcso on mcs.manager = mcso.id left outer join                 dbo.dim_date on inv.invoicedatekey = dbo.dim_date.datekey left outer join                 dbo.dim_patient pat inner join                 dbo.dim_persondata per on per.persondataid = pat.personid on pat.patientid = inv.patientkey    group    inv.invoicenumber,             fact_fininvoice.invoiceid,             dbo.dim_date.fulldate,             pra.practicename,             pra.practicelogonname,             pmai.medicalaidnumber,             sch.schemes5,             sch.schemeoption,             mcs.name,             mcso.name,             pat.accno,             pat.fileno,             per.title + ' ' + per.initials + ' ' + per.surname,             [fact_fininvoice].practiceidkey  

use cte on top , join cte joins:

;with mycte ( select max(agedate) maxagedate,invoiceid  fact_fininvoice  group invoiceid )  select         inv.invoicenumber,        fact_fininvoice.invoiceid,        dbo.dim_date.fulldate invoicedate,        pra.practicename,        pra.practicelogonname,        pat.accno,        pat.fileno,        pmai.medicalaidnumber,        per.title + ' ' + per.initials + ' ' + per.surname patientname,        sch.schemes5,        sch.schemeoption,         maxagedate,        sum(fact_fininvoice.amount) amount,        sum(fact_fininvoice.amountfunder) amountfunder,        sum(fact_fininvoice.amountpatient) amountpatient,          sum(case when datediff(dd, maxagedate, getdate()) between 0 , 29 , amountfunder != 0 amountfunder else 0 end) fundercurrent,        sum(case when datediff(dd, maxagedate, getdate()) between 30 , 59 , amountfunder != 0 amountfunder else 0 end) funder30days,         sum(case when datediff(dd, maxagedate, getdate()) between 60 , 89 , amountfunder != 0 amountfunder else 0 end) funder60days,        sum(case when datediff(dd, maxagedate, getdate()) between 90 , 119 , amountfunder != 0 amountfunder else 0 end) funder90days,        sum(case when datediff(dd, maxagedate, getdate()) >= 120 , amountfunder != 0 amountfunder else 0 end) funder120days,          sum(case when datediff(dd, maxagedate, getdate()) between 0 , 29 , amountpatient != 0 amountpatient else 0 end) patientcurrent,        sum(case when datediff(dd, maxagedate, getdate()) between 30 , 59 , amountpatient != 0 amountpatient else 0 end) patient30days,         sum(case when datediff(dd, maxagedate, getdate()) between 60 , 89 , amountpatient != 0 amountpatient else 0 end) patient60days,        sum(case when datediff(dd, maxagedate, getdate()) between 90 , 119 , amountpatient != 0 amountpatient else 0 end) patient90days,        sum(case when datediff(dd, maxagedate, getdate()) >= 120 , amountpatient != 0 amountpatient else 0 end) patient120days,          mcs.name bureaumanager,        mcso.name bureauofficer            dbo.fact_fininvoice left outer join                 dbo.dim_practice pra on dbo.fact_fininvoice.practiceidkey = pra.practiceidcode left outer join                 dbo.fact_invoice inv on dbo.fact_fininvoice.invoiceid = inv.id left outer join                 dbo.dim_personmainfo pmai  inner join                 dbo.dim_scheme sch  on sch.codeno = pmai.codeno on inv.personmainfocode = pmai.personmainfoid  left outer join                 dbo.dim_bureaustaffprovider bsp  inner join                 dbo.dim_medichargestaff mcs on mcs.id = bsp.bureaustaffid on bsp.providerid = inv.providerid inner join                 dbo.dim_medichargestaff mcso on mcs.manager = mcso.id left outer join                 dbo.dim_date on inv.invoicedatekey = dbo.dim_date.datekey left outer join                 dbo.dim_patient pat inner join                 dbo.dim_persondata per on per.persondataid = pat.personid on pat.patientid = inv.patientkey                 join mycte ct                 on fact_fininvoice.invoiceid = ct.invoiceid  group    inv.invoicenumber,             fact_fininvoice.invoiceid,             dbo.dim_date.fulldate,             pra.practicename,             pra.practicelogonname,             pmai.medicalaidnumber,             sch.schemes5,             sch.schemeoption,             mcs.name,             mcso.name,             pat.accno,             pat.fileno,             per.title + ' ' + per.initials + ' ' + per.surname,             [fact_fininvoice].practiceidkey 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -