I need to do a sum on the columns MTH1 MTH2 etc but for the parameters I pass it will produce six rows of information for the same account code which is correct and what it shou
You seem to simply want a GROUP BY
. But you can also simplify the query in other ways:
select nl.ACCOUNT_REF,
sum(nl.PRIOR_YR_MTH1) as PRIOR_YR_MTH1,
sum(nl.PRIOR_YR_MTH2) as PRIOR_YR_MTH2,
. . . - fill in the rest of the months
from NOMINAL_LEDGER nl join
CATEGORY c
on c.CompanyID = nl.CompanyID
where nl.ACCOUNT_REF = @AccountRef and
nl.SORT_ORDER = @SortOrder and
c.CATEGORY = @CATEGORY
group by nl.ACCOUNT_REF;
Notes:
WHERE
clause (presumably) undoes the LEFT JOIN
, turning it into an INNER JOIN
. So use the proper JOIN
.