Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2008
    Posts
    6

    Unanswered: How to write this SQL

    Dear All,

    I have the table datas as below:
    OrderHeaderID OrderDetailID
    5001 6001
    5001 6002
    5001 6003
    5002 6004
    5002 6005

    How to write SQL for showing as below??
    5001 0
    5001 1
    5001 2
    5002 0
    5002 1

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    SELECT t.OrderHeaderID 
         , ( SELECT COUNT(*)
               FROM datas
              WHERE OrderHeaderID = t.OrderHeaderID
                AND OrderDetailID < t.OrderDetailID ) AS cnt
      FROM datas AS t
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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