473,407 Members | 2,315 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,407 software developers and data experts.

Help with a QUERY

Im running an access database.
Which looks like this:



Im struggling to make a query to do exactly what i need.

Firstly i'll put it into context. Im allowing users to sign up to play in a sport, and i want to return a list of all the sports that are available to play in.
therefore...
I need to select all the sportName from TblSport.

However, i have a few other constraints i need to include:
A sport should only be selected if it has a league that has exactly ONE season stored in the TblLeagueSeason.

Also the size attribute in TblLeague specifies the maximum number of teams that can play in it.
A sport should only be Selected if the season has less than the max number of records (size) stored in TblLeagueSeasonTeam

So ive got as far as saying:

Select sportName FROM TblSport WHERE "number of seasons for a league in that sport = 1" AND "Number of teams for that season < size of its sport"

Any Help would be greatly appreiated!
Thanks! Ash
Mar 29 '08 #1
3 1070
Delerna
1,134 Expert 1GB
Try this one, I think it will do what you want.
Expand|Select|Wrap|Line Numbers
  1. SELECT tblSport.SportName, 
  2.       tblLeague.LeagueName, 
  3.       tblLeague.Size AS MaxTeams, 
  4.       z.NumTeams
  5. FROM 
  6. (    SELECT LeagueID,count(LeagueID) as NumSeasons 
  7.      FROM tblLeagueSeason 
  8.      GROUP BY LeagueID
  9. ) AS y 
  10.  
  11. INNER JOIN 
  12. (   SELECT  a.LeagueID,Count(b.TeamID) AS NumTeams
  13.     FROM tblLeagueSeason AS a 
  14.     LEFT JOIN tblLeagueSeasonTeam AS b 
  15.                ON a.LeagueSeasonID = b.LeagueSeasonID
  16.     GROUP BY a.LeagueID
  17. ) AS z ON y.LeagueID = z.LeagueID
  18.  
  19. INNER JOIN tblLeague ON y.LeagueID = tblLeague.LeagueID 
  20. INNER JOIN tblSport ON tblLeague.SportID = tblSport.SportID
  21. WHERE z.NumTeams<[Size] AND y.NumSeasons=1;
  22.  
  23.  
Mar 30 '08 #2
Hi thanks alot! just spent some time getting my headaround it, i think im getting there.

Ive tried using it, i made some changes just to some table names, so it fits my database just simple things such as tblSport to TblSport

Now i have this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT TblSport.sportName,
  3.       TblLeague.leagueName,
  4.       TblLeague.size AS MaxTeams,
  5.       z.NumTeams
  6. FROM
  7. (    SELECT leagueID,count(leagueID) AS NumSeasons
  8.       FROM TblLeagueSeason
  9.       GROUP BY leagueID
  10. )  AS y
  11.  
  12. INNER JOIN
  13. (    SELECT  a.leagueID,Count(b.teamID) AS NumTeams
  14.       FROM TblLeagueSeason AS a
  15.       LEFT JOIN TblLeagueSeasonTeam AS b
  16.                 ON a.leagueSeasonID = b.leagueSeasonID
  17.       GROUP BY a.leagueID
  18. ) AS z ON y.leagueID = z.leagueID
  19.  
  20. INNER JOIN TblLeague ON y.leagueID = TblLeague.leagueID
  21. INNER JOIN TblSport ON TblLeague.sportID = TblSport.sportID
  22.  
  23. WHERE z.NumTeams<[size] AND y.NumSeasons=1;
  24.  
however when I try saving it as a query in access i get this error



Any ideas?
Mar 30 '08 #3
Delerna
1,134 Expert 1GB
Can't see anything obvious and without being able to get my hands on it and try different things it can be difficult to discover these errors.
I wrote the entire thing as a single query and I wrote it on a mock up of your table schema so it works without error.
In reality there are 3 queries in my solution. 2 subqueries for the aggregate fields, and then joining them to the other two tables to get the result you needed.

So you could break the query up into 3 separate queries. I each for the 2 subqueries and the third one to join them all together. That way you should be able to build it up piece by piece.
So this would be 1 query, perhaps called qryCountSeasons

Expand|Select|Wrap|Line Numbers
  1. SELECT leagueID,count(leagueID) AS NumSeasons
  2. FROM TblLeagueSeason
  3. GROUP BY leagueID
  4.  
and this would be the second query, perhaps called qryCountTeams
Expand|Select|Wrap|Line Numbers
  1. SELECT  a.leagueID,Count(b.teamID) AS NumTeams
  2. FROM TblLeagueSeason AS a
  3. LEFT JOIN TblLeagueSeasonTeam AS b
  4.          ON a.leagueSeasonID = b.leagueSeasonID
  5. GROUP BY a.leagueID
  6.  
then all you need do is use query designer to connect the two tables and the two queries together for your final result.

There is nothing wrong with doing it like that. I don't like to because there are two extra queries in the query window that are not being used by anything other than a single query and they can be removed from the list by using subqueries in the final result query. Just makes it less confusing for me in 6 months time when i've forgotten what all 150 + queries in the display do
Mar 30 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
8
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
5
by: Steve Patrick | last post by:
Hi All You guys are my last hope, despite spending money on books and hours reading them I still can not achieve the results I need. I have designed a database in Access 2000 based on 1 table,...
4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
6
by: Takeadoe | last post by:
Dear NG, Can someone assist me with writing the little code that is needed to run an update table query each time the database is opened? From what I've been able to glean from this group, the...
3
by: mcmahonb | last post by:
Hey people... I've been searching this forum for a few hours and even though this topic has been went over from many different angles; I cannot seem to figure out how to make things work on my...
4
by: n | last post by:
Hello! Here is a problem I hope you can point me to a solution. It Problem: A teacher needs to know which lesson to teach. A school has a curriculum with 26 lessons, A-Z. For a given class,...
47
by: Jo | last post by:
Hi there, I'm Jo and it's the first time I've posted here. I'm in process of creating a database at work and have come a little unstuck.....I'm a bit of a novice and wondered if anyone could...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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.