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 April 9th, 2012, 08:24 AM
Patch1897 Patch1897 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 3 Patch1897 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 19 sec
Reputation Power: 0
Duplicates

Disclaimer - My knowledge of MSSQL is limited. So I apologize in advance if this is a 'dumb' question.

Purpose of Query - Gathering information from a multi-table database from a program our company uses and depositing the results into our web database so we can display some of the information.

Issue - Duplicate information. Part of the issue is that there are multiple instances of people in the database, so there are results duplicated that way... and the other duplicates come from the join. I need to combine the results so that I deposit just one result with the same Name or Code (ID). The rest of the information I would like to combine so that I don't lose any information that may be needed.

Here is query I have:
SELECT
people.Code AS [athlete-id],
people.Name AS [athlete-name],
people.DOB AS [DOB],
people.Division AS [agency-id],
certifs.[Expiration date] AS [expiration-date],
groups.Name AS [agency-name],
address.Addresses AS [address],
address.City AS [city],
address.State AS [state],
case when tags.Field = 'ABJD86GHKXXQWA9Q'
then tags.Value end AS [restrictions],
case when tags.Field = 'VH2C78N9A15S059T'
then tags.Value end AS [comments]

FROM people LEFT OUTER JOIN
certifs ON people.Code = certifs.Owner LEFT OUTER JOIN
groups ON people.Division = groups.Code LEFT OUTER JOIN
address ON people.Code = address.Owner LEFT OUTER JOIN
tags ON people.Code = tags.Owner
ORDER BY [agency-name], [athlete-name]

Let me know of any questions you may have. I appreciate the help.

Reply With Quote
  #2  
Old April 9th, 2012, 10:14 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,371 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 28 m 35 sec
Reputation Power: 4140
Quote:
Originally Posted by Patch1897
...and the other duplicates come from the join.
this is what's going to drive you nuts

based on your column names, i would guess the cardinality of the relationships as follows...
  • a person can have multiple certificates
  • a person can have multiple addresses
  • a person can have multiple tags
this being the case, when you join all these other tables at the same time, you'll get cross-join effects

for example, if a person has 3 certifs, 2 addresses, and 4 tags, your joins will produce 24 result rows

i'm afraid the best that you can do is completely disassemble your query, return only one row per person from the person table (and any other tables, like the groups table, for which a person has only one row), and then retrieve the one-to-many data in the other tables with a separate query for each
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old April 9th, 2012, 10:42 AM
Patch1897 Patch1897 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 3 Patch1897 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 19 sec
Reputation Power: 0
Quote:
i'm afraid the best that you can do is completely disassemble your query, return only one row per person from the person table (and any other tables, like the groups table, for which a person has only one row), and then retrieve the one-to-many data in the other tables with a separate query for each


This is where my lack of sql knowledge kills me. Will I be able to make multiple select statements to gather all of the different data, and then combine it into one table in one statement?

The reason I ask if it can be done in one statement is because this statement will be run in a cron job each morning to redeposit the queried information of the multiple tables into one table on the web database.

I really appreciate the help.. this has been driving me crazy.

Reply With Quote
  #4  
Old April 9th, 2012, 12:00 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,371 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 28 m 35 sec
Reputation Power: 4140
Quote:
Originally Posted by Patch1897
...to redeposit the queried information of the multiple tables into one table on the web database.
in that case, perhaps you could share the design of that table?

Reply With Quote
  #5  
Old April 10th, 2012, 08:26 AM
Patch1897 Patch1897 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 3 Patch1897 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 19 sec
Reputation Power: 0
Quote:
Originally Posted by r937
in that case, perhaps you could share the design of that table?


I really appreciate the help.

The design of that table is exactly the output of the select that I have above.

[athlete-id], [athlete-name], [DOB], [agency-id],
[expiration-date], [agency-name], [address],
[city], [state], [restrictions], [comments] Are the column heads.

Reply With Quote
  #6  
Old April 10th, 2012, 10:02 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,371 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 28 m 35 sec
Reputation Power: 4140
Quote:
Originally Posted by Patch1897
The design of that table is exactly the output of the select that I have above.
and how exactly do you want multiple expiration dates stored? what about multiple addresses? multiple tags?

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Duplicates

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