Results 1 to 5 of 5

Thread: SELECT DISTINCT on 2 columns

  1. #1
    Join Date
    Jul 2004
    Posts
    106

    SELECT DISTINCT on 2 columns

    with SELECT DISTINCT a,b,c,d FROM x i get distinct abcd value

    how can I do SELECT DISTINCT a,b,c,d FROM x but only distinct on a,b (no matter if c,d are equals or differents) ?

    I am using MS SQL 2000 and Accees 2000

    thank you

  2. #2
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    Post example data and result you want.

    You can't do partial distinct in a SELECT, you can group by a, b and get max or min of c and d

    select a,b,min(c), min(d)
    from x
    group by a, b

  3. #3
    Join Date
    Jul 2004
    Posts
    106
    if I have

    1, a1, b1
    2, a1, b1
    3, a1, b2


    i want to get

    1, a1, b1
    3, a1, b2


    let's say the first one found (no matter which one)

    thank you

  4. #4
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    create table t1(id int, col1 varchar(3),col2 varchar(3))
    insert into t1 select 1, 'a1', 'b1'
    insert into t1 select 2, 'a1', 'b1'
    insert into t1 select 3, 'a1', 'b2'

    select min(id) as ID,col1,col2 from t1
    group by col1,col2

  5. #5
    Join Date
    Jul 2004
    Posts
    106
    it works very well , thanks a lot !

Posting Permissions

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