PowerPivot SQL to Access databases - sql

I need to union tables into PowerPivot from 6 Access Databases that are currently on a network drive.
PowerPivot gives me the option to pull the data using SQL code but I have not found any examples online as to the syntax.
Basically wanting to do
SELECT * FROM path [DB01].[TBL01]
UNION
SELECT * FROM path [DB02].[TBL01]
UNION
SELECT * FROM path [DB03].[TBL01]
UNION
SELECT * FROM path [DB04].[TBL01]
UNION
SELECT * FROM path [DB05].[TBL01]
UNION
SELECT * FROM path [DB06].[TBL01]

Simply add paths in brackets:
SELECT * FROM [C:\Path\To\DB01.accdb].[TBL01]
UNION
SELECT * FROM [C:\Path\To\DB02.accdb].[TBL01]
UNION
SELECT * FROM [C:\Path\To\DB03.accdb].[TBL01]
UNION
SELECT * FROM [C:\Path\To\DB04.accdb].[TBL01]
UNION
SELECT * FROM [C:\Path\To\DB05.accdb].[TBL01]
UNION
SELECT * FROM [C:\Path\To\DB06.accdb].[TBL01]

Related

Incorrect syntax near ')' in Microsoft SQL when I'm trying to create a temporary table using WITH clause [duplicate]

This question already has an answer here:
TSQL CTE Error: Incorrect syntax near ')'
(1 answer)
Closed 4 months ago.
I want to combine 12 CSVs using UNION ALL into a new table. For this I used the WITH clause but it's not running because of Incorrect Syntax near End of File. This is the code I ran. I initially wanted to use CREATE TABLE but it doesn't seem to work with Microsoft SQL. Could someone let me know where I'm going wrong? Or if you know a better approach I'd love to apply that as well.
WITH YearlyData AS
( SELECT * FROM ['202109-divvy-tripdata-Sept2021$']
UNION ALL
SELECT * FROM ['202110-divvy-tripdata-Oct2021$']
UNION ALL
SELECT * FROM ['202111-divvy-tripdata-Nov2021$']
UNION ALL
SELECT * FROM ['202112-divvy-tripdata-Dec2021$']
UNION ALL
SELECT * FROM ['202201-divvy-tripdata-Jan2022$']
UNION ALL
SELECT * FROM ['202202-divvy-tripdata-Feb2022$']
UNION ALL
SELECT * FROM ['202203-divvy-tripdata-Mar2022$']
UNION ALL
SELECT * FROM ['202204-divvy-tripdata-Apr2022$']
UNION ALL
SELECT * FROM ['202205-divvy-tripdata-May2022$']
UNION ALL
SELECT * FROM ['202206-divvy-tripdata-June2022$']
UNION ALL
SELECT * FROM ['202207-divvy-tripdata-July2022$']
UNION ALL
SELECT * FROM ['202208-divvy-tripdata-Aug2022$']
)
after your query you have to select YearlyData :
WITH YearlyData AS
( SELECT * FROM ['202109-divvy-tripdata-Sept2021$']
UNION ALL
SELECT * FROM ['202110-divvy-tripdata-Oct2021$']
UNION ALL
SELECT * FROM ['202111-divvy-tripdata-Nov2021$']
UNION ALL
SELECT * FROM ['202112-divvy-tripdata-Dec2021$']
UNION ALL
SELECT * FROM ['202201-divvy-tripdata-Jan2022$']
UNION ALL
SELECT * FROM ['202202-divvy-tripdata-Feb2022$']
UNION ALL
SELECT * FROM ['202203-divvy-tripdata-Mar2022$']
UNION ALL
SELECT * FROM ['202204-divvy-tripdata-Apr2022$']
UNION ALL
SELECT * FROM ['202205-divvy-tripdata-May2022$']
UNION ALL
SELECT * FROM ['202206-divvy-tripdata-June2022$']
UNION ALL
SELECT * FROM ['202207-divvy-tripdata-July2022$']
UNION ALL
SELECT * FROM ['202208-divvy-tripdata-Aug2022$']
)
select * from YearlyData

How do I create a new table in SQL with using the UNION ALL function?

I am working on my google analytics course case study and I am trying to create a new table on Big Query- SQL by combining 12 datasets. I keep getting this error:
Syntax error: Unexpected string literal "bikeshare_total" at [1:14]
Please see the below query. I tried different variations but I'm getting the same error.
CREATE TABLE "bikeshare_total" AS (
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.Jan2021`
UNION ALL
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.Feb2021`
UNION ALL
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.March2021`
UNION ALL
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.April2021`
UNION ALL
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.May2021`
UNION ALL
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.June2021`
UNION ALL
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.July2021`
UNION ALL
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.August2021`
UNION ALL
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.Sep2021`
UNION ALL
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.Oct2021`
UNION ALL
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.Nov2021`
UNION ALL
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.Dec2021`)
You miss the project name?
Or try to remove the quotes.
Try using backticks for your table name and remove the parenthesis:
CREATE TABLE `bikeshare_total` AS
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.Jan2021`
UNION ALL
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.Feb2021`
UNION ALL
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.March2021`
UNION ALL
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.April2021`
UNION ALL
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.May2021`
UNION ALL
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.June2021`
UNION ALL
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.July2021`
UNION ALL
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.August2021`
UNION ALL
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.Sep2021`
UNION ALL
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.Oct2021`
UNION ALL
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.Nov2021`
UNION ALL
SELECT *
FROM `cyclisticjan21-dec21.Bike_share.Dec2021`
;

Combining multiple Unions Queries - is this the best way?

I've got some SQL code working at the moment but I need to do another union. I want to ensure what I do is correct. Currently, this is what I have:
select * from (select * from stametadata
union select * from stajjmetadata)
I want to union on another table. Would this be correct?
select * from (select * from stametadata
union select * from stajjmetadata
union select * from staggmetadata)
Is this the neatest way to do it?
You can chain unions. To be honest, you don't even need the outer query:
SELECT * FROM stametadata
UNION
SELECT * FROM stajjmetadata
UNION
SELECT * FROM staggmetadata

How to union many tables with same name but different schema without dynamic sql?

Microsoft SQL Server
[dbo].[MyTable]
[schema1].[MyTable]
[schema2].[MyTable]
[schema3].[MyTable]
I want to union them. For example:
select * from [*].[MyTable]
Can I do that?
Something like this
select * from [dbo].[MyTable]
UNION
select * from [schema1].[MyTable]
UNION
select * from [schema2].[MyTable]
UNION
select * from [schema3].[MyTable]

Access append query inconsistent

I have a table creation query (combine2) which takes a query (combine) and makes it a table (linegraph). This table must reflect changes in the associated query. Currently I have assigned a macro to run the append query to reflect changes.
The problem is that no matter how the append query is called it sometimes but not always copies all the data. Sometimes it wont import the first 100 or so rows. How can I get an updating table that always matches my query?
Append Query: Combine2
INSERT INTO linegraph SELECT * FROM Combine;
Query to turn into table: Combine
SELECT * FROM Month1calc UNION
SELECT * FROM Month2calc UNION
SELECT * FROM month3calc UNION
SELECT * FROM Month4calc UNION
SELECT * FROM Month5calc UNION
SELECT * FROM Month6calc UNION
SELECT * FROM Month7calc UNION
SELECT * FROM Month8calc UNION
SELECT * FROM Month9calc UNION
SELECT * FROM Month10calc UNION
SELECT * FROM Month11calc UNION
SELECT * FROM Month12calc UNION
SELECT * FROM Month13calc UNION
SELECT * FROM Month14calc UNION
SELECT * FROM Month15calc UNION
SELECT * FROM Month16calc UNION
SELECT * FROM Month17calc UNION
SELECT * FROM Month18calc;
`
Try changing UNION to UNION ALL.
When you use UNION, the query only returns unique rows (i.e.: duplicates are removed). When you use UNION ALL, duplicates are not removed.