| Author |
Topic |
|
gurusamy.senthil
Yak Posting Veteran
65 Posts |
Posted - 2006-06-20 : 06:26:14
|
| Category ClassID SeqNbrSQUARE TUBING 000001 1 RECTANGULAR TUBING 000542 2 TUBING SPECIALS 000543 3TUBING SPECIALS RUSTY 001042 5PLASTIC PLUGS 000544 7If u see the seqnbr falls to 5 for category Tubing specials rusty and 7 for Plastic Plugs.Actually 4 have to be present in place of 5 and 5 in place of 7 in the seqnbr.For that have to write stored procedures We have to result the seqnbr in asc order not random if we use the select stmt.Please help the logic or in code that how to write procedures to get result for this. |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-20 : 06:32:47
|
Here's one method...  --datadeclare @t table (Category varchar(30), ClassID varchar(10), SeqNbr int)insert @t select 'SQUARE TUBING', '000001', 1 union all select 'RECTANGULAR TUBING', '000542', 2 union all select 'TUBING SPECIALS', '000543', 3union all select 'TUBING SPECIALS RUSTY', '001042', 5union all select 'PLASTIC PLUGS', '000544', 7--calculationupdate a set SeqNbr = (select count(*) from @t where SeqNbr <= a.SeqNbr) from @t aselect * from @t/*Category ClassID SeqNbr ------------------------------ ---------- ----------- SQUARE TUBING 000001 1RECTANGULAR TUBING 000542 2TUBING SPECIALS 000543 3TUBING SPECIALS RUSTY 001042 4PLASTIC PLUGS 000544 5*/ Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-06-20 : 06:36:18
|
| Where do you want to show the data?If you use front end application, do the numbering thereMadhivananFailing to plan is Planning to fail |
 |
|
|
gurusamy.senthil
Yak Posting Veteran
65 Posts |
Posted - 2006-06-20 : 07:58:02
|
| Table is available already with 500 rows. Rows consists of unordered seqnbr. We could touch or update category only thing is we have to update the seqnbr starting from 1 till end in asc order.Maddy this is an already entered through front end application.But now the client want the seqnbr to be in order without entering data once again. How to proceed |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-20 : 08:07:29
|
| gurusamy.senthil - did you see my post? The code there updates the seqnbr starting from 1 according to the order of the existing seqnbr data. Is that not what you wanted?Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-06-20 : 09:15:43
|
| >>But now the client want the seqnbr to be in orderHow does the client view data, thru application or from file where you export data?If you dont use any front end application, try the method suggested by Ryan. But this is not advisable if the table has thousands of rowsMadhivananFailing to plan is Planning to fail |
 |
|
|
gurusamy.senthil
Yak Posting Veteran
65 Posts |
Posted - 2006-06-20 : 09:27:17
|
| Ok maddy.I will try and come back. |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2006-06-20 : 11:50:13
|
| http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=55210&SearchTerms=HomeworkHelp us help YOU!Read this blog entry for more details: http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspx*need more coffee*SELECT * FROM Users WHERE CLUE > 0(0 row(s) affected) |
 |
|
|
gurusamy.senthil
Yak Posting Veteran
65 Posts |
Posted - 2006-06-21 : 01:03:30
|
| Hi ryan, You are joining all the values by using Union. But there are large amount of data, So we cannot use Union all operator for everyone.The below code is what I tried.IF EXISTS (SELECT * FROM SYSOBJECTS WHERE id = object_id('dbo.xCattmp') and sysstat & 0xf = 4) DROP PROCEDURE xCattmpGOCREATE PROCEDURE xCattmp AS--DECLARE @cat varchar(10)DECLARE @Var1 smallintSET @Var1 = '1'DECLARE currxCat CURSOR FOR SELECT * from xcategorytmp order by seqnbrOPEN currxCatFETCH NEXT FROM currxCat --into @cat,@var1WHILE @@FETCH_STATUS = 0BEGIN UPDATExcategorytmp SET seqnbr = @var1 FROM xcategorytmp SET @Var1 = @Var1 + '1' FETCH NEXT FROM currxCat ENDCLOSE currxCatDEALLOCATE currxCatGOThe Update stmt I used inside the While loop will be updating all the record. Actually I want to update the seqnbr line by line.I used Cursors to fetch record line by line. Will anyone seniors can help me to come out this problem. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-06-21 : 03:07:46
|
| Why cant you simulate the logic that Ryan suggested?MadhivananFailing to plan is Planning to fail |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-21 : 05:03:27
|
| gurusamy.senthil - I've only used union to prepare the data. You don't need to worry about that because you already have the data in your table. You just need to use the calculation bit (the 1-line update statement)...Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
gurusamy.senthil
Yak Posting Veteran
65 Posts |
Posted - 2006-06-22 : 03:18:42
|
| IF EXISTS (SELECT * FROM SYSOBJECTS WHERE id = object_id('dbo.xCat_Order_SeqNbr') and sysstat & 0xf = 4) DROP PROCEDURE xCat_Order_SeqNbrGOCREATE PROCEDURE xCat_Order_SeqNbrASDECLARE @SeqNbr intDECLARE @ClassID varchar(6)SET @SeqNbr = 1DECLARE xCategory CURSOR FOR SELECT ClassID from xcategory order by SeqnbrOPEN xCategoryFETCH NEXT FROM xCategory INTO @ClassIDWHILE @@fetch_status = 0BEGIN UPDATE xcat SET xcat.SeqNbr = @SeqNbr FROM xcategory xcat WHERE xcat.ClassID = @ClassID SET @SeqNbr = @SeqNbr + 1 FETCH NEXT FROM xCategory INTO @ClassIDENDCLOSE xCategoryDEALLOCATE xCategoryGOThanks ryan and maddy. The above code gives me the result, but the thing is I have used Cursors. Any other better solution instead of using the cursors to get the result. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-22 : 03:21:24
|
"The above code gives me the result, but the thing is I have used Cursors. Any other better solution instead of using the cursors to get the result."Did't Ryan has given you the solution ?quote: update a set SeqNbr = (select count(*) from @t where SeqNbr <= a.SeqNbr) from @t a
KH |
 |
|
|
gurusamy.senthil
Yak Posting Veteran
65 Posts |
Posted - 2006-06-22 : 03:26:41
|
| It will give the result but the scenerio is suppose if there is repetated seqnbr in an table Will that code works?.I tried with and examples given by ryan by giving repeated seqnbr, it is not working |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-22 : 03:32:42
|
Can you post the sample data of the scenario that you describe ? KH |
 |
|
|
gurusamy.senthil
Yak Posting Veteran
65 Posts |
Posted - 2006-06-22 : 03:36:34
|
| khtan I tried with the sample data given my ryan and changed the some number for repetation. And I didn't understand how the incrementing seqnbr processing. Will u please help me?for eg:-declare @t table (Category varchar(30), ClassID varchar(10), SeqNbr int)insert @t select 'SQUARE TUBING', '000001', 1 union all select 'RECTANGULAR TUBING', '000542', 2 union all select 'TUBING SPECIALS', '000543', 3union all select 'TUBING SPECIALS RUSTY', '001042', 3union all select 'PLASTIC PLUGS', '000544', 7--calculationupdate a set SeqNbr = (select count(*) from @t where SeqNbr <= a.SeqNbr) from @t aselect * from @t |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-22 : 06:11:14
|
gurusamy.senthil - A small modification will deal with duplicate SeqNbrs. Do you have any other issues with this approach?--datadeclare @t table (Category varchar(30), ClassID varchar(10), SeqNbr int)insert @t select 'SQUARE TUBING', '000001', 1union all select 'RECTANGULAR TUBING', '000542', 3union all select 'TUBING SPECIALS', '000543', 3union all select 'TUBING SPECIALS RUSTY', '001042', 5union all select 'PLASTIC PLUGS', '000544', 7--calculationupdate a set SeqNbr = (select count(*) from @t where SeqNbr < a.SeqNbr or (SeqNbr = a.SeqNbr and ClassID <= a.ClassID))from @t aselect * from @t/*Category ClassID SeqNbr ------------------------------ ---------- ----------- SQUARE TUBING 000001 1RECTANGULAR TUBING 000542 2TUBING SPECIALS 000543 3TUBING SPECIALS RUSTY 001042 4PLASTIC PLUGS 000544 5*/ Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
gurusamy.senthil
Yak Posting Veteran
65 Posts |
Posted - 2006-06-22 : 06:34:52
|
| Thanks ryan it is working, but tell me What is the logic behind this code. How the seqnbr is incrementing automatically without setting any prior declaration. What is happening inside the code if u don't mine please explain it. I will be greatfull to you all my friends. |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-22 : 06:45:42
|
Great! Okay - I'll try to explain....For each row in the table, we're counting how many rows have a SeqNbr less then that row (or have the same SeqNbr and a ClassId less than or equal to the other rows). So take, for example, our '3rd' row (with ClassID 000543 and SeqNbr 3):* # rows that have a SeqNbr less then that row = 1 (our '1st' row)* # rows that have the same SeqNbr and a ClassId less than or equal to the other rows = 2 (our '2nd' and our '3rd' row)So, the total for our '3rd' row is 3 (1 + 2). Try this for any row and you'll see that it always gives you what you need.I hope that helps Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
gurusamy.senthil
Yak Posting Veteran
65 Posts |
Posted - 2006-06-22 : 06:48:31
|
| Sorry it will not work if the seqnbr are repeated in random orders.For eg:--datadeclare @t table (Category varchar(30), ClassID varchar(10), SeqNbr int)insert @t select 'SQUARE TUBING', '000001', 2union all select 'RECTANGULAR TUBING', '000542', 2union all select 'TUBING SPECIALS', '000543', 3union all select 'TUBING SPECIALS RUSTY', '001042', 3union all select 'PLASTIC PLUGS', '000544', 2--calculationupdate a set SeqNbr = (select count(*) from @t where SeqNbr < a.SeqNbr or (SeqNbr = a.SeqNbr and ClassID <= a.ClassID))from @t aselect * from @t. |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-06-22 : 07:01:16
|
| Eh? That ranks the rows 1-5 with no duplicates - just ask you asked and as I'd expect. Why do you think this does not work?You must have some criteria to determine the ranking based on the data alone. Remember that sql server does not 'store' the data in any particular order.Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
Next Page
|