Merge two tables while doing operations - sql

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 ...;

Related

How to select distinct records based on a given condition?

I have the following table in the MySQL database:
| id | col | val |
| -- | --- | --- |
| 1 | 1 | y |
| 2 | 1 | y |
| 3 | 1 | y |
| 4 | 1 | n |
| 5 | 2 | n |
| 6 | 3 | n |
| 7 | 3 | n |
| 8 | 4 | y |
| 9 | 5 | y |
| 10 | 5 | y |
Now I want to distinctly select the records where all the values of similar col are equal to y. I tried both the following queries:
SELECT DISTINCT `col` FROM `tbl` WHERE `val` = 'y'
SELECT `col` FROM `tbl` GROUP BY `col` HAVING (`val` = 'y')
But it's not working out as per my expectation. I want the result to look like this:
| col |
| --- |
| 4 |
| 5 |
But 1 is also being included in the results with my queries. Can anybody help me building the correct query? As far as I understand, I may need to create a derived table, but can't quite figure out the right path.
You are close, with the second query. Instead, compare the min and max values:
SELECT `col`
FROM `tbl`
GROUP BY `col`
HAVING MIN(val) = MAX(val) AND MIN(`val`) = 'y';
Check that 'y' is the minimum value:
HAVING MIN(val) = 'y'

SQL Count number of column based on another column

Simple question here but I don't have any data to test with so I'm not sure if it is right.
Let's say I have a table that looks something like this:
+---------+---------+
| ColumnA | ColumnB |
+---------+---------+
| 1 | a |
| 2 | a |
| 3 | a |
| 3 | b |
| 4 | a |
| 5 | a |
| 6 | a |
| 6 | b |
| 6 | c |
| 7 | a |
+---------+---------+
I want to count the number of distinct items in column b for each column a.
So the result table would look like this:
+---------+---------+
| ColumnA | Count |
+---------+---------+
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 1 |
| 5 | 1 |
| 6 | 3 |
| 7 | 1 |
+---------+---------+
How would I do this?
I've been trying something like this:
dense_rank() over (partition by ColumnA order by ColumnB) count
You can use GROUP BY and COUNT with DISTINCT:
SELECT ColumnA, COUNT(DISTINCT ColumnB) AS [Count]
FROM MyTable
GROUP BY ColumnA
demo on dbfiddle.uk

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;

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

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.