Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2016
    Posts
    2

    Answered: Find different values from DB

    Hi,.
    I've a db "t-shirt" with two columns "Name" and "Colour".


    Name Colour
    ------------------------
    Dario | Blue
    Francesco | Red
    Luca | Green
    Dario | Red
    Luca | Green
    Francesco | Red

    I want the code tell me if for the same name exits two different types of colour.
    I can't use "SELECT xxxx WHERE xxxx" because I don't have any fixed value to search.
    The code should do this :

    Take one by one the value from "Name" and check inside "Colour" if the values are the same.. if yes ok, otherwise... error!

    Ex : Dario -> Blue , Red . there are two different values.. ERROR
    Francesco -> Red , Red. Same Value. OK
    Luca -> Green , Green . Same Value. OK
    Dario -> already checked. go ahead


    Thanks a lot

  2. Best Answer
    Posted by gvee

    "Find those pesky shirts that have more than one colour available:
    Code:
    SELECT Name
         , Count(DISTINCT Colour) AS number_of_available_colours
    FROM   your_table
    GROUP
        BY Name
    HAVING Count(DISTINCT Colour) > 1
    "


  3. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,445
    Provided Answers: 12
    Find those pesky shirts that have more than one colour available:
    Code:
    SELECT Name
         , Count(DISTINCT Colour) AS number_of_available_colours
    FROM   your_table
    GROUP
        BY Name
    HAVING Count(DISTINCT Colour) > 1
    George
    Home | Blog

  4. #3
    Join Date
    Dec 2016
    Posts
    2

    Thumbs up

    thks gvee. Works perfect !

  5. #4
    Join Date
    Jan 2017
    Posts
    18
    Below given code will help you

    SELECT Name
    , Count(DISTINCT Colour) AS number_of_available_colours
    FROM your_table
    GROUP
    BY Name
    HAVING Count(DISTINCT Colour) > 1

Posting Permissions

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