Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help Needed with Query

Posted on 2015-01-20
9
Medium Priority
?
41 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
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

Technology Partners: 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!

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

715 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