MySQL conditional concatenate based on multiple columns - mysql

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.

Related

Sort columns based on data in tuples mysql

Say, I have a table
A B C D E F
1 2 4 3 6 5
4 2 3 1 6 5
4 5 3 6 1 2
How can one get an output based on rearranging based on its data. For example,
ABDCFE
DBCAFE
EFCABD
is it possible?
EDIT:
The question seems to be asking: How can I get the list of column names in order by value?
I got it. You want to sort the values in each row and show the names of the columns in order.
Let me assume that you have a row id, so you can identify each row. Then:
select id, group_concat(which order by val) as ordered_column_names
from ((select id, a as val, 'A' as which from t) union all
(select id, b, 'B' as which from t) union all
(select id, c, 'C' as which from t) union all
(select id, d, 'D' as which from t) union all
(select id, e, 'E' as which from t) union all
(select id, f, 'F' as which from t)
) t
group by id
order by id;
SQL is fundamentally not the tool to do the operation you describe, because it violates the concept of a relation. I don't mean the common use of "relation" meaning a relationship, I mean the mathematical definition of relation.
There is no order of columns in a relation. The columns are a set, which is by definition unordered. Columns are identified by their name, not their position left-to-right.
All the entries in rows under each respective named column must be part of the same data domain. If you mix them around on a row-by-row basis, you're violating this.
I guess all your columns A through F are actually using values in the same data domain, or else reordering them wouldn't make any sense. If this is true, then you're violating First Normal Form by defining a table with repeating groups of columns. You should instead have all six columns be in one column of a second table. Then it becomes very easy to sort them by value.
Basically, what you're trying to do is better solved by formatting the data results in some application code.
There is a way to do it ,get coulmn name by column ordinal order and print it.
For each value in coulmn iterate this and get the column name for the ordinal specified in cloumn data. Here ordinal position is value in each coulmn data. Iterate for each row and each column and your problem is solved.
select column_name
from information_schema.columns
where table_name = 'my_table_name' and ordinal_position = 2;
It appears that you are asking for output where each row in the output is just a specification of the order of the data values in the columns.
Then, if the values are always integers between 1 and 5, you can do it by outputting a character value of 'A' where the data value is 1, a 'B' where the data value is 2, etc. This SQL will do that.
Select Char(A+64)A, Char(B+64) B,
Char(C+64) C, Char(D+64) D,
Char(E+64) E, Char(F+64) F
From table
if the want the column sort order in one output column, you could also do this:
Select Char(A+64) + Char(B+64) +
Char(C+64) + Char(D+64) +
Char(E+64) + Char(F+64) SortOrder
From table

Mysql how to sum column with previous column sum

In XLS I have two columns A and B.
A,B
1,1
2,3
2,5
1,6
5,11
2,13
A column have value, and B column is calculated with formula, (A + (previous row B value))
How can i do this calculation on MYSQL?
I'm trying to join same table twice and i can get previous rows A column next to B.
I can sum them, but how can I sum them with this formula?
XLS formula looks like this:
H20 = H19+G20
This is my SQL created from suggestions.
SELECT
date, time, sum, #b := sum+#b as 'AccSum', count
FROM
(SELECT
t.date, t.time, t.sum, t.count
FROM TMP_DATA_CALC t
ORDER BY t.epoch) as tb
CROSS JOIN
(SELECT #b := 0) AS var
;
SELECT A, #b := A+#b AS B
FROM (SELECT A
FROM YourTable
ORDER BY id) AS t
CROSS JOIN
(SELECT #b := 0) AS var
The user variable #b holds the value of B from the previous row, allowing you to add the current row's A to it.
DEMO
http://sqlfiddle.com/#!2/74488/2/1 shows how to select the data.
SET #runtot:=0;
Select a,b, #runtot:=#runtot+a from b
However there's an underlying problem I can't figure out. Since you don't have a defined order, the SQL could do this ordering in any way, so you may not get the desired results.. without a defined order you results may be unpredictable.
runtot = running total.
In MySQL we don't have any function like partition by which Oracle has. You can use curser to achieve your requirement. Or we can write any function which will get rownumber as input then add these two values then return that to query.
select b from xsl limit rownum-1,1 + select a from xsl limit rownum,1

Different between group by() and group by

when I use mysql I got this error and please can anybody explain me this. what is the different between A, B, C of the following?
A) select * from table where a=a group by(b) // this execute & work fine
B) select * from table where a=a group by b,c // this execute * work fine
c) select * from table where a=a group by (b,c) // this is giving an error - error is operand should contain 1 column.
In the A it works fine with out errors with brackets, but when I use the same method in C for multiple grouping its not working and gives mentioned error.
why is that? what is the different between group by () & group by in mysql grouping?
Thankyou.
These are equivalent:
group by (b), (c)
group by b, c
Because the brackets are redundant(they have no effect), but in this:
group by (b, c)
The brackets are creating a single ordering term from the expression b, c, which is not a single value, and order by terms must be single-valued.
group by (b,c) means you group by the field "b,c", because you use "()".
group by b,c means you group by the field b then group by field c

How to select on field value in MySQL

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;

count result output don't show if result is zero

Count multiple column using the following query:
select count(price) as a ,count(buy_price) as b from table where c=1
if a or b one of result are zero then that column don't show. suppose count(price) as a output is 0 and count(buy_price) as b is 10 then output will be:
b
10
First and foremost, your query should look like this:
select count(price) as a, count(buy_price) as b from table where c=1
Note the comma (",") between ...as a and count(buy_price)....
In general, you should have commas separating the fields in your projection, e.g.:
select field1, field2, field3 from myTable
If you really want both fields to appear in your result set, you'll need to use the built-in IFNULL function:
select COUNT(IF(price=0,1,price)) as a, COUNT(IF(buy_price=0,1,buy_price)) as b from table where c=1