Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2007
    Posts
    32

    Unanswered: shows the id that have 3 records and fewer than 2 NO

    Hi
    I have some records in a table 'Preference'
    I would like to return the id that have 3 records but less than 2 'N' in the 'Access' column (sample table below)
    It is suppose to return

    ID
    2

    I tried a query below but it doesn't return any record
    Any idea where I did wrong? Thanks
    Code:
    select distinct ID from Preference where ID in (select distinct ID from Preference group by ID having count(Access)=3) and  Access = 'N' group by Access having count(*) < 2
    ID,Access,Product
    1,N,car
    1,N,bike
    1,Y,boat
    1,Y,bicycle
    1,Y,truck
    2,N,car
    2,N,bike
    2,Y,boat
    3,N,car
    3,N,bike
    3,N,boat

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    SELECT id
      FROM Preference 
    GROUP
        BY id
    HAVING COUNT(*) = 3
       AND COUNT(CASE WHEN Access = 'N' 
                      THEN 'oops' 
                      ELSE NULL END) < 2
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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