So I have this:
const string sqlString =
"SELECT * FROM Causes c INNER JOIN CauseMembers m ON " +
"(c.Id = m.CauseId) AND (UserName = @UserName) " +
"ORDER BY c.DateTime DESC;";
Works fine, however I’d like to return a generated column containing the number of rows returned from this query from the table causemembers. So that I can get the actual members who joined this ’cause’ (from Causes).
Your inputs? Thanks
If you’re using SQL Server 2005 or higher, then you can use a window function:
This will get you a count of the number of rows returned per cause ID. If you want to ensure that every cause is returned, whether any members belong to it or not, then change the
JOINto aLEFT OUTER JOIN. If there are no CauseMembers, then theNULLfrom the outer join will not be counted, so you’ll get zero.