What want to achieve is something like the following: select a.column1 as value x if a.column1 has a value otherwise I want b.column1 as value x.
I would imagine something like the following pseudocode:
SELECT (hasValue(a.column1)?a.column1:b.column1 as x FROM a, b
In my real situation it is actually three tables. I guess that could make the effort a bit more tricker?
Coalesce returns the first non-null value, try this:
SELECT COALESCE(a.column1,b.column1) as x FROM a,b;
Related
I'm stumped on a proper query to get an output based on multiple columns and the conditions in those columns:
a b c
1 x x
2 x x
3 x
I would like the results to output, based on where the x's are in columns a, b and c:
1 a,c
2 a,b
3 a
Is this possible to do in mysql? Much appreciated
You can use the CONCAT_WS function (docs) and some IF statements.
SELECT tn.ID,
CONCAT_WS(
',',
IF(tn.a='x','a',null),
IF(tn.b='x','b',null),
IF(tn.c='x','c',null)
) as result
FROM TableName tn;
You can use IFNULL function for that (docs). For example:
SELECT a, IFNULL(b, c) FROM table_name;
It will select a for every case and conditionally b or c, depending on it's value (it have to be not null). But I'm afraid you cannot do more than that.
I want to calculate percentage for test groups.
I have group A,B and C. And I want to know how much success percentage each group have.
My first query is counting total test ran in each group by doing the following:
SELECT type, count(type) as total_runs
From mytable
Where ran_at > '2015-09-11'
Group by type
Second query is counting success for each group:
SELECT type, count(type) as success
FROM mytable
where run_status like '%success%' and ran_at> '2015-09-11'
Group by type
Now I need to divide one in the other and multiply in 100.
how do I do this in one query in an efficient way, I guess nested query is not so efficient- but anyway I can't see how I can uses nested query to solve it.
I would appreciate answer which include simple way, maybe not so efficient, and an efficient way with explanations
You can just use conditional aggregation:
SELECT type, sum(run_status like '%success%') as success,
100 * avg(run_status like '%success%') as p_success
FROM mytable
where ran_at> '2015-09-11'
Group by type;
In a numeric context, MySQL treats boolean expressions as integers with 1 for true and 0 for false. The above works assuming that run_status is not NULL. If it can be NULL, then you need an explicit case statement for the avg().
I had this one, but Gordon have a better solution if run_status is not NULL.
Select type, sum(if(run_status like '%success%',1,0)) / count(1) * 100) as p_success
From mytable
Where ran_at > '2015-09-11'
Group by type
I have a string for example 'p2p3p4p9c5c6c7' I want to make a select-statement in mysql that returns how much of those strings ('p6','p7','p8' or 'p9') are containing in the initial string.
The result of my example should be 1, because only 'p9' is containing in my string.
I don't find a good way to do that. Can someone help?
another example
'k2p4p6p8p9c8' the result should be here 3
You would seem to have a poor data format. If you want to store lists of things, use a junction table.
However, the best answer that I can think of is a set of conditions that are added together:
select ((str like '%p6%') +
(str like '%p7%') +
(str like '%p8%') +
(str like '%p9%')
) as NumInString
MySQL treats booleans as integers in a numeric context, with "1" for true and "0" for false.
I should repeat that if the substrings are really codes of some type, then these should be stored in a separate junction table, with one row per code and original row.
SELECT count(*) FROM
(SELECT 'p2p3p4p9c5c6c7' AS a) AS string_table
INNER JOIN
(SELECT 'p6' AS b UNION ALL
SELECT 'p7' UNION ALL
SELECT 'p8' UNION ALL
SELECT 'p9') AS list_table
ON INSTR(string_table.a,list_table.b) > 0;
its easier selecting a row whose value is between given number. But I m having no luck to figure out this-
I have a table in which there are two fields min_age and max_age. How to return all rows when given value lies between min_age and max_age
P.S. I m still a newbie in sql, please forgive me if this sounds too silly.
Thanks
SELECT *
FROM `tbl`
WHERE 35 BETWEEN `min_age` AND `max_age`;
That ought to do it. Of course, I used 35. You can use any other value instead.
You can use BETWEEN to get your desired result
SELECT *
FROM tablename
WHERE <some value> BETWEEN `min_age` AND `max_age`;
If you are trying to do the opposite, (provide a single value and find all the rows where the min and max age values bracket the provided value), then try
Select * From table
Where #myValue Between min_age And max_age
Based on your question this is my first thought:
select * from from YOUR_TABLE where VALUE > min_age AND VALUE < max_age
However, I think there maybe more to your question, if these is please elaborate.
You could also try to use the BETWEEN operator
SELECT * FROM Persons WHERE LastName BETWEEN 'Hansen' AND 'Pettersen'
http://www.w3schools.com/sql/sql_between.asp
I am not a MySQL expert by any means, but after reading the documentation for the SELECT statement, I did not find an answer to my problem.
I have this statement:
SELECT COUNT(*)=x.c FROM someTable,
(SELECT COUNT(*) c
FROM someTable
WHERE firstId <= secondId) x;
And I'm trying to figure out what the x.c means in the context of the query? Specifically, what is with the x that seems to be hanging out there?
I interpret the nested SELECT as SELECT COUNT(*) as c, making an alias for the row count as c, is that what the x is as well? What would it be an alias for?
Thanks!
The x is a table alias - a name for the nested SELECT statement in parentheses.
COUNT(*)=x.c
Is a boolean condition that the total row count of someTable be equal to the row count of someTable where firstId <= secondId
x.c is the column name for the count returned by the subquery.
They name the subquery "x" and in the subquery they name the count(*) "c". So "x.c" means the count returned by the subquery.
The x is an alias to the subquery - you will note that there is an x just after the subquery, denoting the alias name for it.
x is an alias for the table (SELECT COUNT(*) c FROM someTable WHERE firstId <= secondId).
MySQL requires that table subqueries have a unique alias. You'll notice there's an x at the end of the subquery, which makes the sub queries results appear as coming from table x.
In this case, x.c in the outer query means to refer to field c (the count result) in the aliased table x, which is the subquery.
X is an alias for joined someTable and the select you joined someTable with. I guess so :D
I guessed wrong, guys over me are right :P