Create new record if column contains value - sql

Wanted to know if I could "artificially" insert new records when a record contains a value for a specific column. For example say I have this table in my database with the following two records:
Column1 Column2 Column3
-------------------------
DataA1 DataA2 null
DataB1 DataB2 DataB3
Now Column3 is the column I want to trigger an extra row if there is a value. Column3 is essentially Column2 but with another value (this is non-normalized and I can't change it so I need to resort to a query instead). So I want to create a query that returns 3 rows using the example above and it should come out like this:
DataA1 DataA2
DataB1 DataB2
DataB1 DataB3
How do I write my sql to return the results above?

Use union all:
SELECT Column1, Column2
FROM TableName
WHERE Column3 IS NULL
UNION ALL
SELECT Column1, Column3
FROM TableName
WHERE Column3 IS NOT NULL

Not totally sure what you want here but I think you are looking for something like this.
select Column1
, Column2
from SomeTable
where Column2 is not null
UNION ALL
select Column1
, Column3
from SomeTable
where Column3 is not null

You could use a UNION statement to merge a result set that uses the third column as the second column when the third column is not null:
SELECT column1, column2
FROM Sample
UNION
SELECT column1, column3
FROM Sample
WHERE column3 IS NOT NULL
http://sqlfiddle.com/#!9/42ca15/6

Related

SQL self join to eradicate duplicate keys

I have a table with below columns:
Column1
Column2
Column3
A
Hello
NULL
A
NULL
WORLD
I want the above table to transform like below:
Column1
Column2
Column3
A
Hello
WORLD
I'm using Snowflake DataWarehouse. Need help in the above transformation using SQL
select column1,
max(column2) as column2,
max(column3) as column3
from your_table
group by column1;

How do I set a column to return a value in a select statement

So if i do a statement such as
Select column1, column2
from dbo.table
How would i return column1 as the actual data and column2 as a fixed value, if i wanted to have column 2 to always show the same value.Im building an output file and need to put in some predefined results.
Column1 Column2
------- -------
dog A
cat A
bird A
thanks a lot
You would then select a constant value:
Select column1, 'A' as column2
from dbo.table
select a constant value :
SELECT t.Column1, 'A' AS Column2
FROM dbo.table t

How to combine multiple columns into one column?

I'm writing a query and want the results in one column
My current results return like this
Column1 Column2 column3
1 A CAT
I want the results to return like this
Column1
1
A
CAT
SELECT Column1 FROM TableName
UNION ALL
SELECT Column2 FROM TableName
UNION ALL
SELECT Column3 FROM TableName
If you don't want duplicate values, use UNION instead of UNION ALL.
You can also do this using UNPIVOT operator
SELECT Column123
FROM
(
SELECT Column1, Column2, Column3
FROM TableName
) AS tmp
UNPIVOT
(
Column123 FOR ColumnAll IN (Column1, Column2, Column3)
) AS unpvt;
https://www.w3schools.com/sql/sql_union.asp
https://www.mssqltips.com/sqlservertip/3000/use-sql-servers-unpivot-operator-to-help-normalize-output/
The answer is.. it depends..
If the number of columns are unknown.. then use unpivot as UZI has suggested
if you know all columns and is a small finite set..
you can simply go
Select
column1
from table
union all
select column2
from table
union all
select column3
from table
The Cartesian product of the T table with derived table of 3 rows.(each row of #t is presented 3 times, for а=1 and а=2 and а=3). For the first case we take value from Column1,
and for the second - from Column2 and for the Third - from Column3.
Here, certainly, there is both union and join but, in my opinion, the title's question means single scanning the table.
CREATE TABLE #t (Column1 NVARCHAR(25),Column2 NVARCHAR(25), column3 NVARCHAR(25))
INSERT INTO #t
SELECT '1','A','CAT'
SELECT
CASE a WHEN 1 THEN Column1 WHEN 2 THEN Column2 ELSE column3 END col
FROM #t, (SELECT 1 a UNION ALL SELECT 2 UNION ALL SELECT 3) B
DROP TABLE #t

SQL Server : how to find ids where columns have different values

I have a table like this:
Column1 Column2
---------------
1 1
1 2
1 3
1 4
2 1
2 1
2 1
2 1
In column1 one there are 2 different ids, in column2 there are different values for each id from column1.
How can I get the id from column1 where not all ids from column2 are the same? So in this instance the output should be 1 - because they have all different values in column2, where id from column1 has all 1's in column2
Just use group by and having:
select column1
from table t
group by column1
having min(column2) <> max(column2);
Note: you could also use count(distinct), but that has more overhead than min() and max().
Similar logic can be used if the second column could be NULL. That doesn't appear in the sample data so it doesn't seem worth including it in the logic unless the OP specifically says this is a possibility.
Try like this:
select Column1
from yourTable
group by Column1
having count(DISTINCT column2) > 1;
I would think something like this should do the job:
SELECT t.column1 FROM table t
GROUP BY t.column1
HAVING COUNT(DISTINCT t.column2) > 1
This approach will handle the case where a null is an acceptable value in column2.
select column1
from
(
select distinct column1, column2
from yourTable
) t
group by column1
having count(*) > 1

appending 2 columns into one list in sql

I have 2 tables and each table has some 3 columns. i want to get one column such that one column from each table are apended one after the other
eg:- suppose one column in a table contains hai, how, are, you.
and another column in another column contains i, am, fine.
i want a query which gives hai, how, are, you,i,am,fine. in just one column
can anybody give a query for this in sql...
If I understand your schema correctly you have this
Table1: Column1
hai,
how,
are,
you.
Table2: Column2
i,
am,
fine.
Do This:
Insert Into Table1 (Column1)
Select Column2 From Table2
You will get this:
Table1: Column1
hai,
how,
are,
you.
i,
am,
fine.
If you have 3 Columns
Then just do this:
Insert Into Table1 (Column1, Column2, Column3) //the (Column1, Column2, Column3) is not neccessary if those are the only columns in your Table1
Select Column1, Column2, Column3 From Table2 //the Select Column1, Column2, Column3 could become Select * if those are the only columns of your Table2
EDIT: Do this if you don't want to modify any tables.
Select Column1, Column2, Column3
From Table1
UNION ALL
Select Column1, Column2, Column3
From Table2
Your question isn't very clear. One interpretation of it is that you want to UNION the two:
select column
from table1
union
select column
from table2;
If you really want all rows from both tables (and not the distinct values), UNION ALL will be faster than UNION.
If you want the rows in a certain order be sure to specify an ORDER BY clause.