Retrieve distinct records on the basis of other columns

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Yogesh Sharma
    New Member
    • Mar 2008
    • 40

    Retrieve distinct records on the basis of other columns

    I want to select that rollnos which have distinct name & address.
    create table studento(roll int,name varchar,address varchar(5))
    insert into studento values(1,'A','A DD1')
    insert into studento values(2,'B','A DD2')
    insert into studento values(3,'A','A DD3')
    insert into studento values(4,'A','A DDR3')
    select name from studento

    I have tried the foll. query to retrive the roll that has distinct name,But query is still returning all roll numbers.

    select roll from studento where name = (Select distinct(name)
    from studento)
  • Delerna
    Recognized Expert Top Contributor
    • Jan 2008
    • 1134

    #2
    Hi Yogesh
    But all of those records that you show do have a distinct name and address

    Comment

    • Delerna
      Recognized Expert Top Contributor
      • Jan 2008
      • 1134

      #3
      also this query
      [code=sql]
      select roll from studento where name = (Select distinct(name)
      from studento)
      [/code]

      should generate a "subquery returned more than 1 value" error
      because there are 2 distinct names

      Comment

      • ck9663
        Recognized Expert Specialist
        • Jun 2007
        • 2878

        #4
        Originally posted by Yogesh Sharma
        I want to select that rollnos which have distinct name & address.
        create table studento(roll int,name varchar,address varchar(5))
        insert into studento values(1,'A','A DD1')
        insert into studento values(2,'B','A DD2')
        insert into studento values(3,'A','A DD3')
        insert into studento values(4,'A','A DDR3')
        select name from studento

        I have tried the foll. query to retrive the roll that has distinct name,But query is still returning all roll numbers.

        select roll from studento where name = (Select distinct(name)
        from studento)

        In your example, which roll will you choose for name A?

        If you're choosing the first one, try this:

        Code:
        select name,min(roll) as roll from studento
        group by name
        You may also take the name column from the SELECT list.

        -- CK

        Comment

        Working...