After several hours of trying to figure this out, I confess that I need help.
I have an SQL statement that performs a few joins based upon whether or not a value does not have duplicates in a combination of columns from the main table. For example, a customer account number can have several entries for any or all of three lines of business - vid, cdv, hsi. An account could be tracked once for vid and twice for cdv, but the statement would only grab the vid entry. Now that I have that ironed out, I find that I also need to test for the opposite condition - retrieving records for account numbers that have been tracked more than once for vid, cdv or hsi.
While this code does give me a list of all account numbers with the line of business that were tracked more than once, the results are grouped. I've tried altering the manner in which I'm testing for multiples and I've tried various Joins, etc. Not having any luck. Can anyone help out?
I have an SQL statement that performs a few joins based upon whether or not a value does not have duplicates in a combination of columns from the main table. For example, a customer account number can have several entries for any or all of three lines of business - vid, cdv, hsi. An account could be tracked once for vid and twice for cdv, but the statement would only grab the vid entry. Now that I have that ironed out, I find that I also need to test for the opposite condition - retrieving records for account numbers that have been tracked more than once for vid, cdv or hsi.
Code:
SELECT mr.tDate, mr.agentId agentA, mr.account, mr.lob, mr.subreq, mr.reason, mr.inPkg, mr.outPkg, dip.inPkgStatus, dop.outPkgStatus, dr.reasonStatus, ISNULL(mr.change, 0) AS change, ISNULL(mr.agentChanged, 0) AS agentB, ISNULL(au.dwn, 0) AS auditInPkg
FROM tbl_masterRetention mr
JOIN (SELECT account, lob FROM tbl_masterRetention GROUP BY account, lob HAVING COUNT(*) > 1 ) AS X
ON X.account = mr.account AND X.lob = mr.lob
JOIN tbl_dropInPkg dip ON mr.inPkg = dip.inPkgCode
JOIN tbl_dropOutPkg dop ON mr.outPkg = dop.outPkgCode
JOIN tbl_dropReasons dr ON mr.reason = dr.reasonCode
LEFT OUTER JOIN tbl_audit au ON au.inPkg = mr.inPkg AND au.outPkg = mr.outPkg
WHERE mr.subreq = 'XXX' AND mr.tDate BETWEEN (SELECT DATEADD(mm, DATEDIFF(mm,0,GETDATE()), 0)) AND GetDate() AND mr.agentId = '231'
ORDER BY mr.account, mr.lob, mr.tDate
While this code does give me a list of all account numbers with the line of business that were tracked more than once, the results are grouped. I've tried altering the manner in which I'm testing for multiples and I've tried various Joins, etc. Not having any luck. Can anyone help out?