Solved

Help Needed with Query

Posted on 2015-01-20
9
33 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
t-sql money data type decimal places 4 28
sqlseverexpress 2008 agent xps question 1 12
Log Backup 2 13
Stored Procedure 2 0
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…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

13 Experts available now in Live!

Get 1:1 Help Now