Picking out top three out of database

T

Thomas

I have a database where I store info about football players goals. The
database contains the following:
GoalID, GoalScorerID, GoalMinute (which minute the goal was scored),
ClubID, FixtureID (to mark in which match the goal was scored) and
Penalty (which determines wether the goal was a penalty or not).

On the club page I want to show the top three goalscorers of the club.
I can do this but I don´t know how to get them in descending order
with the player who´s scored most goals at the top of the list.

Any ideas?
 
J

Jeff Cochran

I have a database where I store info about football players goals. The
database contains the following:
GoalID, GoalScorerID, GoalMinute (which minute the goal was scored),
ClubID, FixtureID (to mark in which match the goal was scored) and
Penalty (which determines wether the goal was a penalty or not).

On the club page I want to show the top three goalscorers of the club.
I can do this but I don´t know how to get them in descending order
with the player who´s scored most goals at the top of the list.

Any ideas?

Lots, some rather simple. Some may not apply to your database though.
If you tell us what database, we can suggest a solution.

(Hint: Check to see if your particular database supports the TOP
modifier in a SELECT statement...)

Jeff
 
T

Thomas Emilson

Sorry for not answering earlier but it´s been a bit much to do.

The db used is Access. I have no clue if it supports the TOP modifier.
If not Access is to be used which db should I use. I need it to be free.
 
B

Bob Barrows [MVP]

Thomas said:
Sorry for not answering earlier but it´s been a bit much to do.

The db used is Access. I have no clue if it supports the TOP modifier.
If not Access is to be used which db should I use. I need it to be
free.
TOP works fine in Access. In the future, specify your database type and
version right at the start of your post so we dont' have to ask.
 
T

Thomas

Yeah, I´ll think of that next time around, but back to the thing here.

So how one use this TOP modifier?
I´m not very good at sql so if anyone who feels they are would like to
explain this to me I would be grateful.

/Thomas
 
B

Bob Barrows [MVP]

Thomas said:
Yeah, I´ll think of that next time around, but back to the thing here.

So how one use this TOP modifier?
I´m not very good at sql so if anyone who feels they are would like to
explain this to me I would be grateful.
Well, Access DOES have online help. It also has a dandy Query Builder that
generates the SQL for you.


Basically, the syntax is
SELECT TOP x <column list> FROM <table/query>

Bob Barrows
 
T

Thomas

Hi again Bob!

I´ve been at the online help for access and looked at the top modifier
but I couldn´t find out how it could help me.
It could only get the top posts of the recordset based on the value of
one of the columns.

But my problem is that I don´t have a column (or a table) that contains
the total goals scored. In my table 'Goalscorers' I have all the goals
that has been done and in which minute it was scored (and by which
player). Then I do a For... exit to find out how many times a playerid
is found in the table (which gives me the number of goals he has
scored).

The table looks like this:

GoalID | GoalScorerID | GoalMinute | ClubID | FixtureID


Hope this helps anything.
 
B

Bob Barrows [MVP]

Thomas said:
Hi again Bob!

I´ve been at the online help for access and looked at the top modifier
but I couldn´t find out how it could help me.
It could only get the top posts of the recordset based on the value of
one of the columns.

But my problem is that I don´t have a column (or a table) that
contains the total goals scored. In my table 'Goalscorers' I have all
the goals that has been done and in which minute it was scored (and
by which player). Then I do a For... exit to find out how many times
a playerid is found in the table (which gives me the number of goals
he has scored).

The table looks like this:

GoalID | GoalScorerID | GoalMinute | ClubID | FixtureID


Hope this helps anything.

So now you need to look up grouping and aggregate functions to, well, group
and aggregate the goals scored by each player.

SELECT TOP 3 GoalScorerID, Count(*) As TotalGoals
FROM GoalScorers
GROUP BY GoalScorerID
ORDER BY TotalGoals DESC

Bob Barrows
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top