I’m starting with this query:
SELECT TOP 1 Parties.FirstName + ' ' + Parties.MiddleName + ' ' + Parties.LastName AS Plaintiffs
FROM Jackets INNER JOIN
JacketPartyLinks ON Jackets.Id = JacketPartyLinks.Jacket_JacketPartyLink INNER JOIN
Parties ON Parties.Id = JacketPartyLinks.JacketPartyLink_Party
WHERE (Jackets.Id = @JacketID) AND (JacketPartyLinks.Role = 0)
What I’d like to accomplish is to take a count of what’s returned, and if it’s one row, do nothing. If it’s more than one row, add something like “, et al”. Notice that this query returns a single column, and it while it should be able to run standalone, it also needs to work as a subquery.
How can this be done?
EDIT: Add TOP 1
Use
topandcount..overand then wrap it in an outer query, like so:The
over ()part aftercounttells it to count all rows and notgroup byanything. This is why there isn’t agroup byclause in that query. You can useoverwith any aggregate function, which is pretty nifty.