Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help Needed with Query

Posted on 2015-01-20
9
Medium Priority
?
42 Views
Last Modified: 2015-03-30
Hi again Experts,

Given the three tables below (which are joined on SCHEDULE_TABLE.Partcipant = MEMBERS_TABLE.Id AND MEMBERS_TABLE.GroupID = GROUPS_TABLE.Id) I need to develop a query (or perhaps a function) that receives a single ParticipantID as a parameter and returns records containing the names of all participants on a team where one of the participant IDs matches the input parameter).  The query or function would return two Partcipants if GROUPS_TABLE.TeamSize = 2:

      OUTPUT NEEDED
      Interval Date         Team#      ParticipantA           ParticipantB
      
or three participants if GROUPS_TABLE.TeamSize = 3:

      OUTPUT NEEDED
      Interval Date         Team#      ParticipantA           ParticipantB            ParticipantC

...and so on.

Any idea how to accomplish this?  Thanks!




      SCHEDULE_TABLE
      [Id] [int] IDENTITY(1,1) NOT NULL,
      [IntervalStartDate] [datetime] NOT NULL,
      [TeamNumber] [int] NOT NULL,
      [Participant] [int] NOT NULL,
      
      
      MEMBERS_TABLE
      [Id] [int] IDENTITY(1,1) NOT NULL,
      [GroupID] [int] NOT NULL,
      [LastName] [nvarchar](50) NOT NULL,
      [FirstName] [nvarchar](50) NULL,
      [MidNames] [nvarchar](50) NULL,
      

      GROUPS_TABLE
      [Id] [int] IDENTITY(1,1) NOT NULL,
      [TeamSize] [tinyint] NOT NULL,
0
Comment
Question by:penlandt
  • 6
  • 2
9 Comments
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40561441
Do you have any sample data please?
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40561694
What happens if the parameterParticipantID is a member of more than one group of equal size?

I suspect you need not only sample data, but expected results for more then one scenario
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 40561803
do you require the participants as individual columns or can it be a delimited string....?

can we limit the max number of participants in anyway (ie only first 10 to be returned)?

is this a real world question or home/coursework?

the table structures provided , and  output format seems to not allow for the unique identification of the participants (e.g. lots  of potential John smiths could be returned)  could we have some sample data to show how you want that to be tackled.

how big will the tables be for which the solution is desired?
whats the scenario in which  the code will be executed... (one-off, daily, frequently, ...)?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:penlandt
ID: 40562977
I do actually have sample data (SQL script attached that creates the three tables with data).  This is a real world application, currently being used by one client however more are expected soon.  This client always has two person teams however every client has the ability to change the size of their teams at any time.  I also have two test groups that I used when developing the app that creates these schedules.  I've included that data as well so that we have different team sizes to deal with.  Please let me know if you need anything else.

scheduler-partial.sql
0
 
LVL 1

Author Comment

by:penlandt
ID: 40563052
I've also created a spreadsheet (attached) that has the February schedules for my real-world group (Worksheet 1) and an example of what I'd like this query (or function) to produce if I passed participantID=30 to it (Worksheet 2).  Thanks again for your help.

Scheduler-Partial.xlsx
0
 
LVL 1

Author Comment

by:penlandt
ID: 40563083
PortletPaul

"What happens if the parameterParticipantID is a member of more than one group of equal size?"

This is not possible in my database.
0
 
LVL 1

Author Comment

by:penlandt
ID: 40563097
Lowfatspread

"do you require the participants as individual columns or can it be a delimited string....?"

Ideally, individual columns.

"can we limit the max number of participants in anyway (ie only first 10 to be returned)?"

Not really, because the size of the team is completely up to the client.

"the table structures provided , and  output format seems to not allow for the unique identification of the participants (e.g. lots  of potential John smiths could be returned)  could we have some sample data to show how you want that to be tackled."

Hopefully the data I've provided will clear that up.  The Participant in the Schedule table is joined on the MemberID in the Members table, so there is a unique identifier for each participant.  Of course it is possible that could be two or more people in a group with the same name but it would be up to the client to figure out how to tell them apart.  From my point of view I'm just working with MemberIDs.

"how big will the tables be for which the solution is desired?
whats the scenario in which  the code will be executed... (one-off, daily, frequently, ...)?"


The tables could be any size, however in all likelihood this will only be run once a week (or maybe only once a month).
0
 
LVL 1

Accepted Solution

by:
penlandt earned 0 total points
ID: 40563255
I've made it this far on my own at least.  This produces the output I want but only for teams with two members.  Somehow I need to make this allow for as many Participant columns as necessary based on the input parameter @TeamSize [int] (which is not included in this SQL yet).

SELECT 	Schedule.IntervalStartDate AS Date, 
		Schedule.TeamNumber AS Team, 
		ClientGroupMembers.LastName AS ParticipantA, 
		ClientGroupMembers_1.LastName AS ParticipantB
FROM 	Schedule AS Schedule_1 
		INNER JOIN ClientGroupMembers AS ClientGroupMembers_1 ON Schedule_1.Participant = ClientGroupMembers_1.Id 
		INNER JOIN ClientGroupMembers 
		INNER JOIN Schedule 
			ON ClientGroupMembers.Id = Schedule.Participant 
			ON Schedule_1.IntervalStartDate = Schedule.IntervalStartDate 
				AND Schedule_1.TeamNumber = Schedule.TeamNumber 
				AND Schedule_1.Participant <> Schedule.Participant
WHERE (Schedule.Participant <> Schedule_1.Participant AND Schedule.Participant = @ParticipantID) 
		AND (Schedule.IntervalStartDate BETWEEN CONVERT(DATETIME, @StartDate, 102) 
		AND CONVERT(DATETIME,@EndDate, 102))

Open in new window

0
 
LVL 1

Author Closing Comment

by:penlandt
ID: 40695566
I'm accepting my own solution because no others have been forthcoming and I completed the project using my own solution (limiting teams to two participants each).  I no longer need an answer, those others may in the future.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question