| Author |
Topic |
|
ashley.sql
Constraint Violating Yak Guru
299 Posts |
Posted - 2008-04-18 : 11:45:32
|
| I have a table like thiscreate table #invoice (invoice_no varchar(5), invoice_amount int)INSERT INTO #invoice SELECT 'A', 100 UNIONSELECT 'A', 200 UNIONSELECT 'A', 300 UNIONSELECT 'B', 400 UNIONSELECT 'B', 500 UNIONSELECT 'B', 600 UNIONSELECT 'B', 700 Now I want a output like thisINVOICE_NO AMOUNT SEQUENCE_NOA 100 1A 200 2A 300 3B 400 1B 500 2B 600 3B 700 4The sequence_no should set back to 1 on change of invoice_nothese are items in a an invoice. they just want a sequence_noI can do this using cursors, but if anyone can help with WHILE LOOPwould be greatTHANKS-----------------------------------------------------------------------------------------------Ashley Rhodes |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2008-04-18 : 12:28:56
|
| Not a while loop butselect invoice_no, invoice_amount, sequence_no = rank() over (partition by invoice_no order by invoice_amount)from #invoice==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
ashley.sql
Constraint Violating Yak Guru
299 Posts |
Posted - 2008-04-18 : 12:33:37
|
| I don't have 2005 its 2000-----------------------------------------------------------------------------------------------Ashley Rhodes |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-18 : 15:05:50
|
| [code]SELECT (SELECT COUNT(*) + 1 FROM YourTable WHERE invoice_no=t.invoice_no AND invoice_amount < t.invoice_amount) AS Seq,invoice_no, invoice_amountFROM YourTable t[/code] |
 |
|
|
ashley.sql
Constraint Violating Yak Guru
299 Posts |
Posted - 2008-04-18 : 15:16:01
|
| YOUR QUERY FAILS WHEN THE DATA IS LIKE THISTHE DIFFERENT ITEMS IN INVOICE ARE VERY LIKELY TO HAVE SAME AMOUNTINSERT INTO #invoice SELECT 'A', 100 UNIONSELECT 'A', 100 UNIONSELECT 'A', 100 UNIONSELECT 'B', 100 UNIONSELECT 'B', 100 UNIONSELECT 'B', 100 UNIONSELECT 'B', 100 -----------------------------------------------------------------------------------------------Ashley Rhodes |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-18 : 15:27:56
|
try this then:-create table #invoice (id int identity(1,1),invoice_no varchar(5), invoice_amount int)INSERT INTO #invoice (invoice_no,invoice_amount)SELECT 'A', 100 UNIONSELECT 'A', 200 UNIONSELECT 'A', 300 UNIONSELECT 'B', 400 UNIONSELECT 'B', 500 UNIONSELECT 'B', 600 UNIONSELECT 'B', 700SELECT ID - (SELECT COUNT(*) FROM YourTable WHERE invoice_no<>t.invoice_no AND ID<t.ID) AS Seq,invoice_no, invoice_amountFROM YourTable t |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2008-04-18 : 15:35:04
|
| sorryselect *, identity(int,1,1) as idinto #a from #invoiceSELECT (SELECT COUNT(*) + 1 FROM #a t2 WHERE invoice_no=t.invoice_no AND (t2.invoice_amount < t.invoice_amount or (t2.invoice_amount = t.invoice_amount and t2.id < t.id))) AS Seq,invoice_no, invoice_amountFROM #a torder by Invoice_no, Seq==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
ashley.sql
Constraint Violating Yak Guru
299 Posts |
Posted - 2008-04-18 : 16:58:42
|
| NR your last post does not work as well just try to execute and seeit return only 2 rows-----------------------------------------------------------------------------------------------Ashley Rhodes |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2008-04-18 : 20:35:15
|
| That'll be because there are only two rows in #invoice.Try changing union to union all.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
ranganath
Posting Yak Master
209 Posts |
Posted - 2008-04-19 : 00:39:54
|
| Hi,Try with this create table#invoice (invoice_no varchar(5), invoice_amount int, Seq Int)INSERT INTO #invoiceSELECT 'A', 100, 0 UNIONSELECT 'A', 200, 0 UNIONSELECT 'A', 300, 0 UNIONSELECT 'B', 400, 0 UNIONSELECT 'B', 500, 0 UNIONSELECT 'B', 600, 0 UNIONSELECT 'B', 700, 0 --SELECT * FROM @TUPDATE T1SET SEQ = (SELECT COUNT(*) FROM #invoice T WHERE T.invoice_no = T1.invoice_no AND T.invoice_amount <= T1.invoice_amount ) FROM #invoice T1SELECT * fROM #invoiceDrop Table #invoice |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-04-19 : 02:53:05
|
| If you want to show the dagta in Reports, you can easily make use RecordNumber and reset in each groupMadhivananFailing to plan is Planning to fail |
 |
|
|
|