![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| |||
| |||
|
|
Hi Alex, There are a couple of ways you could do this, but your first step should be to look up the ISNULL function in BOL ![]() Another thing - as you are returning a COUNT value (Integer), you will never be able to display 'No Comments' (String) as a result for this column as it is not the same type. You'll have to settle for 0, or convert the valid counts to strings which is probably not a good idea for your downstream logic. So sticking as close to your supplied query as possible, try: /* NOTE: All queries untested as no DDL supplied */ SELECT [TableA].* , CASE WHEN X.OldComments IS NULL THEN 0 ELSE X.OldComments END AS Comments FROM dbo.[TableA] LEFT OUTER JOIN ( SELECT EmployeePIN , COUNT(EmployeePIN) AS OldComments FROM dbo.Comments GROUP BY EmployeePIN ) X ON [TableA].PIN = X.EmployeePIN Another way using ISNULL: SELECT [TableA].* , ISNULL(X.OldComments, 0) AS Comments FROM dbo.[TableA] LEFT OUTER JOIN ( SELECT EmployeePIN , COUNT(EmployeePIN) AS OldComments FROM dbo.Comments GROUP BY EmployeePIN ) X ON [TableA].PIN = X.EmployeePIN My preferred way: SELECT [TableA].* , COUNT(c.EmployeePIN) AS Comments FROM dbo.[TableA] LEFT OUTER JOIN dbo.Comments c ON [TableA].PIN = c.EmployeePIN GROUP BY <[TableA].*> -- Column list not supplied Good luck! J |
#4
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |