I am performing a union between two tables. In order to make the two tables consistent for a UNION, I need to add a dummy column.
One table has DATE field whereas the other table does not have that field. How can I create the dummy DATE field which can either be '' (blank) or NULL?
I am trying something like this in DB2
TO_DATE('','MM/DD/YYYY') AS DUMMY_DATE
Just select null.
select col1, datecol from table1
union all
select col1, null from table2
Just use NULL as column:
NULL as Dummy
Related
I have two tables:
This is table1:
customer_name|customer_id|customer_phone|customer_birthday|(10 more columns that don't match with table2)
This is table2:
customer_name|customer_id|customer_phone|customer_status|(141 columns)
table1.customer_id is numeric
table2.customer_id is alphanumeric
table2 was imported from an old database and contains legacy customer data
How do I get data from both tables while only specifying the columns I want from each table.
The two tables have no overlapping data.
Please help me with this. I am not sure how to resolve it
Using SQL Server 2019
You could simply combine data from the both tables using UNION:
select customer_name, cast(customer_id as varchar), customer_phone, customer_birthday, null customer_status from table1
union all -- no need to do a useless distinct
select customer_name, customer_id, customer_phone, null as customer_birthday, customer_status from table2
As long as you have same column names, you can make a list of columns and use UNION like below:
SELECT col1, col2, col3, ...
FROM t1
UNION
SELECT col1, col2, col3
FROM t2;
I have a table which have 2 columns of dates, and I want to look for the closest date in the future (Select min()?) searching in both columns, is that possible?
For example, if I have column1=23/11/2014 and column2=22/11/2014, in the same row, I want get 22/11/2014
I hope it is clear enough, ask me if it doesn't.
Greetings.
In a single table use CASE
SELECT CASE column1 < column2
THEN column1
ELSE column2
END mindate
FROM yourtable
If you have the date column in multiple tables, just replace yourtable with your tables JOINed together
This is what the least() function is for:
select least(column_1, column_2)
from your_table;
min() is an aggregate function that operates on a single column but multiple rows.
try something like this:
select min(date) from(
select a.date date from table1 a
union
select b.date2 date from table1 b
)
My select-statement looks like:
Select column1, column2
From Table1
Group By column2
column1 is a CLOB and I want to receive one of the values that is part of one group. I know they are all the same so it doesn't matter which one I get. I've tried functions like MIN and MAX but they don't accept CLOB as a type.
To be clear I don't want to aggregate the CLOBs just pick one of them.
This is a simplification of the actual SELECT statement and the GROUP BY clause is necessary.
So with this data:
column1 column2
qwerty 1
qwerty 1
asdfgh 2
asdfgh 2
I want to get:
qwerty 1
asdfgh 2
Any idea how this could be done?
A CLOB value cannot be used for grouping or inside a distinct clause.
The only chance you have is to convert the CLOB to a varchar but that means you cannot compare the complete contents of the column (note: those are columns, not rows). If you are certain that all your CLOB values are smaller than 8000 bytes, you can use something like this:
select min(dbms_lob.substr(column1)), column2
from foo
group by column2;
You can use sub-queries.
Add unique column ID and then:
SELECT t1.col2,t2.col1
FROM
(SELECT max(ID) as IDM, col2 FROM Table1 GROUP BY col2) t1
LEFT JOIN
Table1 t2
ON t1.IDM=t2.ID
Try something like this:
SELECT DISTINCT row1, row2 FROM Table1;
I have a problem, where first field in unique/distinct and the second field is like concatenated string. i am looking for the distinct rows to be displayed in the second concatenated column not on the first column and i need in the same seq of columns, for ex:
1stcolumn 2ndcolumn(concatenated)
100 ABC-123-PQR
101 ABC-123-PQR
102 ABC-123-PQR
104 ABC-123-STU
in the above example i need to select only ABC-123-PQR AND ABC-123-STU, i don't care on the first column values.
Why not just use the DISTINCT keyword?
SELECT DISTINCT col2 FROM mytable
SELECT MAX(col1), col2 FROM mytable GROUP BY col2
Given a couple of simple tables like so:
create table R(foo text);
create table S(bar text);
If I were to union them together in a query, what do I call the column?
select T.????
from (
select foo
from R
union
select bar
from S) as T;
Now, in mysql, I can apparently refer to the column of T as 'foo' -- the name of the matching column for the first relation in the union. In sqlite3, however, that doesn't seem to work. Is there a way to do it that's standard across all SQL implementations?
If not, how about just for sqlite3?
Correction: sqlite3 does allow you to refer to T's column as 'foo' after all! Oops!
Try to give an alias to columns;
select T.Col1
from (
select foo as Col1
from R
union
select bar as Col1
from S) as T;
or If the name of column is not necessary then T.* will be enough.
Although there is no spelled rule, we can use the column names from the first subquery in the union query to fetch the union results.
you only need column aliases only in first select (tested in SQl Server 2008 R2)
select T.Col1
from (
select 'val1' as Col1
union
select 'val2'
union
select 'val3'
) as T;