SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


tsql query for counting a 'grouped by' and a where clause


tsql query for counting a 'grouped by' and a where clause

Author
Message
mrichardson 57577
mrichardson 57577
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 83
Can anyone help with this please?
I am trying to count all users in a table who are superusers (only if there is more than 1 super user).

TABLE: 'SuperUs'
FIELDS: 'Role' and 'Employer'


SAMPLE:
ROLE - - - - - EMPLOYER - - - COUNT
Global_User | A Company Ltd | 2
Super_User | A Company Ltd | 2


I have part of this working already (if you see above it counts for every type of role).
This is the code I have got so far:
SELECT     Role, Employer, COUNT(*) AS cnt
FROM dbo.SuperUs
GROUP BY Role, Employer
HAVING (COUNT(Role) > 1)



However, what I'm trying to do now is only count if the 'Role' is a 'Super_User' and ignore other roles like global user.
Phil Parkin
Phil Parkin
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18573 Visits: 20442
mrichardson 57577 (8/7/2013)
Can anyone help with this please?
I am trying to count all users in a table who are superusers (only if there is more than 1 super user).

TABLE: 'SuperUs'
FIELDS: 'Role' and 'Employer'


SAMPLE:
ROLE - - - - - EMPLOYER - - - COUNT
Global_User | A Company Ltd | 2
Super_User | A Company Ltd | 2


I have part of this working already (if you see above it counts for every type of role).
This is the code I have got so far:
SELECT     Role, Employer, COUNT(*) AS cnt
FROM dbo.SuperUs
GROUP BY Role, Employer
HAVING (COUNT(Role) > 1)



However, what I'm trying to do now is only count if the 'Role' is a 'Super_User' and ignore other roles like global user.


SELECT     Role, Employer, COUNT(*) AS cnt
FROM dbo.SuperUs
WHERE Role = 'Super_User'
GROUP BY Role, Employer
HAVING (COUNT(Role) > 1)




Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
mrichardson 57577
mrichardson 57577
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 83
thanks, that's done the trick !
Phil Parkin
Phil Parkin
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18573 Visits: 20442
mrichardson 57577 (8/7/2013)
thanks, that's done the trick !


Great.


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
mrichardson 57577
mrichardson 57577
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 83
however... what seems to be harder to do is list data which has a count = 0 or <1.

e.g. we would also like to show which employers do not have a superuser.

But this isn't possible to show here because if there are no superusers in the 'group by' results - then they are not listed in the first place to count, so it always returns none.
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16306 Visits: 19552
You will need to include both tables in your query:

FROM employers
LEFT JOIN superusers

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

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
Exploring Recursive CTEs by Example Dwain Camps
mrichardson 57577
mrichardson 57577
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 83
I've managed to get thus far:
I've added my employer table and used a left join.

I can count all the users in total (sum of all users for that employer e.g. global users + superusers), but not just superusers.

using this code:

SELECT e.EmployerName, { fn IFNULL(x.super_userCnt, 0) } AS super_userCnt
FROM dbo.mytableEmployer e LEFT OUTER JOIN
(SELECT employerName, COUNT(*) AS super_userCnt
FROM employerSuper_Users
GROUP BY employerName) x ON e.EmployerName = x.employerName



if I add a where clause - it either says invalid column name or incorrect syntax
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16306 Visits: 19552
SELECT 
e.EmployerName,
super_userCnt = ISNULL(x.super_userCnt, 0)
FROM dbo.mytableEmployer e
LEFT OUTER JOIN (
SELECT
employerName,
super_userCnt = COUNT(*)
FROM employerSuper_Users
WHERE [Role] = 'Super_User'
GROUP BY employerName
HAVING COUNT(*) > 1
) x ON e.EmployerName = x.employerName



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

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
Exploring Recursive CTEs by Example Dwain Camps
mrichardson 57577
mrichardson 57577
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 83
Thanks a lot, that works!

(I just had a minor error on my code - I should have put if >0 and not 1)

also changed to COALESCE instead of ISNULL
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search