If this is your first visit, be sure to check out the FAQ by clicking the link above. You may 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.

 
Go Back  dBforums > Database Server Software > MySQL > How to write this SQL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-25-10, 20:13
rchiu5hk rchiu5hk is offline
Registered User
 
Join Date: Apr 2008
Posts: 2
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
Reply With Quote
  #2 (permalink)  
Old 11-25-10, 20:26
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT t.OrderHeaderID 
     , ( SELECT COUNT(*)
           FROM datas
          WHERE OrderHeaderID = t.OrderHeaderID
            AND OrderDetailID < t.OrderDetailID ) AS cnt
  FROM datas AS t
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On