I have following table structure (Table Name is Questions)
c1 c2 c3 Selection
X Y Z 2
A B C 3
Here c1,c2,c3 and Selection are column names. I want to retrieve value of c1 or c2 or c3 on the basis of value of column Selection.
Eg. If Selection value is 2 then I want corresponding value of c2 column i.e. Y.
If Selection value is 3 then it should select value of c3 column which is C here.
Please help me in forming Select Sql query here. I tried myself but was not able to find correct solution.
Thanks in advance
SELECT CASE Selection
WHEN 1 THEN c1
WHEN 2 THEN c2
ELSE c3
END val
FROM tableName
you have to use CASE's or IF's
select if(selection=2 ,b,if(selection=3,c,a)) from Table1 ;
see here for example ... link
This is very simple just run a simple query and it will return all the columns then you should use the appropriate column.
SELECT * FROM mytable WHERE selection = 'yourvalue'
Related
Hello I am trying to find min date value between multiple columns in ssrs.
For example If date value in column A is less then in column B and column B is not nothing and column A is less then column C and column C is not nothing then chose field A
If Not Isnothing(A) and Not Isnothing(B) and A<B and A<C then
A
else if Not Isnothing(B) and Not Isnothing(C) and b<A and B<C
B
...
end if
I will be very grateful if you help me.
I answered a question recently that had a similar answer here..
How to set dynamic axis unit in SSRS Reports??
However, if possible I would so this in your dataset query it will be much easier as you can simply do..
SELECT MIN(myField) FROM
(SELECT A as myField FROM myTable
UNION
SELECT B FROM myTable
UNION
SELECT C FROM myTable
) x
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.
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
I need to write query that should return one column depends on existence of value in 2 columns in this way:
The columns are: C1 and C2 and they are in the same table T. If C1 exists then return C1, if C1 doesn't exist return C2.
select case when isnull(C1,'')<>'' then C1
else C2
end as Column_name
from table T
Case statement is used here.
select case when isnull(C1,'')<>'' then C1
when isnull(C2,'')<>'' then C2
end as Column_name
from table T
So i'm looking to write a MySQL query that will return a result set that, when a particular column has a particular row value, it will return that row instead of another near duplicate row but otherwise return results like normal.
Okay, here is my table
id name value another
1 name1 value1
2 name1 value1 foo
3 name2 value2
4 name3 value3
and results should be (if foo is present):
id name value another
2 name1 value1 foo
3 name2 value2
4 name3 value3
I did find this example: MySQL get rows but prefer one column value over another but couldn't figure out how to adapt it to my needs...
I hope I'm making sense! No sleep in two days ain't good for attempts at elucidation! Also I'm very sorry if this has already been asked, i searched for a good long time but just didn't have the vocabulary to find any results...
Thanks in advance!
SELECT
a.*
FROM atable a
LEFT JOIN atable b ON a.name = b.name AND a.another = 'foo'
This will filter out rows with an empty another, for which an entry with the same name and value exists that does have another.
select *
from YourTable yt1
where not exists
(
select *
from YourTable yt2
where yt1.id <> yt2.id
and yt1.name = yt2.pname
and yt1.value = yt2.value
and yt1.another = ''
and yt2.another <> ''
)
This sounds like a situation where the mysql coalesce function would be handy.
Coalesce returns the first non-null parameter it's given. So you can use,
SELECT id, COALESCE(another, value) FROM MyTable;
this will return two columns, the id field and either the contents of the "another" column (if it is not null) or the contents of the "value" column.