select CS.Ref,
CS.C,
DT.ID,
DT.Init,
DT.Surname,
'C' [P],
'C' [A],
100 [M],
case when Convert(Varchar(10),CA.A,120) is null
then Convert(Varchar(10),CS.DC,120)
else Convert(Varchar(10),CA.A,120) end,
C.R,
Convert(Varchar(10),C.CDate,120),
Do.Outcome,
CT.PhysicalOwner,
Convert(Varchar(10),CS.CDate,120),
Convert(decimal(8,2),CS.B,
Convert(decimal(8,2),CS.CB),
Convert(decimal(8,2),CS.IB),
Convert(decimal(8,2),CS.Calance),
Convert(money,SUM(dd.C)),
Convert(money,SUM(cl.C)),
CT.CID[Cust_ID],
Convert(varchar(10),CS.JDate,120),
CS.JCN,
CO.C,
Convert(varchar(10),CS.OD,120),
CS.OCN,
CO2.C
from table1 CS
inner join table2 DT
on DT.DID = CS.DID
and CS.DC is not null
inner join table3 DO
on DT.DID = DO.DID
inner join ltable3 CT
on CT.CID = CS.CID
and CT.CD in(10, 12)
left outer join table4 CA
on CA.CID = CS.CID
and CA.A not in('Gt')
inner join table5 C
on C.CID = CS.CID
and C.CRMID = (select top 1 MAX(CR.CRMID) from table5 CR where CR.CD = CS.CD)
left outer join table6 CO
on Co.CID = CS.JID
left outer join table7 CQ
on CQ.CID = CS.EID
left outer join ltable8 dd
on dd.ID = CS.aD
left outer join table8 FE
on cl.ID = C.ID
AND CL.CS = (SELECT MAX(CL2.CS) FROM table8 KM WHERE KM.ID = CL.ID
AND KM.LID IN (SELECT LT.LID FROM table9 LT WHERE LT.Te LIKE 'PM%'
or LT.Te in ('A', 'O')))
where (CA.A between isnull(date,CA.A) and isnull(Date,CA.A) OR CA.A IS null)
ERROR
Column 'Table' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I know that I need to add a GROUP BY, but when I do that I need to group so many tables, any the query takes a very long time to run...Is there a better way to do this?
Nay ideas will help alot.