SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Insert records into one table form other table


Insert records into one table form other table

Author
Message
komal145
komal145
Right there with Babe
Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)

Group: General Forum Members
Points: 723 Visits: 1049
Hi friends,
I have a situation where i need tocreate table and insert records into that table from other table.
New table :
Create table A
(
AId int,
ACode varchar(30),
ATimeStamp datetime,
)

Step 1: Need to load all records all the Acode with 'Runquote'
Insert into tableA
SELECT Aid
, ACode
,MIN(ATimestamp) ATimestamp
FROM dbo.ALog al
WHERE AId > 0 AND ACode='RunQuote'
GROUP BY
Aid, ACode

Step 2: Need to load all AId's that are not "runquote" but with acode 'Generatedocument'

Insert into TableA
SELECT Aid
, ACode
,MIN(ATimestamp) ATimestamp
FROM dbo.ALog al
WHERE AId > 0 AND ACode='Generatedocument'
and AID not in (select Aid from tableA)
GROUP BY
Aid, ACode



This is what I am doing right now.Is there any other way that I can do this???? In stored procedure??? In a single load using TSQL??


Thanks for Help.
Peter Brinkhaus
Peter Brinkhaus
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2066 Visits: 7369
To do it in a single load, you can use a query like


;WITH OrderedALog AS
(
SELECT
Aid, ACode, ATimestamp,
ROW_NUMBER() OVER (PARTITION BY Aid ORDER BY ACode DESC, ATimestamp) RowNum
FROM
dbo.ALog al
WHERE
AId > 0 AND ACode IN ('Generatedocument', 'RunQuote')
)
INSERT INTO
dbo.A
SELECT
Aid, ACode, ATimestamp
FROM
OrderedALog
WHERE
RowNum = 1


ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17066 Visits: 19557
This should replace both original queries:

INSERT INTO tableA (Aid, ACode, ATimestamp)
SELECT
Aid
,ACode
,MIN(ATimestamp) ATimestamp
FROM dbo.ALog al
WHERE AId > 0
AND ACode IN ('RunQuote', 'Generatedocument')
GROUP BY ACode, Aid
ORDER BY ACode, Aid



- always TEST.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
komal145
komal145
Right there with Babe
Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)Right there with Babe (723 reputation)

Group: General Forum Members
Points: 723 Visits: 1049
Issue is there is Aid with both "runquote" and "generatedocument" , need to load Aid with "Runquote",
and then load the Aid's which are not "runquote" but with "Generatedocumnet"

example: AID : 12345 has Acode as "runquote" and "Generatedocument" but need to have only one Aid:12345 with "runquote" in my new tableA.
rVadim
rVadim
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1133 Visits: 2306
komal145 (8/15/2012)
Issue is there is Aid with both "runquote" and "generatedocument" , need to load Aid with "Runquote",
and then load the Aid's which are not "runquote" but with "Generatedocumnet"

example: AID : 12345 has Acode as "runquote" and "Generatedocument" but need to have only one Aid:12345 with "runquote" in my new tableA.


Peter's code should do this. It will mark rows with ROW_NUMBER function and then load only those where RowNum = 1:


12345 'runquote ' 1
12345 'Generatedocument' 2
23456 'runquote ' 1
23456 'Generatedocument' 2
34567 'Generatedocument' 1


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search