MS-Access: Merge two tables "below" each other - sql

I have two tables in my Access-database. They look something like this:
Table1
+--------------+----------+----------+----------+
| Kabelnummer | Column1 | Column2 | Column3 |
+--------------+----------+----------+----------+
| 1 | x | x | x |
+--------------+----------+----------+----------+
| 2 | x | x | x |
+--------------+----------+----------+----------+
| 3 | x | x | x |
+--------------+----------+----------+----------+
| 4 | x | x | x |
+--------------+----------+----------+----------+
table2
+--------------+----------+----------+----------+
| Kabelnummer | Column1 | Column2 | Column3 |
+--------------+----------+----------+----------+
| 1 | x | x | x |
+--------------+----------+----------+----------+
| 2 | x | x | x |
+--------------+----------+----------+----------+
| 3 | x | x | x |
+--------------+----------+----------+----------+
| 4 | x | x | x |
+--------------+----------+----------+----------+
I need a query that gives me 1 table with the data from table1 added to the data from table2:
TableTotal
+--------------+----------+----------+----------+
| Kabelnummer | Column1 | Column2 | Column3 |
+--------------+----------+----------+----------+
| 1 | x | x | x |
+--------------+----------+----------+----------+
| 2 | x | x | x |
+--------------+----------+----------+----------+
| 3 | x | x | x |
+--------------+----------+----------+----------+
| 4 | x | x | x |
+--------------+----------+----------+----------+
| 1 | x | x | x |
+--------------+----------+----------+----------+
| 2 | x | x | x |
+--------------+----------+----------+----------+
| 3 | x | x | x |
+--------------+----------+----------+----------+
| 4 | x | x | x |
+--------------+----------+----------+----------+
The names "Column1", "Column2" and "Column3" are the same in both tables

SELECT *
FROM Table1
UNION
SELECT *
FROM table2;

The question asks for non-distinct values while the current answers provide distinct values. The method below provides non-distinct values such that
SELECT *
FROM Table1
UNION ALL
SELECT *
FROM table2;
which is often more efficient than the union method, particularly with large data sets (not having to compute the distinct).

If your goal is to append the second table to the first one, it can be achieved this way
INSERT INTO TABLE1 SELECT * FROM TABLE2;
The caveat with these other queries is that yes, they do the job, but create a third table with the joined data.

Related

Autonumber rows in select SQL based on column changes

I use a select all statement to retrieve all values from table A. Table A sample is the following:
+---+----+---+
| a | 23 | X |
+---+----+---+
| a | 23 | Y |
+---+----+---+
| a | 24 | X |
+---+----+---+
| a | 24 | Y |
+---+----+---+
| b | 24 | X |
+---+----+---+
| b | 24 | Y |
+---+----+---+
| b | 25 | X |
+---+----+---+
| b | 25 | Y |
+---+----+---+
| b | 25 | Z |
+---+----+---+
For purposes in later stadium of this query, I would like to have a record number for each unique combination of column 1 and 2. For example:
+---+----+---+---+
| a | 23 | X | 1 |
+---+----+---+---+
| a | 23 | Y | 2 |
+---+----+---+---+
| a | 24 | X | 1 |
+---+----+---+---+
| a | 24 | Y | 2 |
+---+----+---+---+
| b | 24 | X | 1 |
+---+----+---+---+
| b | 24 | Y | 2 |
+---+----+---+---+
| b | 25 | X | 1 |
+---+----+---+---+
| b | 25 | Y | 2 |
+---+----+---+---+
| b | 25 | Z | 3 |
+---+----+---+---+
Is this possible to do with SQL and how?
The description of your problem would use dense_rank():
select t.*, dense_rank() over (order by col1, col2)
from t;
Your sample data suggests dense_rank() with partitition by:
select t.*,
dense_rank() over (partition by col1, col2 order by col3) as seqnum
from t;
I believe all you need is
SELECT Distinct
ROW_NUMBER() OVER(ORDER BY Col1 ASC,Col2 ASC) AS Row_num,
Col1, Col2
FROM Table1

SQL insert all data to bridge table, (many to many)

I Have two table like below ;
X table ;
+---+----------+
| id| value |
+---+----------+
| 1 | x value1 |
+---+----------+
| 2 | x value2 |
+---+----------+
| 3 | x value3 |
+---+----------+
Y table ;
+---+----------+
| id| value |
+---+----------+
| 1 | y value1 |
+---+----------+
| 2 | y value2 |
+---+----------+
| 3 | y value3 |
+---+----------+
And I have created new table(x_y table)which has foreign keys for x and y tables ;
And I want to add all data related to each other to new table like below;
x_y table
+----+------+------+
| id | x_id | y_id |
+----+------+------+
| 1 | 1 | 1 |
+----+------+------+
| 2 | 1 | 2 |
+----+------+------+
| 3 | 1 | 3 |
+----+------+------+
| 4 | 2 | 1 |
+----+------+------+
| 5 | 2 | 2 |
+----+------+------+
| 6 | 2 | 3 |
+----+------+------+
| 7 | 3 | 1 |
+----+------+------+
| 8 | 3 | 2 |
+----+------+------+
| 9 | 3 | 3 |
+----+------+------+
how can I add value like this to third table on postgresql script.
This can be done with a cross join and a row_number that generates id's.
select row_number() over(order by x.id,y.id) as id,x.id as x_id,y.id as y_id
from x
cross join y
Presumably, the new table is defined with id as a serial column. If so, you would insert the data by doing:
insert into x_y (x_id, y_id)
select x.id, y.id
from x cross join
y
order by x.id, y.id;

Querying number of children nodes for each node in Hive

I am trying to come up with the best HiveQL query to get a list of rows where the one column would have the number of (direct) children that node has. the database is hierarchical so it looks like this:
| ID | Some other column | ParentID |
+-----------------------------------+
| 1 | XXXXXXXXXX x X X | NULL |
| 2 | XXXXXXXXXX x X X | 1 |
| 3 | XXXXXXXXXX x X X | 2 |
| 4 | XXXXXXXXXX x X X | 1 |
And I am attempting to query it to output something like this:
| ID | Some other column | child count |
+--------------------------------------+
| 1 | XXXXXXXXXX x X X | 2 |
| 2 | XXXXXXXXXX x X X | 1 |
| 3 | XXXXXXXXXX x X X | 0 |
| 4 | XXXXXXXXXX x X X | 0 |
Try something like this with LEFT JOIN.
SELECT a.id,
COALESCE (b.child_count, 0) "child count"
FROM mytable a
LEFT JOIN (SELECT parentid,
Count(*) child_count
FROM mytable
GROUP BY parentid) b
ON a.id = b.parentid;

Merge two tables while doing operations

I'm currently creating two tables using SQL but I need to Union them and it's not clear how to do this when group bys are involved
Select ...
group by email, column 1
Which creates two tables like this:
Table1
+--------------+----------+
| email | Column1 |
+--------------+----------+
| 1 | x |
+--------------+----------+
| 2 | x |
+--------------+----------+
table2
+--------------+----------+
| email | Column1 |
+--------------+----------+
| 1 | x |
+--------------+----------+
| 2 | x |
+--------------+----------+
I want to merge them using a union into this
TableTotal
+--------------+----------+----------+----------+
| email | Column1 | Column2 | Column3 |
+--------------+----------+----------+----------+
| 1 | x | x | x |
+--------------+----------+----------+----------+
| 2 | x | x | x |
+--------------+----------+----------+----------+
| 1 | x | x | x |
+--------------+----------+----------+----------+
| 2 | x | x | x |
+--------------+----------+----------+----------+
But I don't know how to use union when I'm also grouping the selections. When I try to do it, I get the error "column1 is ambiguous".
You need UNION ALL or UNION (removes duplicates).
SELECT ... GROUP BY ...
UNION ALL
SELECT ... GROUP BY ...;

Join tables with values depending on column value

i have 1 long table and 1 short table:
the long table looks like this:
LongTable:
+--------------+----------+----------+----------+
| Kabelnummer | GL |more data |even more |
+--------------+----------+----------+----------+
| 1 | 850 | x | x |
+--------------+----------+----------+----------+
| 2 | 850 | x | x |
+--------------+----------+----------+----------+
| 3 | 1300 | x | x |
+--------------+----------+----------+----------+
| 4 | 1300 | x | x |
+--------------+----------+----------+----------+
and
ShortTable:
+--------------+----------+----------+----------+
| data | GL |more data |numericVal|
+--------------+----------+----------+----------+
| x | 850 | x | 0.2345 |
+--------------+----------+----------+----------+
| x | 1300 | x | 0.2849 |
+--------------+----------+----------+----------+
I would like a query that copies the column "numericVal" into the table "LongTable" where GL.Longtable is the same as GL.shorttable:
LongTable:
+--------------+----------+----------+----------+----------+
| Kabelnummer | GL |more data |even more |numericVal|
+--------------+----------+----------+----------+----------+
| 1 | 850 | x | x | 0.2345 |
+--------------+----------+----------+----------+----------+
| 2 | 850 | x | x | 0.2345 |
+--------------+----------+----------+----------+----------+
| 3 | 1300 | x | x | 0.2849 |
+--------------+----------+----------+----------+----------+
| 4 | 1300 | x | x | 0.2849 |
+--------------+----------+----------+----------+----------+
How do I do this?
Try to join both tables with INNER JOIN:
SELECT L.*, s.numericVal
FROM LongTable l
JOIN sortTable s
ON l.GL = s.GL
SELECT lt.Kabelnummer, lt.GL, lt.X, lt.Y, st.numericVal
FROM LongTable lt
INNER JOIN ShortTable st ON lt.GL = st.GL
You use a JOIN to accomplish this.
You can read about joins here:
Join Fundamentals