|
Folks I have this table called TblCombine Ldg, Bnk, Combine, Dupe BQ 55.23 BQ55.23 0 BQ 65.23 BQ65.23 0 BQ 75.23 BQ75.23 0 BQ 85.23 BQ85.23 0 BQ 85.23 BQ85.23 0 BQ 85.23 BQ85.23 0 Int 95.23 Int95.23 0 Int 105.23 Int105.23 0 Int 95.23 Int95.23 0 Int 95.23 Int95.23 0 I join the Ldg and Bnk fields into the Combine field. Expected Result Ldg, Bnk, Combine, Dupe BQ 55.23 BQ55.23 1 BQ 65.23 BQ65.23 1 BQ 75.23 BQ75.23 1 BQ 85.23 BQ85.23 1 BQ 85.23 BQ85.23 2 BQ 85.23 BQ85.23 3 Int 95.23 Int95.23 1 Int 95.23 Int95.23 2 Int 95.23 Int95.23 3 Int 105.23 Int105.23 1 I have done this in VB where in access as I can follow the process but I am lost at the moment in SQL.
(comments are locked)
|
|
The answer would probably be to stage the import into a temporary table with an identity column, calculate/update the dupe count, and then migrate to your final table. If this intermediate processing is acceptable then Melvyn or Matt would probably have the queries you need ready to present to you based on their comments.
(comments are locked)
|
|
Can you post the table definitions? I have a solution which would be reasonably quick, but it needs either a suitable clustered index, or there to be another column in the table which can uniquely identify each entry. If you can edit your question to include that, then I will post you a solution. Lack of a unique row identifier was the same problem I ran into with answering it!
Nov 03 '09 at 08:44 AM
Melvyn Harbour 1 ♦♦
Table TblCombine Design view LDG varchar 50 1 BNK decimal 9 1 COMBINE varchar 50 1 DUPE numeric 9 1 UniqueID varchar 50 1 The unique field is to be a combination of the Combine Field and the Number in the dupe filed after the the dupe field is populated with the process I require. Thanks for looking at the Problem. Regards Pator
Nov 03 '09 at 09:11 AM
pator
I added a comment on Mel's post - he's right in that there needs to be a unique identifier on each row prior to the operation. Is it possible to add an IDENTITY field to the table?
Nov 03 '09 at 11:50 AM
Matt Whitfield ♦♦
(comments are locked)
|
|
Matt , 1 LDG varchar 50 1 I have got a little further with my problem. I use the following SQL to create the data the way I require it. Then I run the the query DECLARE @Test table (Ldg varCHAR(50), Bnk decimal(9,2), Combine varchar(50), Dupe int IDENTITY(1,1)) INSERT INTO @Test (Ldg, Bnk, Combine) SELECT Ldg, Bnk, Ldg+CAST(Bnk as varchar(25)) FROM TblCombine ORDER BY TblCombine.LDG , TblCombine.BNK , TblCombine.COMBINE SELECT rtrim(TblCombine.LDG) as Ldg, TblCombine.BNK, rtrim(TblCombine.LDG) + rtrim(cast(TblCombine.BNK as varchar(50))) as Combine , test.Dupe - TblCombine.Dupe + 1 AS Dupe FROM @Test test INNER JOIN (SELECT Ldg, Bnk, MIN(Dupe) AS Dupe
on Test.Ldg = TblCombine.Ldg where test.Bnk = TblCombine.Bnk Data after SQl runs, ldg bnk combine Dupe BQ 75 BQ75.00 1 AS you can see the number in the Dupe field is based on the combine field, which increases by 1 each time the field is the same. Any further help would be great. Regards Pator @Pator - sorry - I only just saw this now, as it didn't alert for me. I will let Jeff answer though, his solution will be about 40x better than anything I would write.
Mar 08 '10 at 12:54 PM
Matt Whitfield ♦♦
(comments are locked)
|
|
Oh, sorry... I'm not sure how I lost track of this one so very badly. Pator, here's the SQL Server 2000 method I was thinking off. The clustered index, TABLOCKX, and MAXDOP thingies are absolutely essential...
(comments are locked)
|
|
Pator, I realize this is a pretty old post... do you still need an answer on this because, if you do, I have one. I just don't want to take the time to write it up if you don't need it.
(comments are locked)
|
1 2 next page »

