Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
AJL
Starting Member
7 Posts |
Posted - 2007-06-09 : 02:06:21
|
| SELECT pid, lname, visit_date, quantityFROM customerORDER BY pidpid lname visit_date quantity-------------------------------------------23 wang 07/23/2006 10023 wang 07/30/2006 14023 wang 08/05/2006 13023 wang 08/15/2006 13523 wang 08/22/2006 11034 linden 06/23/2006 9934 linden 07/06/2006 11034 linden 07/15/2006 12034 linden 08/26/2006 99How do I use cursor (or is there a better way) to create an additional column "index"? "Index" starts at 1 for every unique pid index pid lname visit_date quantity----------------------------------------------------------1 23 wang 07/23/2006 1002 23 wang 07/30/2006 1403 23 wang 08/05/2006 1304 23 wang 08/15/2006 1355 23 wang 08/22/2006 1101 34 linden 06/23/2006 992 34 linden 07/06/2006 1103 34 linden 07/15/2006 1204 34 linden 08/26/2006 99Please let me know if I can explain the my question better. Thanks in advance |
|
|
PeterNeo
Constraint Violating Yak Guru
357 Posts |
Posted - 2007-06-09 : 02:34:41
|
| For this u dont need a cursor, try thisSELECT pid, lname, visit_date, quantity, ( SELECT COUNT(*) FROM customer WHERE pid = C.pid AND visit_date <= C.visit_date) AS 'index'FROM customer CORDER BY pidif u got large data then use update. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-09 : 02:42:18
|
| Where do you want to show data?Note that if the table has millions of rows, the above query will run for hoursIf you want to show data in front end, you can easily do numbering thereMadhivananFailing to plan is Planning to fail |
 |
|
|
AJL
Starting Member
7 Posts |
Posted - 2007-06-09 : 03:21:21
|
| Thank you all.PeterNeo, it worked wonderfully!! Madhivanan, thank you for your suggestion, I will remember it for future reference. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-09 : 06:52:21
|
| Also, if you want to show data in Report, you can group it by pid and Reset Recordnumber for each groupMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|