1

I'm developing a CRM, that's why I created poles to separate my users.

I created a view which gathered all the information of a user.

I'm looking to know how to count the number of users logged in by pole.

In my VIEW I have a user_online field.

I also have a pole_name field.

I tried this request but it doesn't work.

SELECT pole_name, COUNT(user_online) AS nbr_online FROM `ViewProjet_userPoleRole` GROUP BY pole_name

Which gives me the total number of users of a pole and therefore not if it is online.

data

And finally, here is my entire VIEW.

VIEW

I have tried several requests, but I cannot.

Shadow
  • 33,525
  • 10
  • 51
  • 64

2 Answers2

1

I think you can use conditional aggregation as following:

SELECT
    POLE_NAME,
    SUM(CASE WHEN USER_ONLINE = 'Y' THEN 1 ELSE 0 END) AS NBR_ONLINE
FROM VIEWPROJET_USERPOLEROLE
GROUP BY POLE_NAME

or If you want to know only Poles with minimum one online user then put the condition in the WHERE clause as follows:

SELECT
    POLE_NAME,
    COUNT(1) AS NBR_ONLINE
FROM VIEWPROJET_USERPOLEROLE
WHERE USER_ONLINE = 'Y'
GROUP BY POLE_NAME

If you represent online user by some other kind of norms then use them in WHEN clause of the CASE statement accordingly.

Cheers!!

Popeye
  • 35,427
  • 4
  • 10
  • 31
1

Try this:

SELECT pole_name, COUNT(user_id) AS nbr_online 
FROM `ViewProjet_userPoleRole` 
WHERE user_online = 1
GROUP BY pole_name

Hope this will help

Or you can try the following if want to display both the online and offline count:

SELECT pole_name, SUM(if(user_online = 1,1,0)) as nbr_online, SUM(if(user_online = 0,1,0)) as nbr_offline 
FROM `ViewProjet_userPoleRole` 
GROUP BY pole_name

Try this.

Nikhil Gyan
  • 682
  • 9
  • 16