SQL Combine two tables in select statement - sql

I have a situation where I want to combine two tables for queries in a select statement, and I haven't found a working solution yet.
The Situation:
Table A
Table B
Both A and B have identical fields but distinct populations. I have other queries that are pulling from each table separately.
I want to build a query that pulls from them as if they were one table. There are no instances of records being in both tables.
My research so far led me to think the FULL OUTER JOIN was what I wanted, but I'm not entirely sure how to do that when I'm not really joining them on any field and it failed in my tests. So I searched for append options thinking that might more accurately represent what I'm trying to do and the INSERT INTO looked promising but less so for select statements. Is there a way to do this or do I need to create a third table that's the combination of the first two to query from?
.
This is being done as an Excel VBA query to Access via DAO. I'm building up SQL statements piece by piece in my VBA code based on user-selected options and then pulling the results into Excel for use. AS such my hope is to be able to only alter the FROM statement (since I'm building up the queries piecemeal) to effect this so that any other aspects of the select statement won't be impacted. Any suggestions or help will be much appreciated!

You can UNION the tables to do this:
SELECT StuffYouWant
FROM (SELECT *
FROM TableA
UNION ALL
SELECT *
FROM TableB) c

Something like this:
SELECT * FROM a
UNION
SELECT * FROM b
Make sure the a table and the b table have the same number of columns and the corresponding columns have the same data type

Related

sql - perform join excluding repeated fields

I have the following problem, I use sql server, I need to join two tables by a field, but when performing the join I am duplicating the key field, my query is as follows:
select A.*, B.*
from Database.dbo.Module1 A
LEFT JOIN RRHH.dbo.Module2 B on A.key1 = B.key1
is it possible to exclude from the select the key1 field from the module2 table?
In the tables, I have another few duplicate fields, I could write every field I need from the tables in the select, but , it would be easier to exclude the fields I don't need. Consider that each table has hundreds of fields that are needed.
It is impossible. Specify fields you need.
There is no "all columns except <these>" syntax in T-SQL, sorry.
Of course it's very easy to generate the list of columns from any table by simply dragging the Columns node onto a query window. This works in both SSMS and Azure Data Studio, as I describe in this Bad Habits post:
Then just prefix the ones you need, and delete the ones you don't.

Is there a way to query multiple tables at once based on common table name in Oracle through SQL or PL/SQL

I have a requirement to fetch data from all the tables based on common table name pattern every week.Basically my requirement is to merge all the data to form a single lookup table.
Example:
Table Names:
Department_20190101
Department_20190109
Department_20190122
Department_20190129
I have to fetch data from all the tables and have to create a single lookup table. Is there a simple way to do this other than by going through iteration in PL/SQL by getting the table names with the help of ALL_TABLES
Note:The Date part is not consistent.If i can achieve this requirement once then i can easily start inserting the data from the new table to the existing lookup(n+1)
Please share your suggestions.
Regards,
Scott
If you have a very long list of tables, or you requirement is to aggergate the results from all tables e.g. starting with Department_ followed by a certain date or range of dates, then you might need dynamic SQL for this. For the exact example you did show in your question, a CTE with a union query might work:
WITH cte AS (
SELECT * FROM Department_20190101 UNION ALL
SELECT * FROM Department_20190109 UNION ALL
SELECT * FROM Department_20190122 UNION ALL
SELECT * FROM Department_20190129
)
And then use the CTE as:
SELECT *
FROM cte;
This assumes that all tables have identical structures. Also, as a side note, if this be the case, you might want to consider just having a single table with a date column to differentiate between the otherwise common data.
Check here: Execute For Each Table in PLSQL there is nice example to resolve Your problem using pl/sql (pl/sql help You dinamicly grow up sql-query) .

Append Linked Tables Into One Table?

I have three linked tables in my MS Access Database, and I am trying to run just one query to append them all into a master table, rather than creating a separate query for each one.
Can someone give me an example code using generic table names to show how I would union or join these table with query code?
SELECT [TableA].*
FROM TableA
UNION
SELECT [TableB].*
FROM TableB;
etc.
You can do a SELECT * INTO NEWTABLE . . .
You can do all kinds of things. Google is your best friend.

SQL to identify duplicate columns from table having hundreds of column

I've 250+ columns in customer table. As per my process, there should be only one row per customer however I've found few customers who are having more than one entry in the table
After running distinct on entire table for that customer it still returns two rows for me. I suspect one of column may be suffixed with space / junk from source tables resulting two rows of same information.
select distinct * from ( select * from customer_table where custoemr = '123' ) a;
Above query returns two rows. If you see with naked eye to results there is not difference in any of column.
I can identify which column is causing duplicates if I run query every time for each column with distinct but thinking that would be very manual task for 250+ columns.
This sounds like very dumb question but kind of stuck here. Please suggest if you have any better way to identify this, thank you.
Solving this one-time issue with sql is too much effort. Simply copy-paste to excel, transpose data into columns and use some simple function like "if a==b then 1 else 0".

Compare two tables look for gaps, append them in new table

This is related to MS Access 2007. I'm in bit of a pickle and anyone who can solve this for me I will be greatly indebted to.
I have two tables: Actual and Schedule. My job is to compare these two tables, look for gaps, and fill them in a new table (append). Important point: tbnum corresponds to timeband. It's something I came up with to identify gaps easily. I have created these sample tables:
http://i39.tinypic.com/2mhesxs.jpg
There are 2 scenarios:
JFKATL: there is a match in Actual table, so bring the original record for JFKATL from Schedule as well as from Actual for the missing JFKATL record into NewTable
ORDSLC: there is no match in Actual table, but there is an apparent "gap" in timeband. So extend the timeband for the unmatched record and paste it in NewTable
Edit: Sorry, there was a small issue i noticed in the original image. Posted new one.
I may not understand your problem completely or correctly, but it appears that an UNION operator will work for you. UNION will combine the unique records between two queries and merge them into one recordset. UNION ALL will include duplicates.
SELECT c1, c2
FROM table1
UNION
SELECT c1, c2
FROM table2;