Results 1 to 7 of 7
  1. #1
    gesch is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    2

    Question Query Problem (Distinct values from 2 rows)

    Hello,

    I have a report I'm importing into Access where I need to generate a a count of unique reservations rather than just a count of people at the table for each unique table type.

    I.e. if I have the following table:

    Code:
    Table | Reservation | Name
    Banquet	|	1	|	Larry David
    Banquet	|	2	|	Jeff Garlin
    Banquet	|	2	|	Suzie Yellsalot
    Banquet	|	3	|	Marty Funkhowser
    Banquet	|	3	|	Miss Funkhowser
    Group1	|	4	|	Jason Alexander
    Group1	|	5	|	Elaine Bennis
    Group1	|	6	|	Jerry Seinfeld
    Group1	|	7	|	Kramer
    Group2	|	8	|	Thomas Jefferson
    Group2	|	8	|	Martha Jefferson
    Group2	|	8	|	Edith Jefferson
    Group2	|	9	|	Ronald Reagan
    I am trying to construct a query that will return the following:

    Code:
    Table | # of Reservations
    Banquet | 3
    Group1 | 4
    Group2 | 2
    I am able to get the count of one or the other using SELECT DISTINCT. Is there a way in a single query to get the count of distinct reservations for each distinct Table?



    Any help would be greatly appreciated

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    This is going to be a two step process. I duplicated your data and put it in a table named Tbl_Reservations

    use this SQL code and save it as Query1
    Code:
    SELECT Tbl_Reservations.Table, Tbl_Reservations.Reservation
    FROM Tbl_Reservations
    GROUP BY Tbl_Reservations.Table, Tbl_Reservations.Reservation;
    use this SQL code and save it as Query2
    Code:
    SELECT Query1.Table, Count(Query1.Reservation) AS TotalReservations
    FROM Query1
    GROUP BY Query1.Table;

  3. #3
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    I was able to achieve your expected results by building two aggregate queries. The second one is the only one you need to run as it is based upon the first.

    See attached.

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    hah, same solution

  5. #5
    gesch is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    2
    Thanks guys, that worked great. Much appreciated

    Seems so logical when split into two queries.

  6. #6
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    rpeare;
    Great minds thinking alike.

    Alan

  7. #7
    gap is offline Novice
    Windows Vista Access 2007
    Join Date
    Jul 2011
    Posts
    12
    I had the same problem ones.
    Here is a solution in just one query using select distinct:

    Code:
    SELECT Table1, Count (t.Reservation) AS NumberofR
    FROM (SELECT DISTINCT Table1, Reservation FROM Test) As t
    GROUP BY Table1;

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Query for distinct member number
    By Lauri in forum Queries
    Replies: 1
    Last Post: 08-23-2013, 03:22 AM
  2. DISTINCT on a Union All query?
    By Robeen in forum Queries
    Replies: 3
    Last Post: 04-27-2011, 10:30 AM
  3. help counting distinct values
    By joo-joo-man in forum Queries
    Replies: 2
    Last Post: 10-17-2010, 05:18 AM
  4. Replies: 2
    Last Post: 11-18-2009, 06:49 PM
  5. Distinct Values
    By Acramer8 in forum Reports
    Replies: 1
    Last Post: 06-15-2009, 08:37 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums