MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 28th, 2013, 01:38 PM
Funes Funes is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 4 Funes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 11 sec
Reputation Power: 0
Complex SQL update query

Hello all - I am trying to write an update query that is much more complicated than anything I've done and could use some help.

I have a primary table (TBL1) with the following relevant fields:

ID
Status
DupeID
OwnerID
OtherOwners

I have an additional table (TBL2) where the owners are defined:

OwnerID
Name

I'm trying to build a query that will do the following:

For all of the records in TBL1 where 'Status' = 'Primary', update the OtherOwners field with the 'Name' values (multiple values separated by a semicolon) of ALL of the records where
DupeID of the other records is equal to the DupeID of the 'Primary' record.

So - if I had the following values:

TBL1:

ID Status DupeID OwnerID OtherOwners
1 Primary 234 3
2 Secondary 234 1
3 Secondary 234 2
4 Secondary 567 2

TBL2:

OwnerID Name
1 Joe
2 Jane
3 Jim


I would want the query to result in an updated TBL1 that looks like this:

ID Status DupeID OwnerID OtherOwners
1 Primary 234 3 Joe; Jane
2 Secondary 234 1
3 Secondary 234 2
4 Secondary 567 2

As an added complication, I only want the value 'Joe' to appear once in the DupeOwners column regardless of how many 'secondary' records share the DupeID value of the primary record.

After banging my head against the wall for quite awhile now, I think this is beyond my capabilities at the moment. Can anyone help?

Thanks.

Reply With Quote
  #2  
Old January 28th, 2013, 02:20 PM
MrFujin's Avatar
MrFujin MrFujin is offline
Lord of the Dance
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Oct 2003
Posts: 3,129 MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 22 h 8 m 38 sec
Reputation Power: 1736
Besides it is complex, it will be wrong to do this.
You should never store several values in one field.

Can you explain why you want to do this?

You will be able to get the secondary names in a select statement, using JOIN and maybe also GROUP BY.

Reply With Quote
  #3  
Old January 28th, 2013, 02:54 PM
Funes Funes is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 4 Funes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 11 sec
Reputation Power: 0
I agree. Unfortunately these are just a few fields in a very large database that needs to be maintained as-is for downstream processing. Eventually this data needs to be exported from the DB along with other fielded data in a large CSV file. The final export is application-driven and I don't have control of it, so ultimately the data needs to get populated back into this DB so downstream users have it available.

If it's easier to port the results to a CSV file that contains just the ID and the 'RelatedOwners' fields I can always overlay it back into the DB in a separate step, but I don't imagine that would make it any easier.

Reply With Quote
  #4  
Old January 28th, 2013, 03:37 PM
MrFujin's Avatar
MrFujin MrFujin is offline
Lord of the Dance
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Oct 2003
Posts: 3,129 MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 22 h 8 m 38 sec
Reputation Power: 1736
What I read, is that you have a application that create a CSV file and then put it back into the same DB to be accessible by "downstream" user?
I think/hope I read it wrong, as it doesn't make any sense to me.

Can see it is more complex than I even thought. Forgot that GROUP_CONCAT only works with MySql.

Reply With Quote
  #5  
Old January 28th, 2013, 03:51 PM
Funes Funes is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 4 Funes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 11 sec
Reputation Power: 0
Thanks for the comments. The users perform their own ad-hoc exports of fielded data and I can't change the export process, so I need to actually put that data into a single field so it's available to them.

Reply With Quote
  #6  
Old January 28th, 2013, 04:07 PM
MrFujin's Avatar
MrFujin MrFujin is offline
Lord of the Dance
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Oct 2003
Posts: 3,129 MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 22 h 8 m 38 sec
Reputation Power: 1736
First, I must admit I am not expert in MS SQL, but please see if this will return the kind of information you are looking for (have not been able to test it):
sql Code:
Original - sql Code
  1.  
  2. SELECT  TBL1.ID
  3.     ,   TBL1.OwnerID
  4.     ,   RelatedOtherOwner = STUFF((SELECT '; '+name FROM TBL2 WHERE TBL2.STATUS = 'Secondary' AND TBL2.DupeID=TBL1.DupeID AND  FOR XML PATH('')) , 1 , 1 , '' )
  5. FROM TBL1
  6. WHERE TBL1.STATUS = 'Primary'

Last edited by MrFujin : January 28th, 2013 at 04:14 PM.

Reply With Quote
  #7  
Old January 29th, 2013, 08:30 AM
Funes Funes is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 4 Funes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 11 sec
Reputation Power: 0
This is not quite right, as some of the fields are associated with the wrong tables (i.e. 'Status' is a tbl1 field not tbl2, and tbl2 does not have a DupeID field). But I was unaware of the 'STUFF' statement, which looks like it could be helpful in doing what I need. I'm going to try to simplify the query and play around with the STUFF statement to see if I can get close and will post back. Thanks.

Reply With Quote
  #8  
Old January 29th, 2013, 01:11 PM
MrFujin's Avatar
MrFujin MrFujin is offline
Lord of the Dance
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Oct 2003
Posts: 3,129 MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 22 h 8 m 38 sec
Reputation Power: 1736
Can see I did managed to messed up when I tried to simplify it.

My idea was to make a self-join, to get the name with the primary part:
Code:
TBL3
ID	Status		DupeID	OwnerID	OtherOwners	Name
1	Primary		234	   3			Joe
1	Primary		234	   3			Jane

sql Code:
Original - sql Code
  1.  
  2. SELECT T1a.*, T1b.name
  3. FROM TBL1 T1a
  4. INNER JOIN TBL1 T1b ON T1b.DupeID = T1a.DupeID
  5. WHERE T1a.STATUS = 'primary' AND T1b.STATUS = 'Secondary'


And then from this result, try to make a SELECT around this using STUFF, which should simulate/replace the GROUP_CONCAT function from MySql.
As you said, it is complex.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Complex SQL update query

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap