Results 1 to 7 of 7

Thread: Counting members & selecting names [Resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Resolved Counting members & selecting names [Resolved]

    Hi,

    I'm using the same tables as in my previous post ( http://www.vbforums.com/showthread.php?t=328403 ), but I now need to count the number of members present on a date.

    The query I have so far is:
    SELECT Members.MemID, COUNT(RegMem.MemID) AS NumPresent FROM Members LEFT JOIN RegMem ON Members.MemID = RegMem.MemID GROUP BY Members.MemID;

    This returns a list of MemIDs against the number of sessions that they have been to, but I also need the names of the members, which are contained in the Members table and are matched uniquely to their MemID. I could do this in a second query, moving through each MemID in the first query to obtain a name, but I think it's possible in one query. I don't need the MemID returned and I can't just GROUP BY Surname because some members are siblings.

    Any ideas?
    Last edited by olamm2k; Mar 12th, 2005 at 03:23 AM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Re: Counting members & selecting names

    Just experimented some more - looks like I can just do:
    SELECT Members.Surname & Chr(44) & Chr(32) & Members.Forenames AS FullName, Count(RegMem.MemID) AS CountOfMemID
    FROM Members LEFT JOIN RegMem ON Members.MemID = RegMem.MemID
    GROUP BY Members.Surname, Members.Forenames;

  3. #3
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Counting members & selecting names [Resolved]

    Hi again,

    Do you not need SDate in your SELECT and GROUP BY clauses too? (To get the number present on each date.)
    Also, I think you'll be better with an INNER JOIN this time

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Re: Counting members & selecting names [Resolved]

    Well, I'll be modifying the query, but it's a bit different this time. I'm producing a report with member details, and one of the fields that I want to put on the report is how many sessions the member has been to in the past year, so it's not dependent on SDate, but on MemID.

    The reason it seems a little odd is because I was thinking of two separate reports - one for the sessions, one for the members. I suppose I just need to use:
    "SELECT COUNT(MemID) AS SessionsAttended FROM RegMem WHERE MemID = " & intMemID & ";"

    The first query was just to display a list of all the members, and how many sessions they've been to.
    Last edited by olamm2k; Mar 11th, 2005 at 05:50 PM.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Re: Counting members & selecting names

    Thinking about it, I need to extract all of the details from the Members table (Surname, Forenames etc) where the field MemID is equal to some passed intMemID along with the number of times that that MemID appears in the table RegMem.

    What's the best way to do this? Obviously, I could do it in two separate queries, but what about in one?

  6. #6
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313

    Re: Counting members & selecting names

    I'd have thought your original query would be pretty close. Something like this:
    VB Code:
    1. strSQL = "SELECT mem.Surname & Chr(44) & Chr(32) & mem.Forenames AS FullName, Count(reg.MemID) AS SessionAttended " & _
    2.      "FROM Members mem " & _
    3.      "INNER JOIN RegMem reg " & _
    4.      " ON mem.MemID = reg.MemID " & _
    5.      "WHERE mem.MemID = " & intMemID & " " & _
    6.      "GROUP BY mem.Surname, mem.Forenames"

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Re: Counting members & selecting names

    That's great, thanks!

    As to the other report, I can just use:
    SELECT COUNT(SDate) AS NumAttended FROM RegMem WHERE SDate = dteSDate;
    Last edited by olamm2k; Mar 12th, 2005 at 03:22 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width