Solved

Help Needed with Query

Posted on 2015-01-20
9
40 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Stack Overflow Podcast - Frustrating Miracles

In this podcast, Stack Overflow interviewed Linux Academy CEO/Founder, Anthony James, and got his developer story!

"Follow your passion, be prepared to work hard and sacrifice, and, above all, don't let anyone limit your dreams."  - Donovan Bailey

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…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

622 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