What is the best way to append a column at the bottom of another column? So it would look something like this:
column_a column_b
a e
f k
c m
new_column
a
f
c
e
k
m
All i can really find is answers on how to merge two column into one and this is with CONCAT() but nothing about pasting two column ontop of each other.
If you want to keep the order, you could use something like this.
with col_a as (select 'a' as column_a union select 'f' union select 'c'),
col_b as (select 'e' as column_b union select 'k' union select 'm')
select 1 as num_order,
column_a
from col_a
union all
select 2 as num_order,
column_b
from col_b
order by num_order;
Yields:
1,a
1,c
1,f
2,e
2,k
2,m
If the order is irrelevant you can omit the num_order attribute.
I have a multiple SELECT and trying to add If or Case statement to assign values based on what it is DB. Trying to change Y to DD and blanks to TTTT
My code looks like that
SELECT
wo_1.ata_chapter AS 1,
wo_2.type AS 2,
wo_3.comp_partno AS 3,
' ' AS 4,
wo_5.mel_code AS 5,
WHEN wo_6.hil AS DD
FROM wo_db
I tried CASE
CASE
WHEN wo_6.hil = 'Y'
Then 'DD'
Else 'TTTT'
I tried If as well
IF (wo_6.hil = 'Y', 'DD', Then 'TTTT')
The syntax for case would be:
(CASE WHEN wo_6.hil = 'Y' THEN 'DD' ELSE 'TTTT' END) as xx
The parentheses are not needed (although recommend them), but the END is.
Note: This is based on your code. Your description doesn't specify what to do when the value is neither 'Y' nor blanks.
I need that when var1 is equal to 'Y' the query show the result of the table with the column = 'Y', but when var1 = 'N' the result should be the data with column with 'Y' and 'N'.
I need to put it in a where clause, cause I'm using oracle forms.
I tried this way but the query didn't show any result:
SELECT *
FROM table
WHERE column1 IN ((CASE WHEN var1 = 'Y' THEN q'[('Y')]'
ELSE TO_CHAR(q'[('Y','N')]')
END))
Can you help me? Thank you.
There is no need for CASE logic here, as you can fit this into a regular WHERE clause with boolean logic, wrapping each condition (var1 = 'Y', var1 <> 'Y') in a () group.
SELECT *
FROM table
WHERE
(var1 = 'Y' AND column1 = 'Y')
OR (var1 <> 'Y' AND column1 IN ('Y','N'))
Note, I used var1 <> 'Y' here to emulate your ELSE case, but if it is only two possible values Y/N you may use var1 = 'N' for clarity.
WHERE
(var1 = 'Y' AND column1 = 'Y')
OR (var1 = 'N' AND column1 IN ('Y','N'))
Actually, if Y/N are the only possible values for column1, then it could be simplified to:
WHERE
(var1 = 'Y' AND column1 = 'Y')
-- Returns all rows for column1 if Y,N are the only possible values
-- No need to explicitly filter it
OR (var1 <> 'Y')
How do I rewrite this SQL statement to do what I need?
SELECT * FROM TABLE
WHERE COLUMN_NAME_1 IN (CASE
WHEN COLUMN_NAME_2 = 'X' THEN 'A'
WHEN COLUMN_NAME_2 = 'Y' THEN 'B', 'C' END)
Obviously I can't return multiple values from a CASE clause... so how else could I write this? I am pretty sure I am slow today because this seems so easy ....
SELECT * FROM Table WHERE
(COLUMN_NAME_2 = 'X' AND COLUMN_NAME_1 = 'A') OR
(COLUMN_NAME_2 = 'Y' AND COLUMN_NAME_1 IN ('B', 'C'))
or
SELECT * FROM Table WHERE COLUMN_NAME_2 = 'X' AND COLUMN_NAME_1 = 'A'
UNION ALL
SELECT * FROM Table WHERE COLUMN_NAME_2 = 'Y' AND COLUMN_NAME_1 IN ('B', 'C')
This presumes that you only want results with X or Y in COLUMN_NAME_2. If you want other rows it's not possible to tell which ones from your original SQL.
Is it possible to alias a column name and then use that in a CASE statement? For example,
SELECT col1 as a, CASE WHEN a = 'test' THEN 'yes' END as value FROM table;
I am trying to alias the column because actually my CASE statement would be generated programmatically, and I want the column that the case statement uses to be specified in the SQL instead of having to pass another parameter to the program.
This:
SELECT col1 as a,
CASE WHEN a = 'test' THEN 'yes' END as value
FROM table;
...will not work. This will:
SELECT CASE WHEN a = 'test' THEN 'yes' END as value
FROM (SELECT col1 AS a
FROM TABLE)
Why you wouldn't use:
SELECT t.col1 as a,
CASE WHEN t.col1 = 'test' THEN 'yes' END as value
FROM TABLE t;
...I don't know.
I think that MySql and MsSql won't allow this because they will try to find all columns in the CASE clause as columns of the tables in the WHERE clause.
I don't know what DBMS you are talking about, but I guess you could do something like this in any DBMS:
SELECT *, CASE WHEN a = 'test' THEN 'yes' END as value FROM (
SELECT col1 as a FROM table
) q
#OMG Ponies - One of my reasons of not using the following code
SELECT t.col1 as a,
CASE WHEN t.col1 = 'test' THEN 'yes' END as value
FROM TABLE t;
can be that the t.col1 is not an actual column in the table. For example, it can be a value from a XML column like
Select XMLColumnName.value('(XMLPathOfTag)[1]', 'varchar(max)')
as XMLTagAlias from Table
It should work. Try this
Select * from
(select col1, col2, case when 1=1 then 'ok' end as alias_col
from table)
as tmp_table
order by
case when #sortBy = 1 then tmp_table.alias_col end asc
I use CTEs to help compose complicated SQL queries but not all RDBMS' support them. You can think of them as query scope views. Here is an example in t-sql on SQL server.
With localView1 as (
select c1,
c2,
c3,
c4,
((c2-c4)*(3))+c1 as "complex"
from realTable1)
, localView2 as (
select case complex WHEN 0 THEN 'Empty' ELSE 'Not Empty' end as formula1,
complex * complex as formula2
from localView1)
select *
from localView2
Nor in MsSql
SELECT col1 AS o, e = CASE WHEN o < GETDATE() THEN o ELSE GETDATE() END
FROM Table1
Returns:
Msg 207, Level 16, State 3, Line 1
Invalid column name 'o'.
Msg 207, Level 16, State 3, Line 1
Invalid column name 'o'.
However if I change to CASE WHEN col1... THEN col1 it works
If you write only equal condition just:
Select Case columns1 When 0 then 'Value1'
when 1 then 'Value2' else 'Unknown' End
If you want to write greater , Less then or equal you must do like this:
Select Case When [ColumnsName] >0 then 'value1' When [ColumnsName]=0 Or [ColumnsName]<0 then
'value2'
Else
'Unkownvalue' End
From tablename
Thanks
Mr.Buntha Khin
SELECT
a AS [blabla a],
b [blabla b],
CASE c
WHEN 1 THEN 'aaa'
WHEN 2 THEN 'bbb'
ELSE 'unknown'
END AS [my alias],
d AS [blabla d]
FROM mytable
Not in MySQL. I tried it and I get the following error:
ERROR 1054 (42S22): Unknown column 'a' in 'field list'
In MySql, alice name may not work, therefore put the original column name in the CASE statement
SELECT col1 as a, CASE WHEN col1 = 'test' THEN 'yes' END as value FROM table;
Sometimes above query also may return error, I don`t know why (I faced this problem in my two different development machine). Therefore put the CASE statement into the "(...)" as below:
SELECT col1 as a, (CASE WHEN col1 = 'test' THEN 'yes' END) as value FROM table;
Yes, you just need to add a parenthesis :
SELECT col1 as a, (CASE WHEN a = 'test' THEN 'yes' END) as value FROM table;
make it so easy.
select columnnameshow = (CASE tipoventa
when 'CONTADO' then 'contadito'
when 'CREDITO' then 'cred'
else 'no result'
end) from Promocion.Promocion