I'm attempting to retrieve values joined from 6 different tables and further filtered based upon values in 2 different columns using WHERE IN. The statements worked independently as expected, but when I combine them into one I get an error about the number of arguments in my final SELECT. I understand why I'm getting it, but it seems there should be some way to accomplish the task. I need to select records where the the account occurs only once for a specific lob. Can someone tell me what I need to do to tweak this out?
SELECT mr.tDate, mr.account, mr.lob, mr.subreq, mr.reason, mr.inPkg, mr.outPkg, dip.inPkgStatus, dop.outPkgStatus, dr.reasonStatus, mr.comp, mr.comments, mr.change, au.dwn AS auditInPkg
INTO #once
FROM tbl_masterRetention mr
INNER JOIN tbl_dropInPkg dip ON mr.inPkg = dip.inPkgCode
INNER JOIN tbl_dropOutPkg dop ON mr.outPkg = dop.outPkgCode
INNER JOIN tbl_dropReasons dr ON mr.reason = dr.reasonCode
LEFT JOIN tbl_audit au ON au.inPkg = mr.inPkg AND au.outPkg = mr.outPkg
WHERE mr.account IN
(SELECT account, lob FROM tbl_masterRetention GROUP BY account, lob HAVING COUNT(account) = 1 AND COUNT(lob) = 1)
SELECT mr.tDate, mr.account, mr.lob, mr.subreq, mr.reason, mr.inPkg, mr.outPkg, dip.inPkgStatus, dop.outPkgStatus, dr.reasonStatus, mr.comp, mr.comments, mr.change, au.dwn AS auditInPkg
INTO #once
FROM tbl_masterRetention mr
INNER JOIN tbl_dropInPkg dip ON mr.inPkg = dip.inPkgCode
INNER JOIN tbl_dropOutPkg dop ON mr.outPkg = dop.outPkgCode
INNER JOIN tbl_dropReasons dr ON mr.reason = dr.reasonCode
LEFT JOIN tbl_audit au ON au.inPkg = mr.inPkg AND au.outPkg = mr.outPkg
WHERE mr.account IN
(SELECT account, lob FROM tbl_masterRetention GROUP BY account, lob HAVING COUNT(account) = 1 AND COUNT(lob) = 1)