Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2012
    Posts
    6

    Unanswered: Select if all values equal

    What I'd like to do is select distinct values for one field if ALL values for another field are identical - like this:

    SELECT DISTINCT A, B FROM TABLE WHERE (ALL B=1)

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    could you repeat the question please?

    perhaps with a better example
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Apr 2012
    Posts
    6

    Sure

    I want to select distinct values in column A (which holds names), but only if all the values in column B (which holds numerical values) = 1. So if there's a single record in column B holding any value other than a "1", the name in column A should not be shown.

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    SELECT a
      FROM daTable
    GROUP
        BY a
    HAVING 0 = COUNT(CASE WHEN b <> 1 THEN 'uh oh' END)
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  5. #5
    Join Date
    Apr 2012
    Posts
    6

    Sorry, No Go

    It doesn't work (freezes the w3wp.exe), but thanks for trying. By the way, I visited the Simply SQL to check out your book but there are no links there.

  6. #6
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    it wasn't just "trying" -- that's the solution for the problem you posted

    at least, for this problem

    if it's actually related to your other problem then i'm afraid i don't understand either of them

    and yes, i know there's no link there, but it's a long story
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  7. #7
    Join Date
    May 2009
    Posts
    258
    Seems pretty straightforward:
    Code:
    SELECT DISTINCT A FROM TABLE WHERE B=1
    Ax

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
  •