MySQL group by based on column having a given set of data - mysql

I want to produce an accurate dataset from mysql based on true and false logic, if I have say 4 rows and a column has status, if in the status any row has a zero the data group should be regarded as false else true
e.g
k | s
--------- //This if i group by k, it returns 1 while i
a 0 //want it to return 0 on group
a 0
a 1
a 1
k | s
--------- //This one if i group by k will return 1 which is very true
a 1
a 1
a 1
a 1
Suggestions

Try this query:
SELECT k,
CASE WHEN SUM(s) < COUNT(s) THEN 0 ELSE 1 END AS label
FROM yourTable
GROUP BY k
This would work well assuming that the only values in the s column are 0 or 1. The logic here is that if even a single row has a zero value, then the SUM(s) for a given k group would be less than the number of records in that group.

Related

SQL query statement Self Join?

new to SQL.
I have the following set of data
A X Y Z
1 Wind 1 1
2 Wind 2 1
3 Hail 1 1
4 Flood 1 1
4 Rain 1 1
4 Fire 1 1
I would like to select all distinct 'A' fields where for all rows that contain A have flood and rain.
So in this example, the query would return only the number 4 since for the set of all rows that contain A = 4 we have Flood and Rain.
I need the values of A where for a given value 'a' in A, there exists rows with 'a' that must contain all of the following fields provided (in the example Flood and Rain).
Please let me know if you need further clarification.
I need the values of A where for a given value 'a' in A, there exists rows with 'a' that must contain all of the following fields provided (in the example Flood and Rain).
You can use aggregation, and filter with a having clause:
select a
from mytable t
where x in ('Flood', 'Rain') -- either one or the other
having count(*) = 2 -- both match
If tuples (a, x) tuples are not unique, then you want having count(distinct x) = 2 instead.
You Shooud use count(distinct X) group by A and having
count(distinct...) avoid situation where you have two time the same value for X
select A
from my_table
WHERE x in ('Flood', 'Rain')
group A
having count(distinct X) = 2

mysql select counts by if else condition

i have a table named cq500_all(to record diffrent doctor feedback)
now i want know counts when condition status is
field dr_1_finish and field dr_2_finish value is all fill 1
and
when field dr_1 different dr_2 (like dr_1=1 and dr_2=0,or dr_1=0 and dr_2=1 )
cause i want to know two doctors feedback counts (when different doctor's feedback on jpg)
for example image show CQ500-CT-1_36_08.jpg and CQ500-CT-1_36_09.jpg is match my select counts
it will be two (select counts result)
how to make the query on mysql?
You can count as
select count(*) as total
from cq500_all
where dr_1_finish = 1 and dr_2_finish = 1 and dr_1 != dr_2
You will got result in total
Pretty much just the way you've described it:
select *
from cq500_all
where dr_1_finish = 1 and dr_2_finish = 1
and dr_1 != dr_2
or (if dr_1 or dr_2 might not be just 0 and 1):
select *
from cq500_all
where dr_1_finish = 1 and dr_2_finish = 1
and ((dr_1 = 1 and dr_2 = 0) or (dr_1 = 0 and dr_2 = 1))

Check column if has the same value on all the rows

How do I check if a column has all the rows the same value?
I don't think this will work.
SELECT column FROM table WHERE value = 1
I want to make, by time each row will turn from 0 to 1 till every row has value 1, if all the values are 1 to turn all in 0
id value
1 1
2 1
3 1
4 1
5 1
6 1
7 1
You can try to use distinct
select count(distinct column) FROM table
If the result is 1 then it means there is only same value present in the column else there are different values present in your column.
Try this query :-
select count(value) from table where value =0;
if rows return count is zero that means there are no zeroes in that column.
Use count
SELECT count(value) as total FROM table
if total > 1 than more than on value

SQL - Show records which meet 2 or more of multiple conditions

I have a MySQL table of many records. I am trying to find a way to show the records which meet more than one condition of a query. For example, if I had this table.
TABLE NAME: DATA
ID contactid flag flag_type
-----------------------------------
1 99 Volunteer 1
2 99 Uploaded 2
3 100 Via Import 3
4 100 Volunteer 1
5 100 Uploaded 2
with conditions such as:
WHERE (ID > 2) OR (flag = 'Uploaded') OR (flag_type = 1) ..etc..
The output would be where IDs 4 & 5 only would be returned.
You can count the number of conditions in MySQL and use this value:
where ((id > 2) +
(flag = 'Uploaded') +
(flag_type = 1)
) > 1
A boolean value of "true" is treated as 1 AND "false" is treated as 0. So, by adding up the values, you get the number of conditions that are met.
Often, you do this in an order by, to get the most matching first:
where id > 2 or flag = 'Uploaded' or flag_type = 1
order by ((id > 2) +
(flag = 'Uploaded') +
(flag_type = 1)
) desc;
http://sqlfiddle.com/#!2/572e1/11
This solution selects each of your successful conditions as 1 which is added to "factors". Then we show results with at least 2 factors:
SELECT * FROM
(select id, contactid, flag, flag_type,
(CASE WHEN id > 2 then 1 else 0 END) +
(CASE WHEN flag = 'Uploaded' then 1 else 0 END) +
(CASE WHEN flag_type = 1 then 1 else 0 END) AS factors
from DATA
) t
WHERE factors > 1
With your limited question, this is the most that can be provided.
WHERE `ID`>2 AND (`flag`='Uploaded' OR `flag_type`=1) ..etc
You are on the right path, but you need to read first about boolean logic. ORs are going to return records that match any of the conditions and ANDs will return records that match all of the conditions.

Counting total and true condition lines

How to count the number of lines in a table and the number of lines where a certain condition is true without resorting to subselects like this:
create table t (a integer);
insert into t (a) values (1), (2), (null);
select
(select count(*) from t) as total_lines,
(select count(*) from t where a = 1) as condition_true
;
total_lines | condition_true
-------------+----------------
3 | 1
select count(*) as total_lines, count(a = 1 or null) as condition_true
from t
;
total_lines | condition_true
-------------+----------------
3 | 1
It works because:
First while count(*) counts all lines regardless of anything, count(my_column) will count only those lines where my_column is not null:
select count(a) as total
from t
;
total
-------
2
Second (false or null) returns null so whenever my condition is not met it will return null and will not be counted by count(condition or null) which only counts not nulls.
Use SUM(condition)!
select
count(*) as total_lines,
sum(a = 1) as condition_true
from t
See it working here.
This works because in mysql, true is 1 and false is 0, so the sum() of a condition will add 1 when it's true and 0 when it's false - which effectively counts the number of times the condition is true.
Many people falsely believe you need a case statement, but you don't with mysql (you do with some other databases)
this can be easily done using a condition inside count. I don't know if its the optimized method of doing it but it gets the work done
you can do it as follows
select count(*) as total_lines, COUNT(CASE WHEN a = 1 THEN 1 END) as condition_true from t
you can check it here
sqlFiddle