Access append query inconsistent - sql

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.

Related

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 Multiple Tables with same column names on Google Big Query?

I am trying to create a union with 3 tables that have the same column names. However, the queries tested seems not to be working.
The first query that I have used is the following:
Select * FROM table1
UNION ALL
SELECT * FROM table2
SELECT * FROM table3
UNION ALL
The second query used is the following:
SELECT *
FROM
(select * from table_1),
(select * from table_2),
(select * from table_3)
Both of them are not working for me. Please someone can help me with this?
This should work if columns are identical:
SELECT * FROM table1 UNION ALL
SELECT * FROM table2 UNION ALL
SELECT * FROM table3

PowerPivot SQL to Access databases

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]

UNION ALL query: "Too Many Fields Defined"

I'm trying to get a UNION of 3 tables, each of which have 97 fields. I've tried the following:
select * from table1
union all
select * from table2
union all
select * from table3
This gives me an error message:
Too many fields defined.
I also tried explicitly selecting all the field names from the first table (ellipses added for brevity):
select [field1],[field2]...[field97] from table1
union all
select * from table2
union all
select * from table3
It works fine when I only UNION two tables like this:
select * from table1
union all
select * from table2
I shouldn't end up with more than 97 fields as a result of this query; the two-table UNION only has 97. So why am I getting Too many fields with 3 tables?
EDIT: As RichardTheKiwi notes below, Access is summing up the field count of each SELECT query in the UNION chain, which means that my 3 tables exceed the 255 field maximum. So instead, I need to write the query like this:
select * from table1
union all
select * from
(select * from table2
union all
select * from table3)
which works fine.
It appears that the number of fields being tracked (limit 255) is counted against ALL parts of the UNION ALL. So 3 x 97 = 291, which is in excess. You could probably create a query as a UNION all of 2 parts, then another query with that and the 3rd part.
I had two tables with 173 fields each (2 x 173 > 255!). So I had to resort to splitting the tables in half (keeping the primary key in both), before using the UNION statement and reassembling the resulting output tables using a JOIN.
select u1.*, u2.*
from (
select [field1_PKID],[field2],...,[field110]
from table1
union all
select [field1_PKID],[field2],...,[field110]
from table2
) as u1
inner join (
select [field1_PKID],[field111],...,[field173]
from table1
union all
select [field1_PKID],[field111],...,[field173]
from table2
) as u2
on [u1].[field1_PKID] = [u2].[field2_PKID]
Perhaps if your 3 tables have duplicate records you can go with UNION instead of UNION ALL which may reduce the number of fields to be tracked. Because UNION will always serve the business purpose which removes duplicates. In that case your query will be like following,
select * from table1
union
select * from table2
union
select * from table3;