August 12, 2012 at 4:58 pm
I'm on a computer that don't SQL Server installed but you could try to use COUNT in an inner SELECT and then in an outer SELECT you could use something like (CASE WHEN total > 1 THEN 'MULTIPLE' ELSE 'SOMETHING ELSE' END). The "total" column would be the count result of the inner SELECT.
I hope I have made myself understandable enough. π
See if it works. π
Best regards,
Andre Guerreiro Neto
Database Analyst
http://www.softplan.com.br
MCITPx1/MCTSx2/MCSE/MCSA
August 12, 2012 at 5:04 pm
Does this work?
SELECT ta.ErrorID, tab.ErrorType, tab.DateLogged,
CASE WHEN total = 1 THEN tab.PolicyNumber ELSE 'MULTIPLE' END
FROM (SELECT tblErrors_ER.ErrorID, tblErrors_ER.ErrorType, tblErrors_ER.DateLogged,
MIN(tblPolicyNumbers.PolicyNumber) AS PolicyNumber,
COUNT(1) AS total
FROM tblErrors_ER
INNER JOIN tblPolicyNumbers_ER
ON tblErrors_ER.ErrorID = tblPolicyNumbers_ER.ErrorID
GROUP BY tblErrors_ER.ErrorID, tblErrors_ER.ErrorType, tblErrors_ER.DateLogged) tab
Best regards,
Andre Guerreiro Neto
Database Analyst
http://www.softplan.com.br
MCITPx1/MCTSx2/MCSE/MCSA
August 12, 2012 at 11:14 pm
Thanks for the help! Got it working with:
SELECT tblErrors_ER.ErrorID, CASE WHEN Count(tblPolicyNumbers_ER.PolicyNumber) = 1 THEN MIN(tblPolicyNumbers_ER.PolicyNumber) ELSE 'Multiple' END
FROM tblErrors_ER
INNER JOIN tblPolicyNumbers_ER ON
tblErrors_ER.ErrorID = tblPolicyNumbers_ER.ErrorID
GROUP BY tblErrors_ER.ErrorID
Thanks
Carl
August 13, 2012 at 8:03 am
Nice. That's almost the same thing without using a derived table.
I tend to use it whenever possible (or even CTEs) to make the code more readable. π
Have a great week!
Best regards,
Andre Guerreiro Neto
Database Analyst
http://www.softplan.com.br
MCITPx1/MCTSx2/MCSE/MCSA
August 13, 2012 at 8:08 am
carl6885 (8/12/2012)
Thanks for the help! Got it working with:SELECT tblErrors_ER.ErrorID, CASE WHEN Count(tblPolicyNumbers_ER.PolicyNumber) = 1 THEN MIN(tblPolicyNumbers_ER.PolicyNumber) ELSE 'Multiple' END
FROM tblErrors_ER
INNER JOIN tblPolicyNumbers_ER ON
tblErrors_ER.ErrorID = tblPolicyNumbers_ER.ErrorID
GROUP BY tblErrors_ER.ErrorID
Thanks
Carl
Hi Carl
Here's the same code using table aliases - it don't 'arf make it more readable π
SELECT
e.ErrorID,
CASE WHEN Count(pn.PolicyNumber) = 1 THEN MIN(pn.PolicyNumber) ELSE 'Multiple' END
FROM tblErrors_ER e
INNER JOIN tblPolicyNumbers_ER pn
ON e.ErrorID = pn.ErrorID
GROUP BY e.ErrorID
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 5 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply