Author |
Topic |
pamyral_279
Posting Yak Master
143 Posts |
Posted - 2014-08-11 : 06:22:29
|
Hi all ,I have a problem as , please give me advices ! Thank in advance !I have seven records in table :Id--Name-AId-PriCate-CateId-Ord5---A----320---0-------3----16---E----300---1-------3----27---G----360---1-------6----98---M----370---1-------3----39---L----330---0------12----810---M----361---1-------3----411---Z----362---1-------3----5Id is increase value,My problem : When i insert any record in CateId =3 with first position:With first 100 records I want to update all other records with Ord field to 100,Other records I will set Ord=101 , Output will display as :5---A----320---0-------3----16---E----300---1-------3----28---M----370---1-------3----37---M----361---1-------3----48---Z----342---1-------3----59---Z----862---1-------3----..10---Z----362---1-------3----9911---Z----122---1-------3----10012---Z----562---1-------3----10113---Z----752---1-------3----10114---Z----322---1-------3----10115---Z----372---1-------3----101...How can i do ?Thank you very much ! |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2014-08-11 : 07:39:23
|
Your question is not very clear to me.what do you mean by "insert any record in CateId =3 with first position" ?quote: With first 100 records I want to update all other records with Ord field to 100,Other records I will set Ord=101
Where is the first 100 records coming from ? You said you only have 7 records in that table. KH[spoiler]Time is always against us[/spoiler] |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2014-08-11 : 08:14:53
|
UPDATE dbo.Table1 SET Ord = Ord + 1 WHERE Cat = 3Then you insert your new row with a value of 1 for Ord. Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
 |
|
pamyral_279
Posting Yak Master
143 Posts |
Posted - 2014-08-11 : 21:55:49
|
quote: Originally posted by SwePeso UPDATE dbo.Table1 SET Ord = Ord + 1 WHERE Cat = 3Then you insert your new row with a value of 1 for Ord. Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Thank you KHtan and Swe so much !For your solution, Ord will increase with no limit ! I want to bound Ord in [1 to 100] , other records will be Ord 101 !Have you other ways ? Thank you so much ! |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2014-08-12 : 02:04:01
|
UPDATE dbo.Table1 SET Ord = Ord + 1 WHERE Cat = 3DELETE FROM dbo.Table1 WHERE Ord >= 101 AND Cat = 3And now insert the new row. Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
 |
|
pamyral_279
Posting Yak Master
143 Posts |
Posted - 2014-08-12 : 03:13:46
|
Thank you swe !But i need to keep old records , just change Ord to 101 !Output :5---A----320---0-------3----16---E----300---1-------3----28---M----370---1-------3----37---M----361---1-------3----48---Z----342---1-------3----59---Z----862---1-------3----..10---Z----362---1-------3----9911---Z----122---1-------3----10012---Z----562---1-------3----10113---Z----752---1-------3----10114---Z----322---1-------3----10115---Z----372---1-------3----101 |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2014-08-12 : 05:11:15
|
[code]UPDATE fSET Ord = CASE WHEN rn >= 101 THEN 101 ELSE 1 + rn ENDFROM ( SELECT Ord, ROW_NUMBER() OVER (ORDER BY Ord) AS rn FROM dbo.Table1 ) AS f;[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
 |
|
pamyral_279
Posting Yak Master
143 Posts |
Posted - 2014-08-12 : 05:51:57
|
quote: Originally posted by SwePeso
UPDATE fSET Ord = CASE WHEN rn >= 101 THEN 101 ELSE 1 + rn ENDFROM ( SELECT Ord, ROW_NUMBER() OVER (ORDER BY Ord) AS rn FROM dbo.Table1 ) AS f; Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Thank swe million ! Seem that working fine ! I will check more !Can i insert new record with any position in [1 .. 100] ? (Not first position in this way) |
 |
|
pamyral_279
Posting Yak Master
143 Posts |
Posted - 2014-08-12 : 05:54:09
|
Thank swe million ! Seem that working fine ! I will check more !Can i insert new record with any position in [1 .. 100] ? (Not first position in this way) |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2014-08-12 : 06:50:25
|
Yes. In case of duplicate values for Ord, just add a second sort column in the windowed function.And also, add a WHERE clause of Cat = 3 for the derived table f.First insert, and thenUPDATE fSET Ord = CASE WHEN rn >= 101 THEN 101 ELSE rn ENDFROM ( SELECT Ord, ROW_NUMBER() OVER (ORDER BY Ord, Name) AS rn FROM dbo.Table1 WHERE Cat = 3 ) AS f; Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
 |
|
pamyral_279
Posting Yak Master
143 Posts |
Posted - 2014-08-14 : 04:13:56
|
quote: Originally posted by pamyral_279
quote: Originally posted by SwePeso
UPDATE fSET Ord = CASE WHEN rn >= 101 THEN 101 ELSE 1 + rn ENDFROM ( SELECT Ord, ROW_NUMBER() OVER (ORDER BY Ord) AS rn FROM dbo.Table1 ) AS f; Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Thank swe million ! Seem that working fine ! I will check more !Can i insert new record with any position in [1 .. 100] ? (Not first position in this way)
What happen if table has many rows (ex. 10000 records) ? Is this best way ? Thank swe so much :) |
 |
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2014-08-14 : 08:04:13
|
10000 rows is nothing. SwePeso's solution is probably the best you can do in any case. |
 |
|
|