• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

SQL brain dead ?

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    SQL brain dead ?

    table
    cols
    id_num group num
    902 738
    456 738
    012 738
    921 602
    223 602
    114 559
    948 559
    335 559

    want to pick ONE row in EACH group_num (there are 250 groups)

    so only 902, 921, 114

    in one query

    how? sorry gone brain -

    #2
    Which DB are you using? I don't think there is an efficient way to do it but you could do:

    select * from mytable where id_num in (select min(id_num) from table group by group_num)

    Comment


      #3
      Originally posted by bored View Post
      Which DB are you using? I don't think there is an efficient way to do it but you could do:

      select * from mytable where id_num in (select min(id_num) from table group by group_num)
      thanks, tried that one doesn't work cos you need id_num in group clause

      SQL Server 2000

      it looks simple to do - there must be a way to do this easily

      humpf - looks like t-sql.....
      Last edited by Fishface; 22 February 2008, 19:23. Reason: t-sql

      Comment


        #4
        select min(id_num) from tbl group by group_num

        or

        select min(id_num), group_num from tbl group by group_num

        (if you need group_num included)

        Or am i missing the point?
        Assuming that the column types are integers and it's ok to get the lowest id_num from each group.
        Last edited by chicane; 22 February 2008, 19:39.

        Comment


          #5
          Originally posted by chicane View Post
          select min(id_num) from tbl group by group_num

          or

          select min(id_num), group_num from tbl group by group_num

          (if you need group_num included)

          Or am i missing the point?
          Assuming that the column types are integers and it's ok to get the lowest id_num from each group.
          doesn't work because columns in the select statment have to be in the group clause - in sql svr 2000.

          Comment


            #6
            Both queries work in SQL Server 2000 here.

            Code:
            select min(id_num) as minidnum, group_num from tbl group by group_num
            
            minidnum    group_num   
            ----------- ----------- 
            114         559
            223         602
            12          738
            
            (3 row(s) affected)

            Comment


              #7
              Originally posted by Fishface View Post
              thanks, tried that one doesn't work cos you need id_num in group clause
              It does work because I'm selecting min(id_num), an aggregate function, and not just id_num.

              Comment


                #8
                if you only want the id_num column

                Code:
                select id_num
                  from (
                select min(id_num) as id_num, group_num
                  from table
                  group by group_num) as T1
                Coffee's for closers

                Comment


                  #9
                  Originally posted by Spacecadet View Post
                  if you only want the id_num column
                  ....
                  Or just:

                  Code:
                  select min(id_num) as id_num from tbl group by group_num

                  Comment


                    #10
                    Originally posted by chicane View Post
                    Or just:

                    Code:
                    select min(id_num) as id_num from tbl group by group_num
                    I stand very much corrected!
                    Coffee's for closers

                    Comment

                    Working...
                    X