Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Data transfer while dynamic increment of duplicate values. Expand / Collapse
Author
Message
Posted Tuesday, May 7, 2013 1:11 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 6:21 AM
Points: 14,298, Visits: 37,353
i f he's inserting into a constraint, and one of the new generated values happens to be the same as an existing value, i'd expect an error;
for example, just based on the RO time, data like the item exemplified below would generate two rows withtr he smae time, which might violate the constraint.
2013-05-07 15:09:00.00
2013-05-07 15:09:00.00 <-- this would generate 2013-05-07 15:10:00.00, a duplicate value
2013-05-07 15:10:00.00



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Post #1450305
Posted Tuesday, May 7, 2013 1:18 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 3:52 PM
Points: 22,945, Visits: 36,532
With nothing to do any testing with, give this a try:


WITH BaseData(
Class,
Date_Entered,
PMmiles,
RO,
RoDate,
Time_In,
Ranking
)AS(
SELECT --RO,Time_In, Ranking = DENSE_RANK() OVER (PARTITION BY RO,Time_In ORDER BY NEWID() ASC)
Class,
Date_Entered,
PMmiles,
RO,
RoDate,
Time_In,
Ranking = row_number() over (partition by RO, RoDate, Time_In order by (select null))
FROM
DTAGatHERTZ_RO
)
INSERT INTO ROs
SELECT
Class,
Date_Entered,
PMmiles,
RO,
RoDate,
DATEADD(mi,Ranking - 1, Time_In
from
BaseData
where
RO = '4187';





Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1450309
Posted Tuesday, May 7, 2013 1:23 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, May 10, 2016 9:43 AM
Points: 863, Visits: 1,300
Lowell, he has mentioned in first post that The primary key combination for the destination table is "ROBumber + ROTime" .
Post #1450311
Posted Tuesday, May 7, 2013 1:28 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 6:21 AM
Points: 14,298, Visits: 37,353
Neeraj Dwivedi (5/7/2013)
Lowell, he has mentioned in first post that The primary key combination for the destination table is "ROBumber + ROTime" .


agreed, but if he auto generates an overlapping value ,"ROBumber + ROTime" + 1 in the case of duplicate "ROBumber + ROTime" from the source, he of course could duplicate the value, but against a different row than was incremented.

that was my point...he's not guaranteeing a PK value is unique with the current logic of arbitrarily incrementing a value.


Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Post #1450312
Posted Wednesday, May 8, 2013 1:16 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, January 29, 2016 11:12 AM
Points: 22, Visits: 74
Thanks Neeraj, It was my lack of pre-knowledge on CTE which was preventing me to interpret your suggested solution correctly. I googled about CTE and then came back to apply what you were trying to suggest and it has worked perfectly.

Many thanks for your valuable time and solutions. Thank you all for the suggestons. Very much appreciative and supportive.

Thanks and Regards,
Prasune Verma.
Post #1450429
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse