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.
And finally, here is my entire VIEW.
I have tried several requests, but I cannot.
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!!