grouping error?

  • Thread starter Thread starter pat67
  • Start date Start date
P

pat67

Hi I am trying to make a query that will show the all-time stats for
my pool league. So I have 2009 stats, 2010 stats and 2011 stats. Here
is the issue say we have Joe played for Team 1 all 3 years and was
we'll say 20-10, 25-10 then 20-15. writing a select query you get Joe
Team 1 65-35. Now say Bill played for Team 1 in 2009 but for Team 2 in
2010 and 2011. Using the same records the query shows this

Bill Team 1 20-10
Bill Team 2 45-25

What I am looking to do is show Bill as say Bill Team 1/Team 2 65-35
so it shows both teams he played for and totals his wins and losses.
Any ideas?
 
pat67 said:
Hi I am trying to make a query that will show the all-time stats for
my pool league. So I have 2009 stats, 2010 stats and 2011 stats. Here
is the issue say we have Joe played for Team 1 all 3 years and was
we'll say 20-10, 25-10 then 20-15. writing a select query you get Joe
Team 1 65-35. Now say Bill played for Team 1 in 2009 but for Team 2 in
2010 and 2011. Using the same records the query shows this

Bill Team 1 20-10
Bill Team 2 45-25

What I am looking to do is show Bill as say Bill Team 1/Team 2 65-35
so it shows both teams he played for and totals his wins and losses.
Any ideas?

Don't group by team. You will need Duane Hookom's Concatenate function
(google for it) to concatenate the teams.

PS. If that's not enough information for you, think of it as payback for the
lack of information about your table structure that you supplied ;-)
 
Don't group by team. You will need Duane Hookom's Concatenate function
(google for it) to concatenate the teams.

PS. If that's not enough information for you, think of it as payback for the
lack of information about your table structure that you supplied ;-)

There was no lack of info for the table. there is a year, a player, a
team, wins and losses. Just like i said.

So for Bill it would be

2009 Bill Team 1 20-10
2010 Bill Team 2 25-10
2011 Bill Team 2 20-15

which is exactly what i said. that being said, i will try and
concatenate the teams.
 
pat67 said:
There was no lack of info for the table. there is a year, a player, a
team, wins and losses. Just like i said.

So for Bill it would be

2009 Bill Team 1 20-10
2010 Bill Team 2 25-10
2011 Bill Team 2 20-15

Very clear ... ? ... errr ... is that four, or five, fields you're showing
me here? What are the names of the relevant fields? What are the datatypes
of the relevant fields? What is the name of the table? What is the primary
key for the table?
What is the sql for the query that produced the incorrect results?
What version of Access are you using? Is it an mdb or accdb?

Do you still think you provided sufficient information? I don't. :-)
Yes, some of the info is overkill for this particular problem, but most of
it is essential even for this one. If you make a habit of providing all of
this information, upfront, whenever you post a question, you will reduce
the time required for us to provide a solution. And you will likely get a
complete, explicit and working solution rather than vague hints/guesses
about what to try.
which is exactly what i said. that being said, i will try and
concatenate the teams.

As I said, you will need Duane's Concatenate function for that Here's a
link to a sample database containing the function:
http://www.rogersaccesslibrary.com/forum/topic16.html
 
Very clear ... ? ... errr ... is that  four, or five, fields you're showing
me here? What are the names of the relevant fields? What are the datatypes
of the relevant fields? What is the name of the table? What is the primary
key for the table?
What is the sql for the query that produced the incorrect results?
What version of Access are you using? Is it an mdb or accdb?

Do you still think you provided sufficient information? I don't. :-)
Yes, some of the info is overkill for this particular problem, but most of
it is essential even for this one. If you make a habit of providing all of
this information, upfront,  whenever you post a question, you will reduce
the time required for us to provide a solution. And you will likely get a
complete, explicit and working solution rather than vague hints/guesses
about what to try.




As I said, you will need Duane's Concatenate function for that  Here's a
link to a sample database containing the function:http://www.rogersaccesslibrary.com/forum/topic16.html- Hide quoted text -

- Show quoted text -

Fine

Year Player Team Won Lost
2009 Bill Team 1 20 10
2010 Bill Team 2 25 10
2011 Bill Team 2 20 15

better?
 
pat67 said:
Fine

Year Player Team Won Lost
2009 Bill Team 1 20 10
2010 Bill Team 2 25 10
2011 Bill Team 2 20 15

better?

A little, but ... look back at the questions I asked. Did you answer them
all? Again, I am not going to be able to provide a working solution.

So have you downloaded the sample database containing the function you need?
Once you import the module containing the function, you'll be able to use it
in a query. The idea is to create two saved queries, the first to calculate
the wins and losses per year:

select [year],player, sum(wins),sum(losses)
from tablename
group by [year],player

and a second that uses Duane's function (I forget the syntax so you will
need to look at the documentation to get it exactly right):

select [year],player,
Concatenate("tablename", "[year],player","Team") As Teams
from tablename
group by [year],player

Then join the two queries in a third query.

Alternatively,
a. you could forget about concatenating and use the crosstab query wizard to
create a saved query to pivot the teams. Then join the two queries as above.
b. you could forget about concatenating and simply display the teams in a
subreport in the report that uses the first query to show the totals.


Incidently, "Year" is a reserved keyword and should not be used for a field
name. As it is the name of a VBA function, its use as a field name can lead
to very hard to debug problems down the line. At the very least, you need to
remember to use brackets around it when you use it in queries. A better
solution is to simply rename the field, making it more descriptive so that
two years from now, you'll be able to tell from looking at it what it's the
year of.
 
pat67 said:
Year  Player    Team     Won  Lost
2009   Bill      Team 1     20     10
2010   Bill      Team 2     25     10
2011   Bill      Team 2     20     15

A little, but ... look back at the questions I asked. Did you answer them
all? Again, I am not going to be able to provide a working solution.

So have you downloaded the sample database containing the function you need?
Once you import the module containing the function, you'll be able to useit
in a query. The idea is to create two saved queries, the first to calculate
the wins and losses per year:

select [year],player, sum(wins),sum(losses)
from tablename
group by [year],player

and a second that uses Duane's function (I forget the syntax so you will
need to look at the documentation to get it exactly right):

select [year],player,
Concatenate("tablename", "[year],player","Team") As Teams
from tablename
group by [year],player

Then join the two queries in a third query.

Alternatively,
a. you could forget about concatenating and use the crosstab query wizardto
create a saved query to pivot the teams. Then join the two queries as above.
b. you could forget about concatenating and simply display the teams in a
subreport in the report that uses the first query to show the totals.

Incidently, "Year" is a reserved keyword and should not be used for a field
name. As it is the name of a VBA function, its use as a field name can lead
to very hard to debug problems down the line. At the very least, you needto
remember to use brackets around it when you use it in queries. A better
solution is to simply rename the field, making it more descriptive so that
two years from now, you'll be able to tell from looking at it what it's the
year of.- Hide quoted text -

- Show quoted text -

tried using that concatenate function, but when i then run the query
i get an error. Missing syntax error (missing operator) in query
expression 'PlayerID=Tim Cooney'

here is my actual query sql

SELECT Archive_tbl_Final_Stats_Totals.PlayerID, Concatenate("SELECT
[Team Name] FROM qryAll_Teams WHERE PlayerID =" & [PlayerID]) AS
[Team(s)]
FROM Archive_tbl_Final_Stats_Totals;

here is the function syntax

Function Concatenate(pstrSQL As String, _
Optional pstrDelim As String = ", ") _
As String
'Created by Duane Hookom, 2003
'this code may be included in any application/mdb providing
' this statement is left intact
'example
'tblFamily with FamID as numeric primary key
'tblFamMem with FamID, FirstName, DOB,...
'return a comma separated list of FirstNames
'for a FamID
' John, Mary, Susan
'in a Query
'SELECT FamID,
'Concatenate("SELECT FirstName FROM tblFamMem
' WHERE FamID =" & [FamID]) as FirstNames
'FROM tblFamily
'

'======For DAO uncomment next 4 lines=======
'====== comment out ADO below =======
'Dim db As DAO.Database
'Dim rs As DAO.Recordset
'Set db = CurrentDb
'Set rs = db.OpenRecordset(pstrSQL)

'======For ADO uncomment next two lines=====
'====== comment out DAO above ======
Dim rs As New ADODB.Recordset
rs.Open pstrSQL, CurrentProject.Connection, _
adOpenKeyset, adLockOptimistic
Dim strConcat As String 'build return string
With rs
If Not .EOF Then
.MoveFirst
Do While Not .EOF
strConcat = strConcat & _
.Fields(0) & pstrDelim
.MoveNext
Loop
End If
.Close
End With
Set rs = Nothing
'====== uncomment next line for DAO ========
'Set db = Nothing
If Len(strConcat) > 0 Then
strConcat = Left(strConcat, _
Len(strConcat) - Len(pstrDelim))
End If
Concatenate = strConcat
End Function


The error shows during the

rs.Open pstrSQL, CurrentProject.Connection, _
adOpenKeyset, adLockOptimistic
Dim strConcat As String 'build return string

part of the function. At this point i am clueless.
 
A little, but ... look back at the questions I asked. Did you answer them
all? Again, I am not going to be able to provide a working solution.
So have you downloaded the sample database containing the function you need?
Once you import the module containing the function, you'll be able to use it
in a query. The idea is to create two saved queries, the first to calculate
the wins and losses per year:
select [year],player, sum(wins),sum(losses)
from tablename
group by [year],player
and a second that uses Duane's function (I forget the syntax so you will
need to look at the documentation to get it exactly right):
select [year],player,
Concatenate("tablename", "[year],player","Team") As Teams
from tablename
group by [year],player
Then join the two queries in a third query.
Alternatively,
a. you could forget about concatenating and use the crosstab query wizard to
create a saved query to pivot the teams. Then join the two queries as above.
b. you could forget about concatenating and simply display the teams ina
subreport in the report that uses the first query to show the totals.
Incidently, "Year" is a reserved keyword and should not be used for a field
name. As it is the name of a VBA function, its use as a field name can lead
to very hard to debug problems down the line. At the very least, you need to
remember to use brackets around it when you use it in queries. A better
solution is to simply rename the field, making it more descriptive so that
two years from now, you'll be able to tell from looking at it what it'sthe
year of.- Hide quoted text -
- Show quoted text -

 tried using that concatenate function, but when i then run the query
i get an error. Missing syntax error (missing operator) in query
expression 'PlayerID=Tim Cooney'

here is my actual query sql

SELECT Archive_tbl_Final_Stats_Totals.PlayerID, Concatenate("SELECT
[Team Name] FROM qryAll_Teams WHERE PlayerID =" & [PlayerID]) AS
[Team(s)]
FROM Archive_tbl_Final_Stats_Totals;

here is the function syntax

Function Concatenate(pstrSQL As String, _
        Optional pstrDelim As String = ", ") _
        As String
'Created by Duane Hookom, 2003
'this code may be included in any application/mdb providing
'   this statement is left intact
'example
'tblFamily with FamID as numeric primary key
'tblFamMem with FamID, FirstName, DOB,...
'return a comma separated list of FirstNames
'for a FamID
' John, Mary, Susan
'in a Query
'SELECT FamID,
'Concatenate("SELECT FirstName FROM tblFamMem
' WHERE FamID =" & [FamID]) as FirstNames
'FROM tblFamily
'

'======For DAO uncomment next 4 lines=======
'====== comment out ADO below =======
    'Dim db As DAO.Database
    'Dim rs As DAO.Recordset
    'Set db = CurrentDb
    'Set rs = db.OpenRecordset(pstrSQL)

'======For ADO uncomment next two lines=====
'====== comment out DAO above ======
    Dim rs As New ADODB.Recordset
    rs.Open pstrSQL, CurrentProject.Connection, _
    adOpenKeyset, adLockOptimistic
    Dim strConcat As String 'build return string
    With rs
        If Not .EOF Then
            .MoveFirst
            Do While Not .EOF
                strConcat = strConcat & _
                .Fields(0) & pstrDelim
                .MoveNext
            Loop
        End If
        .Close
    End With
    Set rs = Nothing
'====== uncomment next line for DAO ========
    'Set db = Nothing
    If Len(strConcat) > 0 Then
        strConcat = Left(strConcat, _
        Len(strConcat) - Len(pstrDelim))
    End If
    Concatenate = strConcat
End Function

The error shows during the

 rs.Open pstrSQL, CurrentProject.Connection, _
    adOpenKeyset, adLockOptimistic
    Dim strConcat As String 'build return string

part of the function. At this point i am clueless.- Hide quoted text -

- Show quoted text -

I got it to work. I just need to tweek some things now. I missed where
the ID needed to be a numeric value. I was using names. Thanks for the
help.
 
pat67 said:
I got it to work. I just need to tweek some things now. I missed where
the ID needed to be a numeric value. I was using names. Thanks for the
help.

Excellent - knew you could do it!
 
Back
Top