Suppose I have a table :
start_range end_range
1 4
4 8
I want the result to be true if it is greater than any of the value of start_range and less than any of the corresponding end_range.
Eg.
value 2 should return true , as 2>1 and 2<4
but value 4 should return false in this case as 4>1 but 4<4 becomes false, as well as 4>4 becomes false for the second case.
I cannot use the query
SELECT Sumthing
FROM XYZ
WHERE value> SOME(start_range) AND value < SOME(end_range)
The problem with the above query is let say value = 4.
Now 4> SOME(start_range) will become true as 4>1. AND
4< SOME(end_range) will also become true as 4<8.
But in actual the comparison should be like (((4>1)AND(4<4)) OR ((4>4)AND(4<8))) . It should return false.
One more thing , the above table is not persistent , I have been creating it in a subquery.Thats why i have been using SOME.
if still my question isn't clear, mention in comments.
Assuming that xyz is your table:
select (count(*) > 0) as HasMatch
from xyz
where value > start_range and value < end_range;
I'm not sure why you are using some.
EDIT:
It occurs to me that you want to use subqueries, and xyz is not the table in question. Perhaps this is what you want:
select xyz.*
from xyz
where exists (select 1
from (<your query here>) t
where xyz.value > t.start_range and xyz.value < t.end_range
);
you can do something like this
SELECT CASE WHEN start_range<value and end_range>value
THEN 'true'
ELSE 'false'
END here_name_to_this_column(optional)
FROM table_name
tutorial link
select (count(*) > 0) as HasMatch
from (select IF(start_range<value and end_range>value, true, false ) as value
from XYZ having value =1) as MatchTable
DEMO
Related
I have a table like this
user result
john +
mike -
john -
rita +
I want to get the percentage of - grouped by user. So for my example the result must be:
user %min
john 50%
mike 100%
rita 0%
Is that possible in mysql to create such a query?
Just use conditional aggregation. Here is a simple method:
select user, avg(result = '-') as percent_min
from t
group by user;
This will give the result as a value between 0 and 1, which can then be formatted as you desire.
Gordon's answer implies a database platform that implicitly casts a Boolean TRUE to 1 and a Boolean FALSE to 0. Which is not prescribed by the standard. Should you run into an error going something like "Function avg(boolean) does not exist", try a CASE expression:
WITH tb (usr,result) AS (
SELECT 'john','+'
UNION ALL SELECT 'mike','-'
UNION ALL SELECT 'john','-'
UNION ALL SELECT 'rita','+'
)
SELECT
usr
, AVG(CASE result WHEN '-' THEN 100 ELSE 0 END) AS percent_min
FROM tb
GROUP BY usr
ORDER BY usr;
Happy playing -
Marco
I have two MySQL statements (see below), I would like to combine them together so if they both result in a 1 then the end result will be 1. I'm not sure how to construct this and was hoping for some help.
select count(*)
from monitor
where name='job_starttime' and value < ( UNIX_TIMESTAMP( ) -600)
select count(*)
from monitor
where name='job_active' and value = 0
So for example I would like when both statements are true to result in a value of 1, if 1 or none are true it results in a 0.
I have a set of records which return flags in individual records. Based on that flags I want to perform and operation within those flags (e.g.if any single flag is "true" final result should return me "true")
Following are the set of records which is returned by normal SQL query ;
select ID,flag from tablename;
--------------------------
ID(int) | flag (varchar(5))
--------------------------
1 | true
2 | false
3 | true
4 | false
5 | false
I would like to know the SQL query which returns result as shown below (such a way that final result should be and operation within each of those 5 records)
-------
flag
-------
true
I'm not sure I completely understand what you want to do, but if you want to define your AND operation as "at least one of the records must have as its flag value 'true'", you can use the following SQL statement which will return 1 if there is at least one 'true' value and otherwise will return 0:
SELECT IF(COUNT(*) > 0, 'true', 'false')
FROM Table1
WHERE flag = 'true';
See the SQL Fiddle: http://sqlfiddle.com/#!2/1e6cd7/11
i came up with this solution but maybe you will get a short and better one :) .
select if(count(*) = 1 , 'mixed' , flag) flag from(
select flag from table1
where flag = 'true'
having count(*) = 5
union
select flag from table1
where flag = 'false'
having count(*) = 5
union
select 'mixed' from table1
)t
DEMO TO PLAY WITH
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
SELECT if((COUNT(vote=1)-COUNT(vote=0) > 0,1,count(groupid))
FROM sample WHERE uid = $uid GROUP BY groupid
In the if statement, I was wondering if there is any simple way to achieve this: COUNT(vote=1)-COUNT(vote=0) > 0
Use SUM instead of COUNT:
SELECT if((SUM(vote=1)-SUM(vote=0) > 0,1,count(groupid))
FROM sample WHERE uid = $uid GROUP BY groupid
You want the sum of vote, except 0 counts as -1:
SELECT if(sum(if(vote, 1, -1)) > 0, 1, count(groupid))
...
are you trying to do
Select columns,
case when (COUNT(vote=1) - COUNT(vote=0)) > 0
then 'something'
else 'something else' as foo
From Sample
Where uid=$uid
Group By groupid
I don't quite understand what you are trying to achieve.
With my example, you select the columns (or just that case), when the difference is greater than 0. If it is greater show 'something' else show 'something else'
although, i don't have a way to test it on this computer, but maybe it will lead you or someone else in the right direction.