Solved

Help Needed with Query

Posted on 2015-01-20
9
37 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 48

Expert Comment

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

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Pivot Rows To Columns 10 52
Using MERGE to UPDATE a third table 14 37
convert in derived column 7 27
Query Help - MSSQL - Averages 5 25
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now