Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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:53 AM
Points: 12,890, Visits: 31,851
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
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: Today @ 6:41 AM
Points: 23,081, Visits: 31,621
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, July 29, 2014 1:21 PM
Points: 809, Visits: 1,160
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:53 AM
Points: 12,890, Visits: 31,851
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

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1450312
Posted Wednesday, May 8, 2013 1:16 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, July 18, 2014 1:33 AM
Points: 19, Visits: 45
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