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
Related
I have the following table structure:
name
value
success
name 1
10
0
name 2
20
0
name 2
30
1
And my query is:
SELECT name, SUM(value) as valueTotal FROM TableName GROUP BY name
The result is:
name
valueTotal
name 1
10
name 2
50
Now I want to add a new column which will contain the sum of only successful rows. But if I add this condition, it will apply to all selected fields:
SELECT name, SUM(value) as valueTotal, SUM(value) as successValueTotal FROM TableName WHERE success = 1 GROUP BY name
Here is the result I want to get:
name
valueTotal
successValueTotal
name 1
10
0
name 2
50
30
How can I add a field with a separate condition that does not affect the main query? Thx)
You can use the SUM function with a conditional aggregation on whether success is 1 or not. When success is 1, then take the value of the value field, otherwise sum up 0.
SELECT name,
SUM(value) AS valueTotal,
SUM(IF(success = 1, value, 0)) AS successValueTotal
FROM TableName
GROUP BY name
Try it here.
This is the typical use case for CASE WHEN:
SELECT name,
SUM(value) AS valueTotal,
SUM(CASE WHEN success = 1 THEN value ELSE 0 END) AS successValueTotal
FROM TableName
GROUP BY name
You can (like lemon showed) also use an if clause in MYSQL. This is a bit shorter, but the query will not work on every DB while CASE WHEN does. So I think both is fine.
I have a table with three columns
ID
Match_A
Match_B
ABC123
1
1
DEF111
0
1
QRS222
1
1
You can see ID 'ABC123' has a Match (as determined by 1) in both the Match_A and Match_B column. If their is a 1 in both of those columns, I am needing to write a formula that changes at least one of those columns into a 0. It doesn't matter if it's Match_A or Match_B.
The output for this ID would then turn into this.
ID
Match_A
Match_B
ABC123
1
0
Essentially, a match can happen in both columns, but for this report managers do not want it to be counted twice so one column has to be changed to 0 if a situation like this happens.
Any help is appreciated here! Thank you!
So a CASE should get you to the result I think you want
SELECT unique_id, match_a,
case when match_a = 1 AND match_a = match_b
then 0
else match_b
end as match_b
from table
I have the following table as an example:
numbers type
--------------
1 1
5 2
6 1
8 2
9 3
14 2
3 1
From this table I would like to select the closest number that is less or equal to 5 AND of type 1 and if there is no such row matching, then (and only then) I would like to return the first closest number larger than 5 of type 2
I can solve this by running two queries:
SELECT number FROM numbers WHERE number <= 5 AND type = 1 ORDER BY number LIMIT 1
and if above query returns 0 results, I simply run the second query:
SELECT number FROM numbers WHERE number > 5 AND type = 2 ORDER BY number LIMIT 1
But is it possible, to achieve the same result by only using one query?
I was thinking something like
SELECT number FROM numbers WHERE (number <= 5 AND type = 1) OR (number > 5 AND type = 2) ORDER BY number LIMIT 1
But that would only work, if mysql first checks the first conditional in the parentheses against all rows and if it finds a match, it returns it, and if not, then it checks all rows against the second parenthesed conditional. It will not work, if it checks each row against both parentheses and only then moves to the next row, which is how I suspect it works.
This query will do what you want. It selects all numbers that match your two query constraints, and orders the results first by type (so that if there is a result for type 1 it will appear first) and then by either -number or number dependent on type (so that numbers <= 5 sort in descending order but numbers > 5 sort in ascending order):
SELECT number
FROM numbers
WHERE ( number <= 5 AND type = 1 )
OR ( number > 5 AND type = 2 )
ORDER BY type, CASE WHEN type = 1 THEN -number ELSE number END
LIMIT 1
Output:
3
Demo on dbfiddle
Combine the two, and you always prefer type 1 over type 2, hence the ORDER BY and LIMIT. The ABS means whichever is first by type, is the closes to the number 5.
SELECT number, type
FROM numbers
WHERE (number <=5 AND type=1) OR
(number > 5 AND type=2)
ORDER BY type ASC, ABS(number-5) ASC
LIMIT 1
I have a table name current_record in mysql database.
id num
1 0
2 1
3 1
4 1
5 0
my question is : how find the 1st id of num = 1 from last means down to up or 3 times up
the output should return like this
id = 2 num = 1
please write a sql query.
You can use MIN() combined with GROUP BY to achieve this. Something like this:
SELECT MIN(id) AS id, num FROM current_record GROUP BY num
Optionally you can add a WHERE clause if you specifically want just one value instead of one for each num:
WHERE num == 1
In My table ,
One column has following values :
Row Column
1 90
2 95
3 99
4 90
5 92
6 90
Now I want to fetch min value for above column but fetch top row if there are more than one row which have same minimum value. How can I do this ?
As illustrated in above example : I want to min value(90) but first row.
Any help will be appreciated.
MIN is also the one that will sort first in ORDER. So simply specify the order you want and take first one:
WHERE Column is NOT NULL
ORDER BY Column, Row LIMIT 1
use this query select Row, min(column) from your_table order by Row desc limit 1
If you want to fetch the records in the same order of rows inserted, you can just use:
select Row, min(Column) from my_table group by Column;
If you want to fetch sorted rows as well:
select Row, min(Column) from my_table group by Column order by column, row;
And if you are looking only first row of the results, just append the query with limit 1;
SELECT TOP 1 * FROM m_test WHERE row IN (
SELECT row FROM m_test b
WHERE b.column_cnt IN (SELECT min(a.column_cnt) FROM m_test a))
ORDER BY row ASC