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 > Data Access, Manipulation & Batch Languages > ANSI SQL > increment by one while ?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-28-08, 19:10
estefex estefex is offline
Registered User
 
Join Date: Jan 2004
Posts: 159
increment by one while ?

Hello,

I am trying to figure out how to assign a value of 1 to a record and it will increment by one while the customer number is still the same. Once the customer number changes it will assign that new record for that customer a one and increment by one and so on.

Below is a table that shows the data structure. The filed Count Inv by Cust is the output i am trying to get.

customer invoice amount Count_Inv_By_Cust
1 111 10.00 1
1 112 150.00 2
1 113 145.00 3
2 114 110.00 1
2 115 75.00 2



All help is appreciated.
Reply With Quote
  #2 (permalink)  
Old 01-28-08, 19:31
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
what database are you using? this isn't an ANSI SQL question

if it's msaccess, see this thread

if it's mysql, that feature is built in to the auto_increment facility

if it's some other database, you may want to investigate doing it in your application logic, e.g. with a transaction lock on retrieving the last number used

but the real question here is: why do your numbers have to have that format?

what's wrong with invoice numbers that are not magically tied to the customer?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-29-08, 10:31
estefex estefex is offline
Registered User
 
Join Date: Jan 2004
Posts: 159
Well the reason why i am trying to do this is because i am trying to come up with a method where i can compare every customers first invoice to their second invoice.

If there is an easier wahy to do this the idea is more than welcome. This was just one of the first solutions i thought of.

Thanks Again.
Reply With Quote
  #4 (permalink)  
Old 01-29-08, 10:40
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
every customer's first invoice is the invoice for that customer with the lowest date

every customer's second invoice is the invoice for that customer with the lowest date that isn't the lowest date

of course, this means you need to add a date to your invoice table
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 01-29-08, 10:48
estefex estefex is offline
Registered User
 
Join Date: Jan 2004
Posts: 159
Okay. The table does have a datetime field for the invoice date.

So i guess i would select the min(invoice_date) and min(invoice_numb) for the customers first invoice.

So to get the second invoice I would do the same but i can't be the same invoice as the one selected first. Let me see what i can come up with to get the second part.

Thanks Again.
Reply With Quote
  #6 (permalink)  
Old 01-29-08, 11:17
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
here's the customer's first invoice --
Code:
select ...
  from invoices as t
 where invoice_date =
       ( select max(invoice_date)
           from invoices
          where customer = t.customer )
the second invoice involves another nesting
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 01-30-08, 14:28
sco08y sco08y is offline
Registered User
 
Join Date: Oct 2002
Location: Baghdad, Iraq
Posts: 697
I ran into a similar problem... in general, given a table Invoices like yours:

SELECT P.*, (SELECT COUNT(*) FROM Invoices Q WHERE P.customer = Q.customer AND P.invoice_date> Q.invoice_date) AS InvoiceOrder FROM Invoices P

Err... maybe that should be a less than sign. Anyway, save it as a view and you can find the Nth invoice per customer quite easily.
Reply With Quote
  #8 (permalink)  
Old 01-30-08, 14:47
estefex estefex is offline
Registered User
 
Join Date: Jan 2004
Posts: 159
Increment by 1 while Customer ID is the Same

Hello,

I am trying to figure out how to assign a value of 1 to a record and it will increment by one while the customer id is still the same. Once the customer id changes it will assign that new record for that customer a 1 and increment by one and so on.

Below is a table that shows the data structure. The filed Count Inv by Cust is the output i am trying to get.

customer invoice amount Count_Inv_By_Cust
1 111 10.00 1
1 112 150.00 2
1 113 145.00 3
2 114 110.00 1
2 115 75.00 2



All help is appreciated.
Reply With Quote
  #9 (permalink)  
Old 01-30-08, 14:59
tprupsis tprupsis is offline
Registered User
 
Join Date: Jan 2008
Location: Billings, MT
Posts: 14
You can use ROW_NUMBER() to do this.

SELECT Customer,
Invoice,
Amount,
ROW_NUMBER() OVER (PARTITION BY Customer ORDER BY Invoice) AS RecID
FROM TableName
__________________
Tom Rupsis
Granite Peak Systems
Phone: 406-672-8292
Email: trupsis@granitepeaksys.com
LinkedIn: www.linkedin.com/in/trupsis
Reply With Quote
  #10 (permalink)  
Old 01-30-08, 15:03
estefex estefex is offline
Registered User
 
Join Date: Jan 2004
Posts: 159
Perfect! That worked exactly the way i needed it to.

Thank you very much!
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