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


UPDATE SQL Query Help !


UPDATE SQL Query Help !

Author
Message
sqldba20
sqldba20
Valued Member
Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)

Group: General Forum Members
Points: 55 Visits: 445
Folks:

I need help with a UPDATE SQL Statement.

CREATE TABLE #TEST1
(
AID INT ,
EmailID VARCHAR (200)
);

INSERT INTO #TEST1
VALUES (100, 'jack@microsoft.com');

INSERT INTO #TEST1
VALUES (100, 'david@microsoft.com');

INSERT INTO #TEST1
VALUES (100, 'paul@microsoft.com');

INSERT INTO #TEST1
VALUES (200, 'amy@microsoft.com');

INSERT INTO #TEST1
VALUES (200, 'bill@microsoft.com');

INSERT INTO #TEST1
VALUES (300, 'chris@microsoft.com');

INSERT INTO #TEST1
VALUES (400, 'katrina@microsoft.com');

INSERT INTO #TEST1
VALUES (500, 'john@microsoft.com');

INSERT INTO #TEST1
VALUES (500, 'jack@microsoft.com');

INSERT INTO #TEST1
VALUES (500, 'david@microsoft.com');

CREATE TABLE #TEST2
(
AID INT ,
EmailIDs VARCHAR (1000) NULL
);

INSERT INTO #TEST2 (AID)
VALUES (100);

INSERT INTO #TEST2 (AID)
VALUES (200);

INSERT INTO #TEST2 (AID)
VALUES (300);

INSERT INTO #TEST2 (AID)
VALUES (400);

INSERT INTO #TEST2 (AID)
VALUES (500);





I would like to update the Email IDs column in #TEST2 with Email ID value from #TEST1 table for matching AID column. Instead of having the email ids on seperate row I would like the email Id for each AID in a single column seperated with semi colon(Wink. Any help on update SQL is appreciated.

OUTPUT:

AID EmailIDs
100 jack@microsoft.com; david@microsoft.com; paul@microsoft.com
200 amy@microsoft.com; bill@microsoft.com
300 chris@microsoft.com
400 katrina@microsoft.com
500 john@microsoft.com; jack@microsoft.com; david@microsoft.com



Thanks !
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16410 Visits: 19081
You shouldn't store lists for delimited values in a single line for a single value. It violates the first normal form.
If you really need this (I suggest to use it only for display or email generation purposes), you could use the method described in this article: http://www.sqlservercentral.com/articles/comma+separated+list/71700/
This is really fast and you don't need to store the values as arrays.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Perry Whittle
Perry Whittle
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19878 Visits: 17242
sorry wrong code

-----------------------------------------------------------------------------------------------------------

"Ya can't make an omelette without breaking just a few eggs" ;-)
sqldba20
sqldba20
Valued Member
Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)

Group: General Forum Members
Points: 55 Visits: 445
It is only for display purpose in reporting because the user doesn't want to see 4 rows for each record if there are 4 email ids for that AID.
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16410 Visits: 19081
sqldba20 (2/20/2014)
It is only for display purpose in reporting because the user doesn't want to see 4 rows for each record if there are 4 email ids for that AID.


So, do you need more help? Or can you find a solution from the article on your own?If you get the solution on your own, you'll learn more than getting the answer already processed.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16164 Visits: 19543
sqldba20 (2/20/2014)
It is only for display purpose in reporting because the user doesn't want to see 4 rows for each record if there are 4 email ids for that AID.


You may find it more efficient to perform this relatively simple operation in the code which generates the #temp table, rather than updating the temp table afterwards. If you're not sure how to do this, post the code and we'll help.

“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
sqldba20
sqldba20
Valued Member
Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)

Group: General Forum Members
Points: 55 Visits: 445
Nevermind...I was able to figure out using the link posted by Luis. Came with this SQL and it works.

    WITH     Ranked (AID, rnk, EmailID)
AS (SELECT AID,
ROW_NUMBER() OVER (PARTITION BY AID ORDER BY AID),
CAST (EmailID AS VARCHAR (8000))
FROM #TEST1),
AnchorRanked (AID, rnk, EmailID)
AS (SELECT AID,
rnk,
EmailID
FROM Ranked
WHERE rnk = 1),
RecurRanked (AID, rnk, EmailID)
AS (SELECT AID,
rnk,
EmailID
FROM AnchorRanked
UNION ALL
SELECT Ranked.AID,
Ranked.rnk,
RecurRanked.EmailID + '; ' + Ranked.EmailID
FROM Ranked
INNER JOIN
RecurRanked
ON Ranked.AID = RecurRanked.AID
AND Ranked.rnk = RecurRanked.rnk + 1)
SELECT AID AS AID,
MAX(EmailID) AS EmaiID
FROM RecurRanked
GROUP BY AID


Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16410 Visits: 19081
sqldba20 (2/20/2014)
Nevermind...I was able to figure out using the link posted by Luis. Came with this SQL and it works.

How on earth did you figure that out from the link I posted? Hehe
This method might kill the performance as it will create more rows than needed.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16164 Visits: 19543
Luis Cazares (2/20/2014)
sqldba20 (2/20/2014)
Nevermind...I was able to figure out using the link posted by Luis. Came with this SQL and it works.

How on earth did you figure that out from the link I posted? Hehe
This method might kill the performance as it will create more rows than needed.


Baffled me too, Luis.
rCTE's aren't the best choice for this - slower than FOR XML PATH, waaaaay overcomplicated.

“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
sqldba20
sqldba20
Valued Member
Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)

Group: General Forum Members
Points: 55 Visits: 445
If this is not appropriate then could you help me a updated SQL ?
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