|
I have a question about fixing a non-contiguous data that I've: Let's say I have this table: Table A: Now for name "ABC" and "XYZ" I've non-contiguous number. I need to find the number that are missing for those two name and make them contiguous So that Any idea
(comments are locked)
|
|
@ThomasRushton's answer is correct (+1) but can be refactored down to a more concise version: I have wrapped the original data into a CTE This saves a join and just resorts the data, so should be a little better on resources (don't forget to test though!) Nice. I'd forgotten that trick of updating a CTE...
Aug 13 '12 at 08:15 AM
ThomasRushton ♦
(comments are locked)
|
|
Try this: What we're doing is building a temporary table "ncdnew" that uses the Row_Number function, and basing the update of your original table with the newvalue created in that temporary table. Hope that helps. Thanks for the quick reply, but what If my table as more column, should I create the @NonContigData table with all the column that's in my table or I can create a subset. Same for the rows, If I have more rows+colums should something like this work: insert into @NonContigData values select * from table A or insert into @NonContigData values select * from table A where Name = 'ABC' if I want to do it one Name at a time Once we update: @NonContigData Do we insert the new value to the main table
Aug 10 '12 at 08:51 PM
thimi3
That @NonContigData is the table I've used to store the data you provided above. Replace that with your table name. If you only want to update a subset of your data, then yes, put a WHERE clause on your update statement. If you have more columns, you only need to deal with enough to uniquely identify each row - the INNER JOIN's SELECT statement and the ON clause are what you need to look at.
Aug 10 '12 at 09:01 PM
ThomasRushton ♦
Okay, I got mislead by that piece. Since I already have the table therefore I need to look at the update section of the script. Is it possible to have an output before updating, like a component that will output the missing int number
Aug 10 '12 at 09:06 PM
thimi3
Can you also explained this section: (ROW_NUMBER() OVER (PARTITION BY Name ORDER BY Value))
Aug 10 '12 at 09:12 PM
thimi3
What, you want me to show my working? ;-) Before I wrote the UPDATE statement, I started with a SELECT statement along these lines: That shows all the data in the table, as well as the steps that go along to making the NewValue.
Aug 10 '12 at 09:15 PM
ThomasRushton ♦
(comments are locked)
|

