MySQL - Count if the values of two column is different - mysql

My Table
colA | colB
0 | 0
1 | 0
0 | 1
1 | 1
if the values of colA and colB are different then count.
expected result: 2 (row 2 and row 3)

SELECT COUNT(*) as different
FROM tablename
WHERE colA != colB

Related

merge TWO results of SQL select

There are two types of data in the same mysql table.
Some data's specific column is set to '1' and another data's column is set to '0' (0 and 1 in the same column)
How can I mix them at once like below? (with select query) - every six '0' row is followed by one '1' row.
Well, you could use window functions for sorting - if you are running MySQL 8.0:
select code, val
from mytable
order by
row_number() over(partition by val order by code)
* (case when val = 0 then 1 else 5 end),
val,
code
The logic is to rank records having the same value ; then simple artithmetics can be used to handle the alternation (5 0s, then 1 1).
Demo on DB Fiddle:
code | val
:----- | --:
data1 | 0
data10 | 0
data11 | 0
data12 | 0
data13 | 0
data2 | 1
data14 | 0
data15 | 0
data3 | 0
data4 | 0
data5 | 0
data8 | 1
data6 | 0
data7 | 0
data9 | 0
I would phrase this as:
order by ( floor(row_number() over (partition by column order by data) - 1) /
(case when column = 1 then 1 else 5 end)
)
),
column,
data

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 get all rows based on a specific column value (filter by column)

I have this table A here:
| colA | colB
| 1 | 2
| 2 | 2
| 3 | 3
| 3 | 2
I want to query all rows where colB is both 3 and 2. In which the result would be:
|colA | colB
| 3 | 3
| 3 | 2
I tried querying:
select * from A where colB = 2 AND colB= 3
But I keep getting an empty response. Tried OR but it also won't work. I understand that what I experimented is sort of a "row" filtering.
Is there a way to handle "column" filtering?
Try this:
select *
from A
where colA IN (select colA
from A
where colB IN (2, 3)
group by colA
having count(distinct colB) = 2)
or with a JOIN:
select A.*
from A
join (select colA
from A
where colB IN (2, 3)
group by colA
having count(distinct colB) = 2
) as t on A.colA = t.ColA
You can use EXISTS() :
SELECT * FROM YourTable a
WHERE EXISTS(SELECT 1 from YourTable s
WHERE a.colA = s.colA
and s.colB in(2,3)
GROUP BY s.colA having count(distinct s.colB) = 2)

need group by data in mysql

in mysql:
Have data in data:
ColA | ColB | Rank
1 2 0
1 3 1
2 1 0
3 1 0
3 2 1
Keeping Col A as key field, need to get data on base of highest rank i.e.,
output:
ColA | ColB | Rank
1 3 1
2 1 0
3 2 1
any ideas..
You can do it this way:
SELECT T1.ColA, T2.ColB, T1.Rank
FROM TableName T2 JOIN
(SELECT ColA,MAX(Rank) as Rank
FROM TableName
GROUP BY ColA) T1 ON T1.ColA=T2.ColA AND T1.Rank=T2.Rank
Explanation:
Inner query (T1) selects records with highest rank for each values of ColA.
Outer query (T2) is used to select ColB with respect to the values of ColA and Rank from T2.
Result:
ColA ColB Rank
--------------------
1 3 1
2 1 0
3 2 1
See result in SQL Fiddle

Count considering sum of 3 columns

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