SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2003
    Location
    NoVA
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Access] Select fields without grouping by them

    Let's say I have a table with these fields:

    ID, LAST_NAME, FIRST_NAME, PROGRAM_CODE, PGM_YEAR

    And there's at least one entry per person for each year they served. I'm trying to list people who have more than one PGM_YEAR row listed, and I need to list all of those fields in the resulting query.

    So here's what I tried:

    Code:
    SELECT * FROM (
    SELECT LAST_NAME, FIRST_NAME, Count(PGM_YEAR) AS YearsServed
    FROM [Table]
    GROUP BY LAST_NAME, FIRST_NAME, PGM_YEAR)
    WHERE YearsServed > 1;
    I also need to select PROGRAM_CODE but definitely don't want to group by that. Is there a way around that?
    Last edited by tsigo; May 3, 2004 at 09:50.

  2. #2
    SitePoint Addict
    Join Date
    Dec 2002
    Location
    Gent, Belgium, Europe, World, Univers
    Posts
    397
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you do not want to group by it, then there can be only 1 for every
    LAST_NAME, FIRST_NAME, PGM_YEAR
    That can be a problem. 1 solution is to do MAX(PROGRAM_CODE) and you won't have to group by it. (or MIN() ).

    BTW, you can ditch the subquery

    SELECT LAST_NAME, FIRST_NAME, max(PROGRAM_CODE), Count(PGM_YEAR) AS YearsServed
    FROM [Table]
    GROUP BY LAST_NAME, FIRST_NAME, PGM_YEAR
    HAVING Count(PGM_YEAR) > 1

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,216
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    you don't want to include PGM_YEAR in the GROUP BY because then the count will be 1 for each year

    i think!!

    perhaps, tsigo, you could give a few sample rows of data, and show the result rows that you want
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

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