Merge data from two tables into single column of another table - sql

How to merge data from multiple tables into single column of another table.
Example:
Table A
Col1 | Col2 | Col3
10
20
Table B
Col1 | Col2 | Col3
13
99
I want my o/p in Table C in Col1 as
Col1
10
20
13
99
I did (part of query)
Select Col1 from A
Union
Select Col1 from B
but it is not giving me this desired result

The SELECT appears correct (you may want to use UNION ALL instead of UNION to avoid elimination of duplicates).
If you want the results to be in the third table C, you need to make an INSERT from your SELECT, like this:
INSERT INTO C (Col1)
(
SELECT Col1 from A
UNION ALL
SELECT Col1 from B
)

Related

Copy created primary key ID back to the temp table from which new row values are taken?

I have a temp table #temp
tid tcol1 tcol2
--------------------
null 1 a
null 2 b
null 3 c
I am using #temp to insert values into table table1
select tcol1, tcol2 into table1 from #temp. Now the table1 looks like
id col1 col2
---------------------
1 1 a
2 2 b
3 3 c
Now I want to copy the ID back to the #temp table. I am trying to get #temp table like
tid tcol1 tcol2
--------------------
1 1 a
2 2 b
3 3 c
Is this possible. If yes how?
Not sure in your real application, but here you can delete rows from #temp and insert from table1.
Delete from #temp
Insert into #temp
Select id, tcol1, tcol2 from table1
But for sure a more sensible way would be not to copy the data back and forth. Generate id's before entering into #temp.
Drop #temp and then
SELECT * INTO #temp FROM table1

SQL: Copy data from one table into another using unique key present in both tables

I have two tables that contain the same unique key. I need to match those keys and then copy data from table 2 into table 1
Original:
Key COL1 COL2 Key COL3
1 01 NULL 1 05
2 02 NULL 2 12
3 03 NULL 3 27
Required:
Key COL1 COL2 Key COL3
1 01 05 1 05
2 02 12 2 12
3 03 27 3 27
Thank you for the help.
Your best answer is probably an UPDATE FROM a lookup based on the two tables (for SQL Server, some say use JOIN, some say there's no need, and without the join is more concise).
See some examples here: SQL update from one Table to another based on a ID match
For convenience, here's a query adapted for your scenario:
UPDATE Table1 SET
Col1 = Table2.Col1,
Col2 = Table2.Col2,
Col3 = Table2.Col3,
FROM Table2
WHERE Table2.Key = Table1.Key
TRY this:
update table1
set col2 = (
select col3
from table2
where table2.key=table1.key
)
where exists (
select *
from table2
where table2.key=table1.key
);

Pivot duplicate column names and get all values for columns

EXPLAINATION
Imagine that I have 2 tables. FormFields where are stored column names as values, which should be pivoted and second table FilledValues with user's filled values with FormFieldId provided.
PROBLEM
As you see (below in SAMPLE section) in FormFields table I have duplicate names, but different ID's. I need to make that after joining tables, all values from FilledValues table will be assiged to column names, not to Id's.
What I need better you will see in OUTPUT section below.
SAMPLE DATA
FormFields
ID Name GroupId
1 col1 1
2 col2 1
3 col3 1
4 col1 2
5 col2 2
6 col3 2
FilledValues
ID Name FormFieldId GroupID
1 a 2 1
2 b 3 1
3 c 1 1
4 d 4 2
5 e 6 2
6 f 5 2
OUTPUT FOR NOW
col1 col2 col3
c a b -- As you see It returning only values for FormFieldId 1 2 3
-- d, e, f are lost that because It have duplicate col names, but different id's
DESIRED OUTPUT
col1 col2 col3
c a b
e f d
QUERY
SELECT * FROM
(
SELECT FF.Name AS NamePiv,
FV.Name AS Val1
FROM FormFields FF
JOIN FilledValues FV ON FF.Id = FV.FormFieldId
) x
PIVOT
(
MIN(Val1)
FOR NamePiv IN ([col1],[col2],[col3])
) piv
SQL FIDDLE
How can I produce the OUTPUT with the multiple rows?
Since you are using PIVOT the data is being aggregated so you only return one value for each column being grouped. You don't have any columns in your subquery that are unique and being used in the grouping aspect of PIVOT to return multiple rows. In order to do this you need some value. If you have a column with a unique value for each "group" then you would use that or you can use a windowing function like row_number().
row_number() will create a sequenced number for each FF.Name meaning if you have 2 col1 you will generate a 1 for a row and a 2 for another row. Once this is included in your subquery, you now have a unique value that is used when aggregating your data and you will return multiple rows:
SELECT [col1],[col2],[col3]
FROM
(
SELECT
FF.Name AS NamePiv,
FV.Name AS Val1,
rn = row_number() over(partition by ff.Name order by fv.Id)
FROM FormFields FF
JOIN FilledValues FV ON FF.Id = FV.FormFieldId
) x
PIVOT
(
MIN(Val1)
FOR NamePiv IN ([col1],[col2],[col3])
) piv;
See SQL Fiddle with Demo. The output is:
| col1 | col2 | col3 |
|------|------|------|
| c | a | b |
| e | f | d |
Just adding GroupId in Pivot source query will fix your problem
SELECT * FROM (
SELECT FF.Name AS NamePiv,
FV.Name AS Val1,
ff.groupid
FROM FormFields FF
JOIN FilledValues FV ON FF.Id = FV.FormFieldId
) x
PIVOT
(
MIN(Val1)
FOR NamePiv IN ([col1],[col2],[col3])
) piv
SQLFIDDLE DEMO
I would be inclined to do this with conditional aggregation:
select max(case when formfieldid % 3 = 1 then name end) as col1,
max(case when formfieldid % 3 = 2 then name end) as col2,
max(case when formfieldid % 3 = 0 then name end) as col3
from FilledValues
group by GroupID;
It is unclear what the rule is for assigning a value to a column. This uses the remainder, which works for your input data.

how to find Duplicate records by comparing the previous records received in a table

I have a table
seq_num create_dt col1 col2 is_load
1 1/1/2014 A B
2 2/1/2014 c D
3 3/1/2014 A B
Everyday data is loaded to the table through loader. I want to write a query where it should find the duplicate data in the current day record by comparing with the previous days records and update the column is_load = 'D'(as of Duplicate) if any duplicate record is found.
so the result should be like
`seq_num create_dt col1 col2 is_load
1 1/1/2014 A B
2 2/1/2014 C D
3 3/1/2014 A B
. .... . .
. .... . .
11 11/3/2014 A B D `
Thanks in advance !!!!!
Try this:
update myTable m
set is_load = 'D'
where exists (select null
from myTable
where m.col1 = col1
and m.col2 = col2
and m.seq_num > seq_num);
This condition m.seq_num > seq_num helps to avoid setting up first entry to 'D'.
If it really matters you can easily change seq_num to create_dt but this can leave some entries which are loaded the same day.
SELECT seq_num, col1,
COUNT(col1) AS Num_Of_Occurrences
FROM table_name
GROUP BY col1
HAVING ( COUNT(col1) > 1 );

Simple INSERT Query in SQL Server

I m looking to a way to insert into database Table T1 from T2 (append operation_
Table 1 : dbo.t1
col1 col2
---- -----
1 ABC
2 ABCr
3 ABCs
4 ABCd
Table 2 : dbo.t2
col1 col2
---- -----
7 ABCe
8 ABCy
Now , table 1 becomes
col1 col2
---- -----
1 ABC
2 ABCr
3 ABCs
4 ABCd
7 ABCe
8 ABCy
SQL query , I m using is:
select *
into dbo.t1
from dbo.t2
I know it would way too simple using #temp table.
I m looking for a way so that I just append the rows from T2 to T1 and keep performance as well. The existing rows of T1 is not touch at all.
Any help would be helpful.
Thanks !!!
Does this answer your question? It will insert all records from Table2 to the end of Table1 (and not touch existing records in Table1)
insert into Table1 (col1, col2) (select col1, col2 from Table2)