Append 2 tables with identical fields - sql

I have 2 tables with identical columns. Is there a way I can append them together. For example if my tables are:
Table 1:
ID Age
1 21
2 26
3 19
4 40
Table 2:
ID Age
6 29
8 40
10 35
I'd like the desired output to be:
ID Age
1 21
2 26
3 19
4 40
6 29
8 40
10 35
Is there a way I can append these 2 tables. Being new to SQL I tried using insert but couldn't do much about it.
Would be great if some one can help out

You can use a UNION ALL query:
SELECT ID, Age
FROM table1
UNION ALL
SELECT ID, Age
FROM table2
This will return all rows from each table in a single result. Here is a demo.

Use Union rather Union All if you want unique values based on two tables.
https://stackoverflow.com/a/49928/2745294
SELECT ID, Age
FROM table1
UNION
SELECT ID, Age
FROM table2

Related

SQL How to SUM rows in second column if first column contain

View of a table
ID
kWh
1
3
1
10
1
8
1
11
2
12
2
4
2
7
2
8
3
3
3
4
3
5
I want to recive
ID
kWh
1
32
2
31
3
12
The table itself is more complex and larger. But the point is this. How can this be done? And I can't know in advance the ID numbers of the first column.
SELECT T.ID,SUM(T.KWH)SUM_KWH
FROM YOUR_TABLE T
GROUP BY T.ID
Do you need this one?
Let's assume your database name is 'testdb' and table name is 'table1'.
SELECT * FROM testdb.table1;
SELECT id, SUM(kwh) AS "kwh2"
FROM stack.table1
WHERE id = 1
keep running the query will all (ids). you will get output.
By following this query you will get desired output.
Hope this helps.

Sql getting MAX and MIN values based on two columns for the ids from two others

I'm having difficulties figuring a query out, would someone be able to assist me with this?
Problem: 4 columns that represent results for the 2 separate tests. One of them taken in UK and another in US. Both of them are the same test and I need to find the highest and lowest score for the test taken in both countries. I also need to avoid using subqueries and temporary tables. Would appreciate theoretical ideas and actual solutions for the problem.
The table looks like this:
ResultID Test_UK Test_US Test_UK_Score Test_US_Score
1 1 2 48 11
2 4 1 21 24
3 3 1 55 71
4 5 6 18 78
5 7 4 19 49
6 1 3 23 69
7 5 2 98 35
8 6 7 41 47
The desired results I'm looking for:
TestID HighestScore LowestScore
1 71 23
2 35 11
3 69 55
4 49 21
5 98 18
6 78 41
7 47 19
I tried implementing a case of comparison, but I still ended up with subquery to pull out the final results. Also tried union, but it ends up in a sub query again. As far as I can think it shoul be a case when then query, but can't really come up with the logic for it, as it requires to match the ID's of the tests.
Thank you!
What I've tried and got the best results (still wrong)
select v.TestID,
max(case when Test_US_Score > Test_UK_Score then Test_UK_Score else null end) MaxS,
min(case when Test_UK_Score > Test_US_Score then Test_US_Score else null end) MinS
FROM ResultsDB rDB CROSS APPLY
(VALUES (Test_UK, 1), (Test_US, 0)
) V(testID, amount)
GROUP BY v.TestID
Extra
The answer provided by M. Kanarkowski is a perfect solution. I'm no expert on CTE, and a bit confused, how would it be possible to adapt this query to return the result ID of the row that min and max were found.
something like this:
TestID Result_ID_Max Result_ID_Min
1 3 6
2 7 1
3 6 3
Extra 2
The desired results of the query would me something like this.
The two last columns represent the IDs of the rows from the original table where the max and min values were found.
TestID HighestScore LowestScore Result_ID_Of_Max Result_ID_Of_Min
1 71 23 3 6
2 35 11 7 1
3 69 55 6 3
For example you can use union to have results from both countries togehter and then just pick the maximum and the minimum for your data.
with cte as (
select Test_UK as TestID, Test_UK_Score as score from yourTable
union all
select Test_US as TestID, Test_US_Score as score from yourTable
)
select
TestID
,max(score) as HighestScore
,min(score) as LowestScore
from cte
group by TestID
order by TestID
Extra:
I assumed that you want to have the additional column with the previous result. If not just take the above select and replace Test_UK_Score and Test_US_Score with ResultID.
with cte as (
select Test_UK as TestID, Test_UK_Score as score, ResultID from yourTable
union all
select Test_US as TestID, Test_US_Score as score, ResultID from yourTable
)
select
TestID
,max(score) as HighestScore
,min(score) as LowestScore
,max(ResultID) as Result_ID_Max
,min(ResultID) as Result_ID_Min
from cte
group by TestID
order by TestID

SQL copy from one table to another with changing ID value

I have two tables:
A
ID VALUE
----------
1 7
2 5
3 44
4 982
5 1
6 0
7 671
B
ID VALUE
---------------
1 6
2 6
3 77
4 22
How do I copy data from #B to #A to get a different ID (one bigger than the MAX in #A)? For example I need to get
ID VALUE
1 7
2 5
3 44
4 982
5 1
6 0
7 671
8 6
9 6
10 77
11 22
Either make it an IDENTITY column which auto-increments, or this:
INSERT INTO A
SELECT b.ID + (SELECT MAX(ID) FROM A) AS ID, b.Value
FROM B
DEMO
The select is slightly different if the ID in table B has gaps. Then those gaps are transferred.
If the ID column in TableA is not already set to auto-increment, do the following command:
ALTER TABLE TableA MODIFY COLUMN ID INT auto_increment
Now you can just insert all the records from TableB into TableA:
INSERT INTO TableA (VALUE)
SELECT VALUE
FROM TableB
It is not a great idea to rely on the business logic in your query to maintain the order of the ID column. Instead, let SQL take care of it for you; it was designed for this purpose.

Fussion of two SQlite3 tables into new one with sum of value in a column

I have two SQLite3 tables in two separate sqlite files:
Table1 (in file1):
Id Number
----- ------
21 1
22 2
23 3
24 4
and Table2 (in file2):
Id Number
----- ------
21 15
32 16
33 17
34 18
I would like to produce a new table, which accumulates values of Number if there is match. So I would like an output:
TableSummary (should be in Newfile or in file1):
Id Number
----- ------
21 16
22 2
23 3
24 4
32 16
33 17
34 18
What kind of statement I should use to achieve the result?
First, use UNION ALL to combine both tables:
SELECT Id, Number FROM Table1
UNION ALL
SELECT Id, Number FROM Table2
Then combine the duplicates by using GROUP BY:
SELECT Id, SUM(Number)
FROM (SELECT Id, Number FROM Table1
UNION ALL
SELECT Id, Number FROM Table2)
GROUP BY Id

Writing a query for figuring out non-existent parent IDs in SQL

I have a table (Table1) wherein some columns reference another table (Table2) by id. it looks something like this:
Table 1
ID Column 1 Column 2 Column 3
3 15 16 0
4 19 0 0
5 21 22 23
6 0 0 0
7 25 26 0
8 27 0 0
Table 2
ID String
15 data
16 data
19 data
21 data
etc.
I am trying to write a query that returns results like this:
Table2ID Table1ID
15 3
16 3
19 4
21 5
22 5
23 5
25 7
etc.
There is no reference to any sort of parent in Table 2, so I'm trying to figure out the best way to query this. Any help would be much obliged, as my SQL experience is about less than two weeks worth.
You can use union (or union all) to combine the results of multiple queries. There are some rules you must follow in order to do this. First, each query must return the same number of columns. Next, each column does not necessarily need to return the same data type, but you can get unexpected errors if they are different data types so you are MUCH better of if each query returns the same data type for each column.
Just to be clear, when you have multiple columns, each query should return the same data type for the first column, and each query should return the same data type for the second column, but the data types for the first column do not need to match the data type of the second column.
Select Column1 As Table2ID, ID As Table1ID From Table1
Union All
Select Column2, ID From Table1
Union All
Select Column3, ID From Table1
SELECT Table2ID,t1.ID
FROM Table2 t2
JOIN Table1 t1
ON t2.Table1ID=t1.Col1 OR t2.Table1ID=t1.Col2 OR t2.Table1ID=t1.Col3