This is a follow-up question to this one: Planning a database describing a multi-versioned product (no need to read it to answer this one).
To recap, I need to define multiple tables describing multiple versions of the same entity, namely print job log entry. These versions stem from different printer models that produce them. The thing is, all these different versions will always contain some number of common fields, e.g. [Copies Printed], regardless of the printer model, and others that are unique to a certain model (and thus, table version).
I'm working in MS Access.
I'm currently leaning towards the solution in which there is a table for each version. Let's say for simplicity's sake that there are only two printer models, and thus two tables, each one containing log entries produced by that model. In both of them there are some identical fields (same name, same type, same meaning). One of the queries that I will have to run will be to select rows from BOTH tables (e.g. select all entries with more than 10 copies in June 2014 run on any printer).
My question: how to write such a query, and how to define these tables, so that the DB knows that we're talking about the same [CopiesPrinted] field? A regular SELECT on such two tables (let's call them PrinterLog1 and PrinterLog2) will return another table, in which there will be two distinct columns, [PrinterLog1.CopiesPrinted] and [PrinterLog2.CopiesPrinted]. What needs to be done so that there is only one column [CopiesPrinted] in the resulting table, that will contain the data from the corresponding printer log table? In other words, how do I merge these two tables into one in a smart way? "JOIN ... ON PL1.CP=PL2.CP" queries won't help either, because the contents of [PrinterLog1.CopiesPrinted] and [PrinterLog2.CopiesPrinted] are supposed to be different.
Thanks!
Edit 1:
Thanks everyone, UNION seems to do the job. However, it seems to change column type for some fields. For instance it changed Yes/No (in the source table) to Number (in the result table) and put -1 and 0 for true and false respectively. Is there a way to remedy this?
Also, I can't seem to find anywhere on the web the list of SQL data types for Access. All I could find was the ones meant for Access users (Yes/No, Currency, etc.), whereas what I need is SQL-style types (VARCHAR, DECIMAL, etc.). Is there a list of SQL-style types for MS Access?
You would need to use the UNION statement.
For instance:
SELECT CopiesPrinted, Blahblah FROM PrinterLog1
UNION ALL
SELECT CopiesPrinted, Blahblah FROM PrinterLog2
More reading:
Combine the results of several select queries by using a union query
Union Query Overview - UNION vs UNION ALL
Related
I think I got a knot in my line of thought, surely you can untie it.
Basically I have two working queries which are based on the same table and result in an identical structure (same as source table). They are simply two different kinds of row filters. Now I would like to "stack" these filters, meaning that I want to retract all the entries which are in query a and query b.
Why do I want that?
Our club is structured in several local groups and I need to hand different kinds of lists (e.g. members with email-entry) to these groups. In this example I would have a query "groupA" and a query "newsletter". Another could be "groupB" and "activemember", but also "groupB" and "newsletter". Unfortunately each query is based on a set of conditions, which imho would be stored best in a single query instead of copying the conditions several times to different queries (in case something changes).
Judging from the Venn diagrams 1, I suppose I need to use INNER JOIN but could not get it to work. Neither with the LibreOffice Base query assistant nor an SQL-Code. I tried this:
SELECT groupA.*
FROM groupA
INNER JOIN newsletter
ON groupA.memberID = newsletter.memberID
The error code says: Cannot be in ORDER BY clause in statement
I suppose that the problem comes from the fact, that both queries are based on the same table.
May be there is an even easier way of nesting queries?
I am hoping for something like
SELECT * FROM groupA
WHERE groupA.memberID = newsletter.memberID
Thank you and sorry if this already has a duplicate, I just could not find the right search terms.
I have two pass through queries coming from two different databases. The data structure of the databases are identical and the layout of both queries are similar. How do I combine the results of the two queries into one table?
I do understand that this should be some form of a UNION. However, in MS Access I only know how to union two local tables. So a potential solution would be to first convert the result of the respective pass through queries as local tables using a macro and then doing an union from there. However, this being my first time working with pass through queries, I am not even sure how to convert the result of a pass through query into a local table. I am more used to working with standard linked tables. I am also not sure if this solution will be the most elegant.
Any assistance will be greatly appreciated.
AFAIK, once you saved your 2 PTQ, you can write a union just like if they were local tables. However the performance will probably be terrible, just like with any heterogeneous data sources.
Depending on the use case (specially if need to read that union many times), you might rather:
1. build (or empty) a local table, or create it using a 'make table query'
2. append the data from your first PTQ into the local table
3. append the data from second first PTQ into the local table
We are a gaming company that stores events (Up to 1 giga events per day) to bigquery. Events are sharded over month and application in order to lower query costs.
Now to our problem.
Our current solution supports adding new type of events which leads to new versions of the table schema. This versions has also been added to the tables.
I.e. events_app1_v2_201308 and events_app1_v2_201308
If we add events with new column types in september we will also get events_app1_v3_201309
We have written code that finds out involved tables (for a date range) and makes a union of them a'la bigquery's comma separeted FROM clause.
But I just realised that this will NOT work when we make unions over different versions of the event tables.
Anyone that has a smart solution of how to deal with this!?
Right now we are investigating if JSON structures could help us. The current solution is just flat columns. [timestamp, eventId, value, value, value, ...]
From https://developers.google.com/bigquery/query-reference#from
Note: Unlike many other SQL-based systems, BigQuery uses the comma syntax to indicate table unions, not joins. This means you can run a query over several tables with compatible !? schemas as follows:
You should be able to modify the table schema of the old tables to add columns, then the union should match. Note that you can only add columns, not remove them. You can use the tables.patch() method to do this, or bq update --schema
Moreover, as long as the new fields aren't marked REQUIRED, they should be considered compatible. If this is not the case, however, it would be a bug -- let us know if that is what you're experiencing.
I'm building a report in SSRS which takes data from several tables with similar names. There are three different 'sets' of tables - i.e., 123xxx, 456xxx, and 789xxx. Within these groups, the only difference in the table names is a three-digit code for a worksite, so, for example, we might have a table called 123001, 123010, and 123011. Within each set of tables, the columns have the same names.
The problem is that there are about 15 different sites, and I'm taking several columns from each site and each group of tables. Is there a more efficient way to write that query than to write out the name of every single column?
I don't believe there is but I feel like the use of aliases on your tables would make it much easier to undestand/follow your query building.
Also, if you aren't comparing values on the tables at all, then maybe a union between each table select would help make sense too.
I would give each table an alias.
SELECT s1t1.name
FROM Site1Table1 as s1t1;
I've a database which contains several tables for various tables of different products. These products have unique part numbers across all tables.
To search across all tables, I've created a view which uses UNION ALL across all common fields in the tables.
Once a part has been identified, I need to select all the columns depending on the table the data resides in. The view includes a field that specifies the table the data was found in.
I'm not sure of the way to accomplish the last part:
CASE statement (I'm leaning towards this one at the moment)
Dynamic SQL (prefer not to use this, would involve SELECT * and other nasties)
SELECT in client side (client needs to select from arbitrary tables, require additional privileges, bad design?)
Alternative solution?
EDIT: Actually, IF statement is the only one that makes sense. Client shouldn't need access to the tables directly. Since the columns are different in each table anyway, might as well have a seperate statement for each table.
(I'd mark the question as answered, but I don't have enough reputation for that)
I am not sure whether i understood your question correctly.. my understanding is you have views which is selecting data from diffrent tables using union all.. you can give table name while creating view only
select "table1",table1.a,table1.b.. from table1
union all
select "table2", table2.a,table2.b ..... from table2
Actually, IF statement is the only one that makes sense. Client shouldn't need access to the tables directly. Since the columns are different in each table anyway, might as well have a seperate statement for each table.