Hello,
I have changed a query from select to select distinct and I keep on getting an error that I can't seem to fix. The error states: Msg 145, Level 15, State 1, Line 1
ORDER BY items must appear in the select list if SELECT DISTINCT is specified. I have added all the categories from the select statement into the order by statement but I still get the error. Any help is appreciated.
I have changed a query from select to select distinct and I keep on getting an error that I can't seem to fix. The error states: Msg 145, Level 15, State 1, Line 1
ORDER BY items must appear in the select list if SELECT DISTINCT is specified. I have added all the categories from the select statement into the order by statement but I still get the error. Any help is appreciated.
Code:
SELECT DISTINCT
ar.UCI
,(case when ar.transagebucket < 1 then '0_30'
when ar.transagebucket = 1 then '31_60'
when ar.transagebucket = 2 then '61_90'
when ar.transagebucket = 3 then '91_120'
when ar.transagebucket = 4 then '121_150'
when ar.transagebucket = 5 then '151_180'
else 'Over180' end) as AgeBucket
,ar.PatName as PatientName
,ar.AcctNu as AccountNumber
,Convert (varchar(10),ar.dos,101) as Svc_Date
,ar.cptdisplay
,(ins.insdesc+ ' - ' +ins.insmne) as Insurance
---,Sum(ar.chgamt) as ChgAmt
,(ar.chgamt) as ChgAmt
---,ISNULL(Sum(pmt.pmtamt),0) as PmtAmt
---,Sum(ar.curbal) as CurBal
,(ar.curbal) as CurBal
FROM rpt_dat_ARDetail ar
INNER JOIN rpt_dic_Ins ins ON ar.insmne = ins.insmne AND ar.clntid = ins.clntid
--- LEFT JOIN rpt_dat_PmtDetail pmt ON ar.clntid = pmt.clntid AND ar.AcctNu = pmt.AcctNu And ar.dos = pmt.dos
WHERE ar.reportmonth ='5/1/2013' and ar.clntid =69
---GROUP BY ar.uci,ar.transagebucket,ar.PatName,ar.AcctNu,ar.dos,ar.cptdisplay,ins.insdesc,ins.insmne,ar.curbal,pmt.pmtamt
GROUP BY ar.UCI,ar.transagebucket,ar.PatName,ar.AcctNu,ar.dos,ar.cptdisplay,ins.insdesc,ins.insmne,ar.chgamt,ar.curbal
---ORDER BY ar.transagebucket ASC,ar.PatName ASC,ar.AcctNu,ar.dos ASC,ar.cptdisplay,ins.insdesc,ins.insmne,pmt.pmtamt;
ORDER BY ar.UCI,ar.transagebucket ASC,ar.PatName ASC,ar.AcctNu,ar.dos ASC,ar.cptdisplay,ins.insdesc,ins.insmne,ar.chgamt,ar.curbal;