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
Related
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 have below result set
Name | ID | Total | CityName
--------------------------------
A 1 2 ABC
--------------------------------
B 2 1 XYZ
--------------------------------
C 3 1 ABC
--------------------------------
How I can show below result
Name | ID | ABC | XYZ
---------------------------
A 1 2 0
---------------------------
B 2 0 1
---------------------------
C 3 1 0
---------------------------
Use conditional aggregation
select name, id, max(case when CityName='ABC' then total else 0 end) as ABC
max(case when CityName='XYZ' then total else 0 end) as XYZ
from tablename
group by name,id
Well it seems like a subquery, or procedure would be a good idea.
Something like:
select Name,ID,
(select Total from tets where CityName ='ABC' and Id = t.Id) as 'ABC',
(select Total from tets where CityName ='XYZ' and Id = t.Id) as 'XYZ'
from tets t
id | p_id
--------------------------
1 | 0
2 | 0
3 | 1
4 | 2
5 | 0
6 | 7
7 | 1
8 | 0
This is above table data's, if i give
SELECT * from tablename order by `id` asc
it will bring the above result set
But my use case is i need to sort by both the id and p_id in a different way (i.e) i need to get result set by like below
id | p_id
--------------------------
1 | 0
3 | 1
7 | 1
6 | 7
2 | 0
4 | 2
5 | 0
8 | 0
let me explain briefly about that, p_id value 1 should be the next to the id 1 and that is arranged like above, it clearly show that p_id 1 and p_id 2 is next to the id 1 and 2 respectively and make sure that id 7 in order and id 6 next to id 7
If I understood you correctly , you want the order to be -> If P_id=0 then to order by ID , else, order by p_id.
You can achieve this by conditional ordering using CASE EXPRESSION :
SELECT *
FROM YourTable t
ORDER BY CASE WHEN t.pid = 0
THEN t.id
ELSE t.p_id
END ASC,
t.id
This should return you your expected results.
You need to know the relationship between the id's and since the 2 id blocks have different sort requirements you also need a helper column and a subquery.
create table t (id int, pid int)
insert into t
values
( 1 , 0),
( 2 , 0),
( 3 , 1),
( 4 , 2),
( 5 , 0),
( 6 , 7),
( 7 , 1),
( 8 , 0)
select s.id,s.pid
from
(
select case
when t.id in (1,3,7,6) then 1
else 2
end as ac
,
case
when t.id in (1,3,7,6) then pid
else id
end as sortcol
,
t.*
from t
) s
order by s.ac,s.sortcol
Result
id pid
1 0
3 1
7 1
6 7
2 0
4 2
5 0
8 0
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
I have 3 column (prod1 , prod2 , prod3 ) with TYPE : DOUBLE
id | prod1 | prod2 | prod3 |
1 | 1.3 | 2.6 | 2.8 |
2 | 0.8 | 3.4 | 0 |
3 | 0 | 0 | 1.3 |
4 | 0 | 0 | 0 |
What I want is COUNT() of 3 columns
SELECT count(prod1,prod2,prod3) AS allc
FROM `testprd`
WHERE id =3
I know above code is wrong
SHOULD GIVE RESULT
allc
-------
1
As prod1 and prod2 have 0 values
Similarly when id = 4 count should be 0 as all column for resp id have zero value ,but when id = 1 then count should be 3
Hence I taught count for each id columns and then sum of all , will result me solution but am not able to reach it.
BELOW IS WHAT I HAVE TRIED
SELECT count(prod1) AS a,
count(prod2) AS b,
count(prod3) AS c
FROM `testprd`
WHERE id =3
Result:
a | b | c
-------------
1 1 1
But should be:
a | b | c
-------------
0 0 1
So sum(a+b+c) = 1
Hence count for id = 3 is 1
What am I doing wrong?
You could get the result you want to have with
SELECT
(prod1 !=0 ) + (prod2 != 0) + (prod3 != 0) AS allc
FROM `testprd`
WHERE id = 3
The aggregate function COUNT counts rows in a table or not null rows in a certain column, but not the values that are not equal zero in a set of columns.
COUNT(expr)
Returns a count of the number of non-NULL values of expr in the rows
retrieved by a SELECT statement. The result is a BIGINT value.
COUNT() returns 0 if there were no matching rows.
Use
SELECT id, if(prod1+prod2+prod3>0,1,0) from testprd;
For all columns separated it should be:
SELECT id, if(prod1>0,1,0), if(prod2>0,1,0), if(prod3>0,1,0) from testprd;
Use a simple query like this
SELECT
IF(prod1 > 0,1,0)+IF(prod2 > 0,1,0)+IF(prod3 > 0,1,0) as Total
FROM test
WHERE id = 3;
SQL Fiddle Demo
OUTPUT
| TOTAL |
|-------|
| 1 |
Just check if that product is different from zero then sum all counts like:
SELECT if(prod1!=0,1,0) +
if(prod1!=0,1,0) +,
if(prod1!=0,1,0) AS ct
FROM `testprd`
WHERE id =3
How about:
SELECT
count(*) AS allc FROM `testprd`
WHERE
id =3 AND
0 < ANY (prod1, prod2, prod3);
COUNT counts the rows slected by your SELECT statement, it does not sum up the column values.
SELECT prod1 + prod2 + prod3 AS mySum
FROM `testprd`
WHERE id =3;
See the MySQL doc concerning Arithmetic Operators and COUNT