If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > Data table design question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-10-07, 21:46
QTpi QTpi is offline
Registered User
 
Join Date: Jun 2007
Posts: 5
Red face Data table design question

Hi. This seems simple, and yet I can think of no elegant way to accomplish this simple modeling. Basically, I have a set of numbers that together can make up a group. There could be a single number in the group, or many numbers in the group. A particular number may appear in multiple groups.

My system will present a set of numbers which I need to go through in order to determine which group is involved. I wanted to do something normalized where each of my numbers is in a record, and these number records relate to the group record or records that they fall in, but the logic doesn't seem to work.

All I can come up with instead is to create a single record for each group that has a column that concatenates all of its member numbers, perhaps separated by a special character, and then match on that.

Any better way to do this?
Many thanks!
Reply With Quote
  #2 (permalink)  
Old 06-10-07, 21:57
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Can you explain the problem in terms of the real-world problem that you are trying to solve (for example finding which service person has worked on a series of customers that are having the same problems after service)? I think I know what you want from your explanation, but I'm not nearly certain enough that I'm willing to try to answer your question until I understand it better.

-PatP
Reply With Quote
  #3 (permalink)  
Old 06-10-07, 22:11
QTpi QTpi is offline
Registered User
 
Join Date: Jun 2007
Posts: 5
Hi Pat.

You could liken it to people who work in teams. The Team Members may belong to one or many Teams. You are presented with a list of Team Member names that worked on Monday and you want to find out which Team (group) name it was that worked. The sizes of teams are not all ths same.

I had wanted to have one record in my Team Member table per member, and then relate those records to the appropriate Group or Groups.

I hope this makes things clearer.
Thanks!
Reply With Quote
  #4 (permalink)  
Old 06-10-07, 22:14
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
it's simple -- you need a third table for the number-group relationship

do a search on many-to-many relationship or association table
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 06-13-07, 22:16
QTpi QTpi is offline
Registered User
 
Join Date: Jun 2007
Posts: 5
I should have elaborated--I understand about the association table, but I don't seem to be able to construct a proper query using that model. For example, let's say I have Team Members and Teams, and I make some tables:

Member:
MemberID
MemberName

Team:
TeamID
TeamName

Associator:
TeamID
MemberID

Now I have some people, allocated thusly--
Team1:
John
Mary

Team2:
John
Mary
Bob

Team3:
John
Billy

How can I construct the query so that if I am presented with the names "John" and "Mary", I can retrieve only the TeamName for Team1? Anything I'm coming up with is getting me Team2 as well. Or if I restrict based on count to fix that problem, then if I change my search criteria to include people not on the same team such as John and Jim, I'll end up retrieving Team3.

Thanks!
Reply With Quote
  #6 (permalink)  
Old 06-13-07, 22:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
maybe i don't understand the problem, but it does indeed look like john and mary are on team 2 as well as team 1
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 06-14-07, 08:17
QTpi QTpi is offline
Registered User
 
Join Date: Jun 2007
Posts: 5
You are correct. However, in my case, I need to return the name of the team where only John and Mary exist. I do not want to retrieve teams that have other members as well.

Thanks.
Reply With Quote
  #8 (permalink)  
Old 06-14-07, 08:29
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
ah, i get it
Code:
select t.TeamName
  from Team as t
inner
  join (
       select a.TeamID
         from Member as m
       inner
         join Associator as a
           on a.MemberID = m.MemberID
       group
           by a.TeamID
       having count(
               case when m.MemberName 
                        in ('John','Mary')
                    then 'ok' end
                   ) = 2
          and count(
               case when m.MemberName 
                    not in ('John','Mary')
                    then 'not ok' end
                   ) = 0
       ) as d
    on d.TeamID = t.TeamID
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 06-14-07, 08:51
QTpi QTpi is offline
Registered User
 
Join Date: Jun 2007
Posts: 5
Smile

Perfect! And I learned some new SQL bits as well--never used that 'case when' before. Thanks so much!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On