I am using SQL 2005. When I get the results of my select query I am getting duplicate rows. I have tried to use orderby and groupby with no luck. Finally I tried to use sum Also this did not work. Any help is appreciated.
Code:
SELECT
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
,ar.dos as Svc_Date
,ar.cptdisplay
,(ins.insdesc+ ' - ' +ins.insmne) as Insurance
,Sum(ar.chgamt) as ChgAmt
,ISNULL(case when adj.trantype = 2 and adj.adjcat = 'DEBIT' then Sum(adj.adjamt) END,0) as Debit
,Sum(pmt.pmtamt) as Pmt
,ISNULL(case when adj.trantype = 3 and adj.adjcat = 'ADMIN_ADJ' then Sum(adj.adjamt) END,0) as Adjustments
,ISNULL(case when adj.trantype = 3 and adj.adjcat = 'WRITE_OFF' then Sum(adj.adjamt) END,0) as WO
,Sum(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
INNER JOIN rpt_dat_PmtDetail pmt ON ar.clntid = pmt.clntid AND ar.AcctNu = pmt.AcctNu AND ar.dos = pmt.dos
INNER JOIN rpt_dat_AdjustmentDetail adj ON ar.clntid = adj.clntid AND ar.aid = adj.aid
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.chgamt,adj.trantype,adj.adjcat,ar.chgamt,adj.adjamt,pmt.pmtamt,ar.curbal
ORDER BY ar.uci,ar.transagebucket,ar.PatName,ar.AcctNu,ar.dos,ar.cptdisplay,ins.insdesc,ins.insmne,adj.trantype,adj.adjcat,ar.chgamt,adj.adjamt,pmt.pmtamt,ar.curbal;