SQL combine two tables into one table and add a new column - sql

I need to combine two tables into one. Ans also, add a column (assign an int value) to the new table on SQL. So that the rows from table1 and ones from table2 are assigned with different values.
Example,
table1
ID1 ID2 ID3 VALUE
table2
ID1 ID2 ID3 VALUE
table3
ID1 ID2 ID3 VALUE
i need to combine table3 and table2 into a new table and add a new column
table_new
top_id ID2 ID3 VALUE
It is Netezza SQL.
INSERT INTO new_table
SELECT *
FROM
(
SELECT * , **sum (table1.VALUE * table2.VALUE) AS new_value**
FROM table1
JOIN
table2
ON table1.id1 = table2.id1
GROUP BY table2.id2, table2.id3
) AS tt_a # here, I need to add a new column to tt, call it as top_id and also assign an int
# value to it, such as 80
UNION ALL
SELECT *
FROM
(
SELECT * , **sum (table1.VALUE * table3.VALUE) AS new_value**
FROM table1
JOIN
table3
ON table1.id1 = table3.id1
GROUP BY table3.id2, table3.id3
) AS tt_b # here, I need to add a new column to tt, call it as top_id and also assign an
# int value to it, such as 81
**ORDER BY top_id**
I got error:
I em new to SQL.
ERROR [HY000] ERROR: 0 : Functionality not implemented
Any help would be appreciated.

It's not entirely clear what you want, but I'm going to assume it's a UNION ALL example.
UNION ALL will combine the result sets of 2 or more SELECT statements. It returns all rows from each select statement (even if the row exists in more than one of the SELECT statements) as 1 result set.
example:
select '81' as top_id,id2,id3 from T1 group by id2,id3
UNION ALL
select '79' as top_id,id3,id400 from T1 group by id3, id400
UNION ALL
SELECT '80' as top_id,id2,id3
FROM table1
JOIN
table2
ON table1.id1 = table2.id1
GROUP BY table1.id2, table2.id3
;
Each select statement (this example combines 3) should be a valid query all by itself and should return the same amount of columns with the same datatypes.
reference: http://www.techonthenet.com/sql/union_all.php

Related

SQL Query to Join Two Tables where data in one table is transposed

I'm trying to join two tables but not getting the desired results. Im new to SQL and will need help to joining these two tables.
The data in Table 2 is transposed and the table will only be populated only when the "value" field is populated with random values.
Details of Table 1 and Table 2 and expected output
You could use CTEs to split up the impact from outcome. Also, your screenshot had an error for task_id 6.
with t_impact as (
select task_id, value
from table2
where name = 'task_impact'
),
t_outcome as (
select task_id, value
from table2
where name = 'task_outcome'
)
select distinct t1.id,
t1.title,
i.value as task_impact,
o.value as task_outcome
from table1 t1
left join t_impact i
on t1.id = i.task_id
left join t_outcome o
on t1.id = o.task_id
order by t1.id
DB-fiddle found here.

How to combine multiple SELECT statements into a single query & get a single result output

I have multiple SELECT queries which is ran against different tables.
The output of all the queries have the same number of rows (every query when ran individually will have the same number of rows). Is there a way I can combine the output of all these queries into a single result? (Keep out from first query and add the output of next query as a column to the output of the next query). I dont want to save these tables into database as I am just doing some validation testing.
Example:
SELECT AAA,BBB,CCC FROM Table1
SELECT Table2.DDD, Table1.AAA
FROM Table2
INNER JOIN Table1
ON Table1.AAA = Table2.AAA
I tried writing combining the query as
SELECT Table1.AAA,Table1.BBB,Table1.CCC,T1.DDD
FROM Table1,
(SELECT Table2.DDD, Table1.AAA
FROM Table2
INNER JOIN Table1
ON Table1.AAA = Table2.AAA)T1
I tried doing the above combined query, but instead of getting 11 rows as output (both queries above had result of 11 rows), I am getting 35 rows as output.
Hope the question made sense!
You'll need to specify a criteria to match each row the first query with which row of the second query.
If, for example, the column AAA is unique in both queries and you want to match rows with the same values you could do:
select a.*, b.*
from (
SELECT AAA,BBB,CCC FROM Table1
) a
full join join (
SELECT Table2.DDD, Table1.AAA
FROM Table2
INNER JOIN Table1
ON Table1.AAA = Table2.AAA
) b on b.aaa = a.aaa
If there aren't any clear matching rules, you can produce an artificial row number on each result set and use it to match rows. For example:
select
a.aaa, a.bbb, a.ccc,
b.ddd, b.aaa
from (
SELECT AAA, BBB, CCC,
row_number() over(order by aaa) as rn
FROM Table1
) a
full join join (
SELECT Table2.DDD, Table1.AAA,
row_number() over(order by table1.aaa, table2.ddd) as rn
FROM Table2
INNER JOIN Table1
ON Table1.AAA = Table2.AAA
) b on b.rn = a.rn
If you have several results and want to have all of them as additional columns you can simply use ",":
create table temp1 as select '1' as c1 from DUAL;
create table temp2 as select '2' as c2 from DUAL;
create table temp3 as select '3' as c3 from DUAL;
select a.c1, b.c2, c.c3 from temp1 a, (select c2 from temp2) b, (select c3 from temp3) c;
An alternative could also be that you want to have all the results as additional rows then you would use UNION ALL between the individual results.

SQL Server: add values of small tables to the values of big table without losing the dimensions of the big table?

I have 3 tables. I want to add corresponding values from second table and third table to the first table in the picture below. Each table has an ID by which they can be matched, ... field in the pictures. The first table has 1531 rows with an ID column and 8 other columns. This table, the top table in the pictures, is almost full of zeroes.
I have tried to join the tables in different ways but the problem is that each table has different number of rows and hence different number unique IDs. The top table has all IDs.
Is there some convenient way to add the second table to the first table and then the third table to that result?
Result of Left Join as suggested Suzena: why do the numbers not get summed up together?
Method1: Joins
select a.id,(a.col1 + b.col1+c.col1) as col1, (a.col2 + b.col2 + c.col2) as col2, (a.col3 + b.col3 + c.col3) as col3
from
table1 a
left join
table2 b
on a.id = b.id
left join
table3 c
on a.id = c.id;
Method2: Unions
select id,sum(col1) col1, sum(col2) col2, sum(col3) col3
from
(
select id,col1,col2,col3
from table1
union all
select id,col1,col2,col3
from table2
union all
select id,col1,col2,col3
from table3
) t
group by id
Let me know if you have any different criteria.
Method 3: having different number of fields so use NULL or 0
SELECT
[MID],
SUM([KEVAT 201501-04]) AS 'KEVAT 201501-04',
SUM([KESA 201504-06]) AS 'KESA 201504-06',
SUM([SYKSY 201507-09]) AS 'SYKSY 201507-09',
SUM([TALVI 201510-12]) AS 'TALVI 201510-12',
SUM([KEVAT 201601-04]) AS 'KEVAT 201601-04',
SUM([KESA 201604-06]) AS 'KESA 201604-06',
SUM([SYKSY 201607-09]) AS 'SYKSY 201607-09',
SUM([TALVI 201610-12]) AS 'TALVI 201610-12'
FROM
(
SELECT * FROM TABLE1
UNION ALL
SELECT [MID]
,0 AS 'KEVAT 201501-04'
,0 AS 'KESA 201504-06'
,0 AS 'SYKSY 201507-09'
,0 AS 'TALVI 201510-12'
,[KEVAT 201601-04]
,[KESA 201604-06]
,[SYKSY 201607-09]
,[TALVI 201610-12]
FROM TABLE2
UNION ALL
SELECT [MID]
,[KEVAT 201501-04]
,[KESA 201504-06]
,[SYKSY 201507-09]
,[TALVI 201510-12]
,0 AS 'KEVAT 201601-04'
,0 AS 'KESA 201604-06'
,0 AS 'SYKSY 201607-09'
,0 AS 'TALVI 201610-12'
FROM TABLE3
) a
GROUP BY [MID]
If i understand your question, you could use an union. Something like:
insert into table1(col1,col2,col3,col4)
(select col1,col2,col3,col4 from table2 union
select col1,col2,col3,col4 from table3)
The names of the columns of table2 and table3 must match. Use alias for that.
Try using MERGE
--Get data from table 2 and merge into table 1
MERGE Table_1 AS TARGET
USING (SELECT [ID]
,[KEVAT 201501-04]
,[KESA 201504-06]
,[SYKSY 201507-09]
,[TALVI 201510-12] FROM Table_2) AS SOURCE
ON (TARGET.ID = SOURCE.ID)
WHEN MATCHED
THEN UPDATE SET
TARGET.[KEVAT 201501-04] = SOURCE.[KEVAT 201501-04],
TARGET.[KESA 201504-06] = SOURCE.[KESA 201504-06],
TARGET.[SYKSY 201507-09] = SOURCE.[SYKSY 201507-09],
TARGET.[TALVI 201510-12] = SOURCE.[TALVI 201510-12];
GO
--Get data from table 3 and merge into table 1
MERGE Table_1 AS TARGET
USING (SELECT [ID]
,[KEVAT 201601-01]
,[KESA 201604-06]
,[SYKSY 201607-09]
,[TALVI 201610-12] FROM Table_3) AS SOURCE
ON (TARGET.ID = SOURCE.ID)
WHEN MATCHED
THEN UPDATE SET
TARGET.[KEVAT 201601-01] = SOURCE.[KEVAT 201601-01],
TARGET.[KESA 201604-06] = SOURCE.[KESA 201604-06],
TARGET.[SYKSY 201607-09] = SOURCE.[SYKSY 201607-09],
TARGET.[TALVI 201610-12] = SOURCE.[TALVI 201610-12];
GO

Fastest SQL & HQL Query for two tables

Table1: Columns A, B, C
Table2: Columns A, B, C
Table 2 is a copy of Table 1 with different data. Assume all columns to be varchar
Looking for a single efficient query which can fetch:
Columns A, B, C from Table1
Additional Rows from Table2 where values of Table2.A are not present in Table1.A
Any differences between the Oracle SQL & HQL for the same query will be appreciated.
I'm fiddling with Joins, Unions & Minus but not able to get the correct combination.
SQL:
SELECT *
FROM Table1
UNION ALL
SELECT *
FROM Table2 T2
WHERE NOT EXISTS(
SELECT 'X' FROM Table1 T1
WHERE T1.A = T2.A
)
HQL:
You must execute two different query an discard the element by Table2 result in a Java loop because in HQL doesn't exist UNION command.
Alternatatively you can write the first query for Table1 and the second query must have a not in clause to discard Table1 A field.
Solution 1:
Query 1:
SELECT * FROM Table1
Query 2:
SELECT * FROM Table2
and then you apply a discard loop in Java code
Solution 2:
Query 1:
SELECT * FROM Table1
Query 2:
SELECT * FROM Table2 WHERE Table2.A not in (SELECT Table1.A from Table1)
This query returns all rows in table1, plus all rows in table2 which does not exist in table1, given that column a is the common key.
select a,b,c
from table1
union
all
select a,b,c
from table2
where a not in(select a from table1);
There may be different options available depending on the relative sizes of table1 and table2 and the expected overlap.

mysql - union tables by unique field

I have two tables with the same structure:
id name
1 Merry
2 Mike
and
id name
1 Mike
2 Alis
I need to union second table to first with keeping unique names, so that result is:
id name
1 Merry
2 Mike
3 Alis
Is it possible to do this with MySQL query, without using php script?
This is not a join (set multiplication), this is a union (set addition).
SELECT #r := #r + 1 AS id, name
FROM (
SELECT #r := 0
) vars,
(
SELECT name
FROM table1
UNION
SELECT name
FROM table2
) q
This will select all names from table1 and combine those with all the names from table2 which are not in table1.
(
select *
from table1
)
union
(
select *
from table2 t2
left join table1 t1 on t2.name = t1.name
where t1.id is null
)
Use:
SELECT a.id,
a.name
FROM TABLE_A a
UNION
SELECT b.id,
b.name
FROM TABLE_B b
UNION will remove duplicates.
As commented, it all depends on what your 'id' means, cause in the example, it means nothing.
SELECT DISTINCT(name) FROM t1 JOIN t2 ON something
if you only want the names
SELECT SUM(something), name FROM t1 JOIN t2 ON something GROUP BY name
if you want to do some group by
SELECT DISTINCT(name) FROM t1 JOIN t2 ON t1.id = t2.id
if the id's are the same
SELECT DISTINCT COALESCE(t1.name,t2.name) FROM
mytable t1 LEFT JOIN mytable t2 ON (t1.name=t2.name);
will get you a list of unique names from the 2 tables. If you want them to get new ids (like Alis does in your desired results), that's something else and requires the answers to a couple of questions:
do any of the names need to maintain their previous id. And if they do, which table's id should be preferred?
why do you have 2 tables with the same structure? ie what are you trying to accomplish when you generate the unique name list?