473,378 Members | 1,405 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,378 software developers and data experts.

How to use a group by in a subquery

I am trying to create a query that will return the athlete's id, their first and last name in one column, the number of games they have participated in, and the number of medals they have won.

So far I have this code:
Expand|Select|Wrap|Line Numbers
  1. SELECT ATHLETE.Athlete_id AS ID, (ATHLETE.Athlete_lname+", "+ATHLETE.Athlete_fname) AS Name, COUNT(REPRESENTATIVE.Athlete_id) AS [# of Game], 
  2. (SELECT COUNT(MEDAL.Cont_id)
  3. FROM MEDAL, ATHLETE, CONTESTANT, REPRESENTATIVE
  4. WHERE MEDAL.Cont_id=CONTESTANT.Cont_id 
  5. And CONTESTANT.Rep_id=REPRESENTATIVE.Rep_id 
  6. And REPRESENTATIVE.Athlete_id=ATHLETE.Athlete_id
  7. GROUP BY ATHLETE.Athlete_id
  8. HAVING COUNT(MEDAL.Cont_id) > 0
  9. ORDER BY COUNT(MEDAL.Cont_id) DESC) AS [# of Medal]
  10. FROM REPRESENTATIVE, ATHLETE
  11. WHERE ATHLETE.Athlete_id=REPRESENTATIVE.Athlete_id
  12. GROUP BY ATHLETE.Athlete_id, ATHLETE.Athlete_lname, ATHLETE.Athlete_fname;
  13.  
  14.  
It is doing what I want except the results from the subquery are not grouped. I want them to be grouped by the athlete, however, whenever I try it says that "at most only one record can be returned by this subquery".
Any help would be greatly appreciated
Oct 1 '10 #1
0 878

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

Similar topics

2
by: lev | last post by:
CREATE TABLE . ( NULL , , (44) ) ID is non-unique. I want to select all IDs where the last entry for that ID is of type 11.
3
by: Evgeny Gopengauz | last post by:
Let us suppose that we have a table: CREATE TABLE transactions( currencyID_1 int, value_1 money, currencyID_2 int, value_2 money )
2
by: Greg Stark | last post by:
I find I often want to be able to do joins against views where the view are aggregates on a column that has an index. Ie, something like SELECT a.*, v.n FROM a JOIN (select a_id,count(*) as n...
4
by: eliffman | last post by:
I get the following error when trying to run a report: Multi-level GROUP BY clause is not allowed in a subquery. (Error 3612) The report's recordsource is a query based on a single table....
1
by: Tim Marshall | last post by:
A2003. I am getting this error message when I try to set a report's recordsource to an SQL statement or a saved querydef that uses sub-queries. I've debug.printed the SQL, and run it as a stand...
6
by: phillip.s.powell | last post by:
update student s set school_year_id = (select distinct s.id from school_year s, interns i where lower(s.school_year_name) = lower(i.enrollment_year)and s.unique_key = i.unique_key group by s.id);...
2
by: dcourington | last post by:
I'm new to this group and not an experienced Access user, but have solved several of my problems already by reading other threads on this forum (Thanks!). Here's my current problem that I can't...
13
by: ThePrinceIsRight | last post by:
I have a problem with using a subquery in MS Access. The purpose of the sub-query is to create a list of people who have had doctor exams in the past 6 months and exclude them from the main query....
1
by: jcf378 | last post by:
Hi all-- Does anyone have any insight as to how I might create a search form that allows a user to select criteria based on any related table in the whole database. The search form I have now only...
1
by: mipo1984 | last post by:
I have subquery into a principal query, and i need the subquery return me only the last row of all results, but i can`t use "order by <field> desc " in the subquery because this return me an error,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.