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
Related
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
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
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
I have three tables in database:
Table: article
id | code | name | quantity | stock_date
--------------------------------------------------
1 1dfod Article name 10 2016-04-01
Table: selling
id | client_id | selling_type_id | selling_date | selling_status
----------------------------------------------------------------
1 1 1 2016-04-02 1
2 1 1 2016-04-03 1
3 1 1 2016-04-04 1
Table: selling_detail
id | selling_id | article_id | quantity
-------------------------------------
1 1 1 2
2 1 1 3
3 1 1 1
4 2 1 3
5 3 1 1
at the end I would have a stock record for this article like this:
date | in_stock (item in stock) | out_stock (sum of item sold)
----------------------------------------------------------------------
2016-04-01 10 0
2016-04-02 0 6
2016-04-03 0 3
2016-04-04 0 1
All mysql queries to my knowledge do not give me this result.
Here is my code:
SELECT SUM(sd.quantity) out_stock, s.search_date, ifnull(ss.quantity, 0) in_stock
FROM selling_detail sd JOIN selling s ON (sd.selling_id = s.id)
LEFT JOIN shop_stock ss ON (ss.search_date = s.search_date) WHERE (sd.shop_stock_id = 1)
GROUP BY s.search_date;
SELECT date,SUM(in_stock) in_stock,SUM(out_stock) out_stock FROM
(
SELECT stock_date date,quantity in_stock,0 out_stock FROM article
UNION
SELECT selling_date,0,quantity FROM selling JOIN selling_detail ON selling_detail.selling_id = selling.id
) x
GROUP BY date;
As you are trying to combine similar data from two very different tables, you'll probably be staring down the barrel of a UNION ALL.
Something along these lines should get you started:
SELECT *
FROM (
SELECT a.stock_date `date`,
SUM(a.quantity) `in_stock (item in stock)`,
0 `out_stock (sum of item sold)`
FROM article a
WHERE a.id = :article_id
GROUP BY `date`
UNION ALL
SELECT s.selling_date,
0,
SUM(sd.quantity)
FROM selling s
JOIN selling_detail sd
ON sd.selling_id = s.id
AND sd.article_id = :article_id
/* WHERE s.selling_type = ??
AND s.selling_status = ?? /* If necessary */
GROUP BY `date`
) sr
ORDER BY `date`
I have the following table:
id club_id club_option_id created
---|-------|--------------|-------
1 | 1 | 2 | 2015-02-11 16:31:23
2 | 1 | 3 | 2015-02-11 16:31:23
3 | 2 | 2 | 2015-03-06 08:16:02
I would like to select the club (club_id) who has both options (2 and 3)
I don't get any results with the following query:
SELECT club_id FROM
club_options_to_clubs
WHERE club_option_id = 2 AND club_option_id = 3
GROUP BY club_id
How can I get club_id 1 as a result?
You can do as below -
select
t1.club_id from table_name t1
where t1.club_option_id = 2
and exits (
select 1 from table_name t2
where t1.club_id = t2.club_id
and t2.club_option_id = 3
)
The other way is
select club_id from table_name
where club_option_id in (2,3)
group by club_id
having count(*) = 2
With the above approach if you need to check multiple club_option_id pass them in the in function and then use the number in having count(*) = n
Here n = number of items in the in function.