Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2003
    Posts
    223

    Unanswered: How to select the first row for each group?

    Hello, everyone:

    There is a table as below that has three groups regarding to cus. How to select first cid for each group. Thanks.

    zyt

    cus cid
    ALFKI 10643
    ALFKI 10692
    ALFKI 10702
    ALFKI 10835
    ALFKI 10952
    ALFKI 11011
    ANATR 10308
    ANATR 10625
    ANATR 10759
    ANATR 10926
    ANTON 10365
    ANTON 10507
    ANTON 10535
    ANTON 10573
    ANTON 10677
    ANTON 10682
    ANTON 10856
    AROUT 10355
    AROUT 10383
    AROUT 10453
    AROUT 10558
    AROUT 10707
    AROUT 10741
    AROUT 10743
    AROUT 10768
    AROUT 10793
    AROUT 10864
    AROUT 10920
    AROUT 10953
    AROUT 11016

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    select cus
         , cid
      from yourtable as X
     where cid =
         ( select min(cid)
             from yourtable
            where cus = X.cus )
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Feb 2004
    Posts
    492
    or:

    select cus, min(cid)
    from ...
    group by cus

Posting Permissions

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