Specific SQL query to count

Benjamin Peuple :

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.

Tejash :

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!!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=11567&siteId=1