How can I get the count of distinct userID with some condition - mysql

I want to query for each distinct user and their count which logged In as Yes
Userid loggedIn
---- -----
1 Yes
1 yes
1 No
2 Yes
2 No
3 No
4 yes
5 yes
1 No
Output Should be
Userid Count
---- -----
1 2
2 1
3 0
4 1
5 1

with main as (
select distinct Userid from <table>
)
select
main.Userid,
count(*) as total
from main
left join <table_name>
on main.Userid = <table_name>.Userid
and loggedIn = 'yes'
group by main.Userid
or
select
user_id,
sum(case when lower(loggedIn) = 'yes' then 1 end) as count_
from <table_name>
group by 1

Related

mysql sum, CASE WHEN with GROUP BY

In MySQL, I want to sum the values ​​of certain statuses and display them in the same count column,
Do I have to give a condition to count ?!
status is 0, 1, 2 and 3, and status = 2 is a sum of 2, 3, 4 count values.
What kind of conditions should I give?
My Query:
SELECT A.STATUS, B.COUNT, B.REG_DT FROM
(SELECT 0 AS STATUS UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4) A
LEFT JOIN (
SELECT STATUS , COUNT(*) AS COUNT, FROM TB_EXE WHERE reg_dt >= CURDATE()
GROUP BY STATUS
) B ON A.STATUS = B.STATUS
My Data:
status | count
-----------------
0 | 1
-----------------
1 | 2
-----------------
2 | 1
-----------------
3 | 0
-----------------
4 | 2
Expected Results:
status | count
-----------------
0 | 1
-----------------
1 | 2
-----------------
2 | 3
SELECT STATUS,COUNT(*)
FROM T
WHERE STATUS < 2
GROUP BY STATUS
UNION ALL
(SELECT 2,COUNT(*)
FROM T
WHERE STATUS >= 2
)
Where the 2 aggregations are dealt with separately.
+--------+----------+
| STATUS | COUNT(*) |
+--------+----------+
| 0 | 1 |
| 1 | 1 |
| 2 | 3 |
+--------+----------+
3 rows in set (0.00 sec)
Or more succinctly
select case when status > 1 then 2 else status end, count(*)
from t
group by case when status > 1 then 2 else status end
you can try like below using case when
select case when status>1 then 2 else status end as status,
sum(count) as cnt from t
group by status
Hmmm, I think I'd go with a framing solution on this one. IE something like this:
SELECT
Status,
Count,
SUM(Count) OVER(ORDER BY status ROWS
BETWEEN UNBOUNDED PRECEDING
AND CURRENT ROW) AS FramedCount
FROM Status
this gives you a running total of the counts from all previous rows as the framed count. You can handle the logic in the application to determine which statuses should use the framed count OR you could handle it in the query by adding the following.
SELECT
status,
CASE
when status <= 3 THEN count
ELSE SUM(count) OVER(ORDER BY status ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
END AS 'MyCount'
FROM statusInfo

MYSQL count only those students with number 1

i would like construct a query to count the number of student who don't have number 0.
assuming i have 2 tables
students
*student_id
*name
Number
*number_id
*student_id
*number
| Student 1 |
1
1
1
1
| Student 2 |
1
1
1
1
| Student 3 |
1
0
1
0
|Student 4 |
0
1
1
1
So the result should be. student without zero = 2
student with zero = 2
This answer assumes that your table has the following structure:
id | value
1 | 1
1 | 1
1 | 1
1 | 1
2 | 1
etc...
SELECT id
FROM yourTable
GROUP BY id
HAVING SUM(CASE WHEN value = 0 THEN 1 ELSE 0 END) = 0
Something like this might return the specified result:
SELECT CONCAT('student without zero = '
, ( SELECT SUM(1)
FROM ( SELECT t.`some_identifier`
FROM `sometable` t
GROUP BY t.`some_identifier`
HAVING SUM(IF(t.`some_col`=0,1,0)) = 0
)
)
, '\n'
, 'student with zero = '
, ( SELECT SUM(1)
FROM ( SELECT z.`some_identifier`
FROM `sometable` z
WHERE z.`some_col` = 0
GROUP BY z.`some_identifier`
)
)
) AS `result`
Just replace some_table with the name of the table, some_col with the name of the column that contains the 1 and 0 values, and some_identifier with the column that contains the Student 1, Student 2, etc. values.
This should return a single row, something like this:
result
--------------------------------
student without zero = 2
student with zero = 2

MySQL Count All Rows and Count All Rows Where field1 = 1

How can I count all rows where game_id = 1 and count all rows where userid = 2 has hit = 1 on that game_id (1)
id userid game_id hit score date
1 2 1 1 1 null
2 2 1 0 2 null
3 2 1 1 3 null
4 4 1 1 1 null
5 2 1 1 4 null
6 2 1 0 5 null
7 2 2 1 1 null
and more rows
For above db information
We should have
6 rows for the game_id = 1
and 3 for userid = 2 and hit = 1
The result returned back should be 6, 3
Use CASE expression.
If you want each count in different columns, use the below sql query.
Query
select
count(case game_id when 1 then 1 end) as count1,
count(case when game_id = 1 and userid = 2 and hit = 1 then 1 end) as count2
from tblGame;
Output
+--------+--------+
| count1 | count2 |
+--------+--------+
| 6 | 3 |
+--------+--------+
else if you want combine the counts with a comma then use CONCAT in the query.
Query
select concat(t.count1,', ',t.count2) as `count` from
(
select
count(case game_id when 1 then 1 end) as count1,
count(case when game_id = 1 and userid = 2 and hit = 1 then 1 end) as count2
from tblGame
)t;
Output
+-------+
| count |
+-------+
| 6, 3 |
+-------+
SQL Fiddle Demo
use MySQL count
select (select count(*) from table where where game_id = 1) as count1,
(select count(*) from table where userid = 2 and hit = 1) as count2
from table limit 1
Try this MySQL query :-
`SELECT COUNT(*) FROM db_table WHERE game_id = 1`
and `SELECT COUNT(*) FROM db_table
WHERE userid = 2 AND hit = 1`
Try this MySQL query:
SELECT COUNT(userid) as totalusers, (SELECT COUNT(*) FROM ForgeRock
where game_id = 1 ) as totalgame FROM ForgeRock where game_id = 1 and
userid = 2 and hit = 1
SQL Fiddle

Rows to columns in mysql

I'm doing the select:
select id,
status,
count(status) as qtd
from user
group by id, status;
The return is:
id | status | qtd
1 YES 5
1 NO 3
2 YES 3
2 NO 1
I want this:
id | YES | NO
1 5 3
2 3 1
Thanks.
NOTE:
you can use case logic to do what you want.. basically you want to pivot the results and to pivot them you have to use aggregates with conditionals to fake a pivot table since mysql doesn't have a way to accomplish that
QUERY:
SELECT
id,
SUM(CASE status WHEN 'Yes' THEN 1 ELSE 0 END) as 'YES',
SUM(CASE status WHEN 'No' THEN 1 ELSE 0 END) as 'NO'
FROM user
GROUP BY id;
DEMO
OUTPUT:
+----+-----+----+
| id | YES | NO |
+----+-----+----+
| 1 | 5 | 3 |
| 2 | 3 | 1 |
+----+-----+----+
You can do so,using expression in sum() like sum(status ='Yes') will result as boolean (0/1) and thus you can have your count based on your criteria you provide in sum function
select id,
sum(status ='Yes') as `YES`,
sum(status ='No') as `NO`
from user
group by id;

handling null values on select query mysql

table stockholders
stockholder_id election_id user_id
1 1 1
2 1 2
3 2 3
table users
user_id user_type
1 1
2 1
3 1
4 1
5 1
select
*
from
tbl_users
left join tbl_stockholders
on tbl_stockholders.user_id = tbl_users.user_id
where
user_type=1
and stockholders.user_id is null
and election_id <> 1
i want to search election_id not equal to 1 and user type equal to 1
stockholder_id election_id user_id user_type
3 2 3 1
null null 4 1
null null 5 1
this is an update
sorry my problem should be excluding the tbl_stockholders from the tbl_users with the parameter election_id.. because a problem exist when i have a duplicate user_id
table stockholders
stockholder_id election_id user_id
1 1 1
2 1 2
3 2 3
4 1 3
in the previous answer this is the result when election_id<>2
stockholder_id election_id user_id user_type
3 1 3 1
null null 4 1
null null 5 1
this must be
stockholder_id election_id user_id user_type
null null 4 1
null null 5 1
this is my current not working code
select * from tbl_users
where not exists (select * from tbl_stockholders where election_id <> 2)
Try this:
SELECT * FROM users u
LEFT JOIN stockholders s ON u.user_id = s.user_id
WHERE u.user_type = 1 AND (s.election_id <> 1 OR s.election_id IS NULL)
Fiddle here.
I think, this is what you should do:
select
*
from
tbl_stockholders
left join tbl_users
on tbl_users.user_id = tbl_stockholders.user_id
where
(user_type=1
and election_id <> 1)
I don't think you intend to select null user_ids. Also that would invalidate the join i.e you are saying joined the two tables based user_id but select null user_id from the first table.