Link to home
Start Free TrialLog in
Avatar of StampIT
StampITFlag for United States of America

asked on

Query with No join

I have a query that has as its data source 4 queries. Each of the 4 sub queries result in a single record with one field. I do not have a need to join them. I simply want to list the 4 fields together in one record. The SQL representation would be:
Select query1.field1, query2.field1,query3.field1,query4.field1 FROM query1, query2, query3,query4;
This appears to work.

My question is: This seems unconventional and am concerned that this may not always work. Should I add a field to each query to join on ? Is there another approach ? I have a similar application where I need to list up to 11 fields.

Thanks for the help
Avatar of Bill Prew
Bill Prew

It really depends on the tables, and the data, and the inter-relations among them.  I assume the queries you are using have WHERE clauses, and if so do they relate to any of the other tables, or no?  All these queries that return just a single field sounds a little different than normal, but it depends on what it is.  For example if you have a table for say fiscal years and one query returns just the current fiscal year, then that is logical, and might make sense not to have a JOIN etc.

Any additional info you can share about what these tables are and the single row / field would be helpful.  And what the additional 11 might be down the road.


»bp
Avatar of StampIT

ASKER

Yes the queries have Where clauses. The data source for the sub queries is a table of all our sales order due due dates by customer. Each customer belongs to one of three groups. query1 for example sums the total value for the period where the due date is between 11/1/2017 and 11/30/2017 and the customer group = Acme. query2 is for the same customer group and the next  period between 12/1/2017 and 12/31/2017, etc. I need date period totals for each customer group and one for all customers. The additional would be 11 date periods rather than 4.  Hope this helps clarify. Thanks.
CROSS JOIN
The SQL CROSS JOIN produces a result set which is the number of rows in the first table multiplied by the number of rows in the second table if no WHERE clause is used along with CROSS JOIN.This kind of result is called as Cartesian Product. If WHERE clause is used with CROSS JOIN, it functions like an INNER JOIN.

Your existing query IS using a join the known as "CROSS JOIN". Without changing the functionality of your query at all, but re-written using "ANSI syntax" your existing query is as the same as this:

Select query1.field1, query2.field1,query3.field1,query4.field1 
FROM (...) query1
CROSS JOIN (...) query2
CROSS JOIN (...) query3
CROSS JOIN (...) query4
;

Open in new window


If ANY of the 4 subqueries fails to return a row, then the whole query will return no row.
If any of those subqueries produces more than one row THEN you will get duplicated rows (because of the multiplier effects)
If more than one of those queries produces more than one row as a result then the final result will be a mess (because of the multiplier effects)

Without further knowledge of the 4 subqueries not much else can be said about them.

{+edit}
I'm not sure that Access uses the syntax shown above, but the effect is the same. if you do not explicitly specify the join then you are using a CROSS JOIN
ASKER CERTIFIED SOLUTION
Avatar of PatHartman
PatHartman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for confirming Pat, I was pretty sure  that was the case. Although the words "cross join" don't exist in the unique variant of SQL that Access does support, the standatd SQL terminology used to describe that join type is "cross join", and importantly, it definitely is a join.
Avatar of StampIT

ASKER

Thanks.