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 |
|
mk_garg20
Constraint Violating Yak Guru
343 Posts |
Posted - 2008-05-13 : 20:18:27
|
| Hi,What is the best to update line_no in following code.As you can see 1 invoice can have multiple lines.CREATE TABLE #xx (invoicenumber INT, line_no int )INSERT INTO #xx VALUES(1000,0)INSERT INTO #xx VALUES(1000,0)INSERT INTO #xx VALUES(1000,0)INSERT INTO #xx VALUES(1000,0)INSERT INTO #xx VALUES(2000,0)INSERT INTO #xx VALUES(2000,0)INSERT INTO #xx VALUES(2000,0)INSERT INTO #xx VALUES(2000,0)SELECT * FROM #xxresult of select * from #xx should be:1000 11000 11000 11000 12000 22000 22000 22000 2Thanksmk_garg |
|
|
singularity
Posting Yak Master
153 Posts |
Posted - 2008-05-13 : 20:22:54
|
| Just taking a guess as to how the line_no field is derived...update aset a.line_no = a.invoicenumber / 1000from #xx a |
 |
|
|
mk_garg20
Constraint Violating Yak Guru
343 Posts |
Posted - 2008-05-13 : 20:31:03
|
| Hi,These invoice numbers are just example.I found out, i have to use DENSE_RANK.UPDATE tSET t.line_no = t.RowNoFROM (SELECT ROW_DENSE_RANK() OVER (ORDER by invoicenumber) AS RowNo,invoicenumber,line_noFROM #xx)tThanksmk_garg |
 |
|
|
mk_garg20
Constraint Violating Yak Guru
343 Posts |
Posted - 2008-05-13 : 20:32:45
|
| sorrythis should workUPDATE tSET t.line_no = t.RowNoFROM (SELECT DENSE_RANK() OVER (ORDER by invoicenumber) AS RowNo,invoicenumber,line_noFROM #xx)tmk_garg |
 |
|
|
|
|
|
|
|