SQL select from several tables and output to several tables - sql

I have several tables of which most contain distinct ID columns and most have an additional column containing dates.
Now I need to retrieve all IDs with their corresponding dates from all tables.
Is there a way to output all IDs and dates from all tables at once and without creating duplicates ?

You can use UNION ALL to combine multiple queries:
SELECT 'table_a' source, table_a_id id, creation_date FROM table_a
UNION ALL
SELECT 'table_b' source, table_b_id id, creation_date FROM table_b
UNION ALL
SELECT 'table_c' source, table_c_id id, creation_date FROM table_c;
If you need to remove duplicate rows, you can use UNION instead of UNION ALL, at the cost of some performance.

Related

PowerBI Union Vs SQL

I have two table's Table A (ID,Name,Location) and Table B(ID , Name)
When we do Union in SQL, Instead of adding any blank location column in TableB, we can perform Union like this
Select ID, name, Location from TableA
Union
Select ID, name, '' as Location from TableB
Here I am dynamically adding column in select statement I am not adding blank column in table B itself.
Can we achieve the same in PowerBI?
You can Append the tables in Power Query. https://learn.microsoft.com/en-us/power-query/append-queries

PostgreSQL - UNION of tables based on number of columns

I'm working in PostgreSQL, and I want to UNION all tables with 11 columns, of a database called 'postgres'.
The list of the mentioned tables, I get it with the following query.
with
completo_tablas as (
select column_name, table_name from information_schema.columns
where table_schema='public'
)
--,tablas as (
select table_name--, count(column_name)
from completo_tablas
group by table_name
having count(column_name)=11
--)
Obteining, something like:
Table_name
--------------
table1
table2
.
.
.
The problem i cant solve, isn't getting the UNION of the tables with 11 columns for a specific day; I know i can do than copying the tables names like:
Select * from tabla1 UNION select * from tabla2 UNION Sel....
Given the nature of our business, the number of tables is going to increase over time.
What I want, cause of the nature of our business, is a "dynamic" query, where I don't have the names of the tables set by hand in the UNION query.
But something with which you can always get just that, the union of all tables with 11 columns
Thank you for your time.

How can I select the same number of records from two different tables in SQL Server

How can I select the same number of records from two different tables in SQL Server?
For example, I have a table A with 10 records and table B with 15 records; how can I select 10 records from A and 10 records from B ?
If both the tables have same number and type of columns. Then use union or union all
Select col1,col2,col3 from table1
union
Select col1,col2,col3 from table2
Something like that?
SELECT col1,col2,col3
FROM TABLE_A
UNION
SELECT TOP (SELECT COUNT(*) FROM TABLE_A) col1,col2,col3
FROM TABLE_B

Union of multiple queries using the count function

I'm working on learning more about how the UNION function works in SQL Server.
I've got a query that is directed at a single table:
SELECT Category, COUNT(*) AS Number
FROM Table1
GROUP BY Category;
This returns the number of entries for each distinct line in the Category column.
I have multiple tables that are organized by this Category column and I'd like to be able to have the results for every table returned by one query.
It seems like UNION will accomplish what I want it to do but the way I've tried implementing the query doesn't work with COUNT(*).
SELECT *
FROM (SELECT Table1.Category
Table1.COUNT(*) AS Number
FROM dbo.Table1
UNION
SELECT Table2.Category
Table2.COUNT(*) AS Number
FROM dbo.Table2) AS a
GROUP BY a.Category
I'm sure there's an obvious reason why this doesn't work but can anyone point out what that is and how I could accomplish what I'm trying to do?
You cannot write a common Group by clause for two different select's. You need to use Group by clause for each select
SELECT TABLE1.Category, --missing comma here
COUNT(*) as Number -- Remove TABLE1. alias name
FROM dbo.TABLE1
GROUP BY Category
UNION ALL --UNION
SELECT TABLE2.Category, --missing comma here
COUNT(*) as Number -- Remove TABLE1. alias name
FROM dbo.TABLE2
GROUP BY Category
If you really want to remove duplicates in result then change UNION ALL to UNION
COUNT as any associated aggregation function has to have GROUP BY specified. You have to use group by for each sub query separately:
SELECT * FROM (
SELECT TABLE1.Category,
COUNT(*) as Number
FROM dbo.TABLE1
GROUP BY TABLE1.Category
UNION ALL
SELECT TABLE2.Category,
COUNT(*) as Number
FROM dbo.TABLE2
GROUP BY TABLE2.Category
) as a
It is better to use UNION ALL vs UNION - UNION eliminates duplicates from result sets, since - let say - you want to merge both results as they are it is safer to use UNION ALL

SQL question, DISTINCT returned values in multiple UNION ALL'ed statements

I have a sql statement that is a union of several queries which all just return keys:
SELECT DISTINCT key as KEY FROM tablea WHERE XYZ
UNION ALL
SELECT DISTINCT id as KEY FROM tableb WHERE XYZ
UNION ALL
...
My question is that there are some queries that return keys that overlap, and I actually want the final KEY field returned values to be distinct values. Any ideas?
Use UNION instead of UNION ALL.