473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

noob needs help with join/union/sub-select

OK, out of my element here once again...

I want a query (or something I can use as a record source for a report)
to do the following:

* look in 2 tables (one field each) to find all entered values (without
duplicates)

* count the number of records in both tables that has that value for the
field.

Sounds simple, but I just can't quite get my head around that. I know
how I could do it using PHP and looping through result sets, but I need
this in an Access report. I assume that there is a way to do this
through the use of joins, sub-selects and unions.

Any help is greatly appreciated!

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.
Nov 13 '05 #1
4 1708
Justin Koivisto <sp**@koivi.com> wrote in message news:<DE*****************@news7.onvoy.net>...
OK, out of my element here once again...

I want a query (or something I can use as a record source for a report)
to do the following:

* look in 2 tables (one field each) to find all entered values (without
duplicates)

* count the number of records in both tables that has that value for the
field.

Sounds simple, but I just can't quite get my head around that. I know
how I could do it using PHP and looping through result sets, but I need
this in an Access report. I assume that there is a way to do this
through the use of joins, sub-selects and unions.

Any help is greatly appreciated!


Access doesn't like Union Queries as subqueries so you need to get
'all entered values sans duplicates' separately:

qry1:
SELECT tbl1.theField FROM tbl1 UNION SELECT tbl2.theField FROM tbl2;

Then:
SELECT qry1.theField, (SELECT Count(tbl1.TheField) FROM tbl1 WHERE
tbl1.theField = qry1.theField) AS Table1Count, (SELECT
Count(tbl2.theField) FROM tbl2 WHERE tbl2.theField = qry1.theField) AS
Table2Count
FROM qry1;

James A. Fortune
Nov 13 '05 #2
James Fortune wrote:
Justin Koivisto <sp**@koivi.com> wrote in message news:<DE*****************@news7.onvoy.net>...
OK, out of my element here once again...

I want a query (or something I can use as a record source for a report)
to do the following:

* look in 2 tables (one field each) to find all entered values (without
duplicates)

* count the number of records in both tables that has that value for the
field.

Sounds simple, but I just can't quite get my head around that. I know
how I could do it using PHP and looping through result sets, but I need
this in an Access report. I assume that there is a way to do this
through the use of joins, sub-selects and unions.

Any help is greatly appreciated!

Access doesn't like Union Queries as subqueries so you need to get
'all entered values sans duplicates' separately:

qry1:
SELECT tbl1.theField FROM tbl1 UNION SELECT tbl2.theField FROM tbl2;

Then:
SELECT qry1.theField, (SELECT Count(tbl1.TheField) FROM tbl1 WHERE
tbl1.theField = qry1.theField) AS Table1Count, (SELECT
Count(tbl2.theField) FROM tbl2 WHERE tbl2.theField = qry1.theField) AS
Table2Count
FROM qry1;


Thanks, I'll try it out and see what comes of it.

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.
Nov 13 '05 #3
James Fortune wrote:
Justin Koivisto <sp**@koivi.com> wrote in message news:<DE*****************@news7.onvoy.net>...
OK, out of my element here once again...

I want a query (or something I can use as a record source for a report)
to do the following:

* look in 2 tables (one field each) to find all entered values (without
duplicates)

* count the number of records in both tables that has that value for the
field.

Sounds simple, but I just can't quite get my head around that. I know
how I could do it using PHP and looping through result sets, but I need
this in an Access report. I assume that there is a way to do this
through the use of joins, sub-selects and unions.

Any help is greatly appreciated!

Access doesn't like Union Queries as subqueries so you need to get
'all entered values sans duplicates' separately:

qry1:
SELECT tbl1.theField FROM tbl1 UNION SELECT tbl2.theField FROM tbl2;

Then:
SELECT qry1.theField, (SELECT Count(tbl1.TheField) FROM tbl1 WHERE
tbl1.theField = qry1.theField) AS Table1Count, (SELECT
Count(tbl2.theField) FROM tbl2 WHERE tbl2.theField = qry1.theField) AS
Table2Count
FROM qry1;


That worked great with a slight modification:

SELECT qry1.theField, (SELECT Count(tbl1.TheField) FROM tbl1 WHERE
tbl1.theField = qry1.theField) + (SELECT Count(tbl2.theField) FROM tbl2
WHERE tbl2.theField = qry1.theField) AS Table2Count
FROM qry1;

One more question: Is there a way to "ORDER BY Table2Count DESC LIMIT 30" ?

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.
Nov 13 '05 #4
Justin Koivisto <sp**@koivi.com> wrote in message news:<kW*****************@news7.onvoy.net>...
James Fortune wrote:
Justin Koivisto <sp**@koivi.com> wrote in message news:<DE*****************@news7.onvoy.net>...
OK, out of my element here once again...

I want a query (or something I can use as a record source for a report)
to do the following:

* look in 2 tables (one field each) to find all entered values (without
duplicates)

* count the number of records in both tables that has that value for the
field.

Sounds simple, but I just can't quite get my head around that. I know
how I could do it using PHP and looping through result sets, but I need
this in an Access report. I assume that there is a way to do this
through the use of joins, sub-selects and unions.

Any help is greatly appreciated!

Access doesn't like Union Queries as subqueries so you need to get
'all entered values sans duplicates' separately:

qry1:
SELECT tbl1.theField FROM tbl1 UNION SELECT tbl2.theField FROM tbl2;

Then:
SELECT qry1.theField, (SELECT Count(tbl1.TheField) FROM tbl1 WHERE
tbl1.theField = qry1.theField) AS Table1Count, (SELECT
Count(tbl2.theField) FROM tbl2 WHERE tbl2.theField = qry1.theField) AS
Table2Count
FROM qry1;


That worked great with a slight modification:

SELECT qry1.theField, (SELECT Count(tbl1.TheField) FROM tbl1 WHERE
tbl1.theField = qry1.theField) + (SELECT Count(tbl2.theField) FROM tbl2
WHERE tbl2.theField = qry1.theField) AS Table2Count
FROM qry1;

One more question: Is there a way to "ORDER BY Table2Count DESC LIMIT 30" ?


The non-elegant answer is to change qry2 into a Make Table query then
create qry3:

SELECT TOP 30 tblMade.theField, tblMade.Table2Count FROM tblMade ORDER
BY tblMade.Table2Count DESC;

I don't know if it's possible to adapt qry2 to do something like this.
I haven't had to use TOP much. I might try it again later.

James A. Fortune
Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: administrata | last post by:
Hello! :) I've reading 'Python Programmin for the Absolute beginner'. I got questions which is... print "\t - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " rock = """ Igneous ...
6
by: Igorati | last post by:
Ok, this is what I have so far: #This program will ask for a user to imput numbers. The numbers will then be calculated #to find the statistical mean, mode, and median. Finallly the user will be...
0
by: Len Coleson | last post by:
I am at wits end with this. I have a transaction report that has a union query as a record source that asks date parameters from an input form. The sub report is based on a variation of the same...
73
by: Sean Dolan | last post by:
typedef struct ntt { int type; union { int i; char* s; }; }nt; nt n; n.i = 0;
2
by: Holger | last post by:
I needed a tool for extracting patches from CVS based on the log messages. I.e. we mark our fixes and features with a "Bugdb XYZ" And sometimes you need to move a fix/feature to another branch or...
7
by: jason.langdale | last post by:
I have 3 tables I want to use in a view. Table A has field 1,2,3,4,5 and table B has field 1,2,3,4,5. I want to do a union on these. (I have done so successfully if I stop here) I also want to join...
4
by: jobs | last post by:
Three noob questions. Please help. 1. Is there a way to have arguments on subroutine that are optional? 2. Say I need to convert datetime to string..in this format : 2006-09-07...
2
by: athos | last post by:
OS: Windows 2000/XP (needs to run on 2 different machines) Language: Python 2.5 Programmer Level: Pathetically new to Python Goal: Using code I've altered for my needs, I'm attempting to create...
3
by: GKRAFT | last post by:
I'm trying to create a function which would look like this when running: >> union(,) >> union(,) I have written the following code: def union(l1, l2): finalList = i = 0
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.