0

I'm using this query

SELECT
     fullname,
     (SELECT 
          count([ID]) FROM [SDT-DB].[dbo].[tbl_InfoBoard_UserLogs] 
          WHERE fullname = fullname AND user_did ='login') AS [Login],
     (SELECT 
          count([ID]) FROM [SDT-DB].[dbo].[tbl_InfoBoard_UserLogs] 
          WHERE fullname = fullname AND user_did ='message') AS [View Messageboard],
     (SELECT 
          count([ID]) FROM [SDT-DB].[dbo].[tbl_InfoBoard_UserLogs] 
          WHERE fullname = fullname AND user_did ='notification') AS [View Notification],
     count([ID]) AS Count
FROM [SDT-DB].[dbo].[tbl_InfoBoard_UserLogs]
GROUP BY fullname

it seems its returning all userlogs...

1 Answer 1

2

Use conditional aggregation:

SELECT fullname,
       SUM(CASE WHEN user_did = 'login' THEN 1 ELSE 0 END) AS [Login],
       SUM(CASE WHEN user_did = 'message' AS [View Messageboard],
       SUM(CASE WHEN user_did = 'notification' THEN 1 ELSE 0 END) AS [View Notification],
       count([ID]) AS Count
FROM [SDT-DB].[dbo].[tbl_InfoBoard_UserLogs]
GROUP BY fullname;

Your logic doesn't work because you have no correlation clause. You seem to intend to have one with fullname = fullname, but that just evaluates to true whenever fullname is not NULL. But, the above is simpler.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.