473,786 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting 2 counts() of 2 foreign keys

Wondering if someone here could help me out, I've tried a number of
ways to do this, and while I have managed to get it to work, Im SURE
im not doing the optimal way, and it seems to be causing problems with
my form which is throwing up '#Name? ' in some fields(at seemingly
random times...)

Basicly I have 3 tables, one being the 'top' table, with only 2
fields, a primary key (ID) and a second field Name, this table is
called 'tblDests'

The two other tables are called tblAreas and tblRates, and both have a
field called DestID which is defined as a foreign key to the tblDests
ID field. Both tables have a many to one relationship with the
tblDests table (ie multiple entrys in the two 'child' tables)

What I want to do is get a result set with the total number of records
in each child tables grouped by Name from the tblDests table, so my
result set would look like:

tblDests.Name | CountOfAreas | CountOfRates
--------------------------------------------
India | 20 | 13
England | 12 | 13

The way I'm doing it now involes first a Union query like
SELECT Name,Area, <fake rates> .... UNION SELECT Name ,<fake area>,
Rates
Then i do a totals query on top of that and get the First() from Area
and Last() from Rates. It works how i want, but like I said, there
surely must be a better way to do this??

Its worth noting that both child tables have no 'ID' field, their
primary key is a multiple field index so I use 'destid' in both cases
(the foreign key)

Thanks for any help
Jim
Nov 13 '05 #1
2 1789
Hi Jim

Create 3 queries, the first 2 create the count for the tblAreas and the
tblRates and the third query joins the first 2.

qryAreaCount
SELECT tblDest.DestID, tblDest.Destina tion, Count(tblArea.A rea) AS AreaCount
FROM tblDest LEFT JOIN tblArea ON tblDest.DestID = tblArea.DestID
GROUP BY tblDest.DestID, tblDest.Destina tion;

qryRateCount
SELECT tblDest.DestID, tblDest.Destina tion, Count(tblRate.R ate) AS RateCount
FROM tblDest LEFT JOIN tblRate ON tblDest.DestID = tblRate.DestID
GROUP BY tblDest.DestID, tblDest.Destina tion;

Join the first 2 queries to the tblDest to create the third query
qryAreaAndRateC ount
SELECT tblDest.Destina tion, qryAreaCount.Ar eaCount, qryRateCount.Ra teCount
FROM (tblDest INNER JOIN qryAreaCount ON tblDest.DestID =
qryAreaCount.De stID) INNER JOIN qryRateCount ON tblDest.DestID =
qryRateCount.De stID;

Stewart
"JimJim" <om*@wired.ie > wrote in message
news:da******** *************** ***@posting.goo gle.com...
Wondering if someone here could help me out, I've tried a number of
ways to do this, and while I have managed to get it to work, Im SURE
im not doing the optimal way, and it seems to be causing problems with
my form which is throwing up '#Name? ' in some fields(at seemingly
random times...)

Basicly I have 3 tables, one being the 'top' table, with only 2
fields, a primary key (ID) and a second field Name, this table is
called 'tblDests'

The two other tables are called tblAreas and tblRates, and both have a
field called DestID which is defined as a foreign key to the tblDests
ID field. Both tables have a many to one relationship with the
tblDests table (ie multiple entrys in the two 'child' tables)

What I want to do is get a result set with the total number of records
in each child tables grouped by Name from the tblDests table, so my
result set would look like:

tblDests.Name | CountOfAreas | CountOfRates
--------------------------------------------
India | 20 | 13
England | 12 | 13

The way I'm doing it now involes first a Union query like
SELECT Name,Area, <fake rates> .... UNION SELECT Name ,<fake area>,
Rates
Then i do a totals query on top of that and get the First() from Area
and Last() from Rates. It works how i want, but like I said, there
surely must be a better way to do this??

Its worth noting that both child tables have no 'ID' field, their
primary key is a multiple field index so I use 'destid' in both cases
(the foreign key)

Thanks for any help
Jim

Nov 13 '05 #2
Thanks very much that works perfectly

"Stewart Allen" <sa****@NOT.wav e.THIS.co.nz> wrote in message news:<cb******* ***@news.wave.c o.nz>...
Hi Jim

Create 3 queries, the first 2 create the count for the tblAreas and the
tblRates and the third query joins the first 2.

Nov 13 '05 #3

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

Similar topics

5
49735
by: Olivier Crèvecoeur | last post by:
Hello, Excuse me for my poor english. I would kike know if create index on the foreign key it's necessary or if Oracle, are optimized for using foreign key whithout index. Best regards Olivier
10
42422
by: Bodza Bodza | last post by:
I'm having an argument with an incumbent self-taught programmer that it is OK to use null foreign keys in database design. My take is the whole point of a foreign key is that it's not supposed to be optional, it's very definition is it's a necessary link to the parent table and part of the definition. If it's optional it shouldn't be part of the definition of a table and should be in a linking table instead. Comments?
1
3008
by: Vinodh Kumar P | last post by:
I understand the number of foreign keys allowed is restricted by the DBMS I use. In a general relational schema design perspective how many foreign keys a table shall have? If I have large number of foreign keys what anamolies it will lead to? Is this crucial to identify all the foriegn key relationships for a table? Vinodh
0
1418
by: Scott Ribe | last post by:
I've got a problem which I think may be a bug in Postgres, but I wonder if I'm missing something. Two tables, A & B have foreign key relations to each other. A 3rd table C, inherits from A. A stored procedure updates a row in C, adds a row each in B & C. I get an integrity violation. All the foreign keys are deferrable, and the stored procedure is called from within a transaction with constraints deferred. (And the foreign keys do refer to...
1
1299
by: tmp | last post by:
I have the following problem: 1) I have one master table with a primary key. 2) In addition I have *several* slave tables, all refering to a primary key in the master table (no two slave tables refer to the same master key) I wan't to make sure that no keys in the master table are unreferred, that is:
9
3911
by: sonal | last post by:
Hi all, I hv started with python just recently... and have been assigned to make an utility which would be used for data validations... In short we take up various comma separated data files for eg: area.txt, school.txt, students.txt.... and so on (ok?!?) now, 1. area code used in the school.txt must be defined in the area.txt (Primary key in area => area_code defined in area.txt & Foreign key on school => area_code defined in...
1
3260
by: rbarber | last post by:
I have to synchronize 2 databases hourly but am having difficulty maintaining foreign key relations. These tables use auto-increment columns as primary keys, with child records in other tables related with foreign keys. I can't change the way the local software uses primary or foreign keys as it is hardcoded in the local app. (microsoft retail management system)..(however the web-remote app is easily customized). I am using CDB synchronizer to...
1
11011
by: apax999 | last post by:
Kinda new to SQL, using SQL Server 2005. I have some foreign keys in a couple of tables. I need to drop these tables, but can't since I'll get the error: Msg 3726, Level 16, State 1, Line 1 Could not drop object 'Client' because it is referenced by a FOREIGN KEY constraint.
2
1423
by: kriz4321 | last post by:
Hi I have a array in which I need to count the number of ocurrence of a particular word for eg I need to count no of times a word "test" , "test2" occurs in a array @list. (The contents of the array is around 100 lines) Code: open(FH3, "sample.txt"); while(<FH3>)
0
9655
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10169
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.