Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
adrox
Starting Member
17 Posts |
Posted - 2005-05-13 : 15:17:14
|
| I have a this query:select msp.memberID, r.Week,(case when msp.PointsAwarded > 0 AND rd.Finish < 11 AND rd.Finish > 0 then 1 ELSE 0 END) PointsAwardedFROM MemberStreakPick msp INNER JOIN raceDriver rd ON rd.RaceDriverID = msp.RaceDriverIDLEFT OUTER JOIN Race r ON r.raceID = rd.raceIDwhere memberID = 719181 order by r.weekwhich returns the following results:MEMBERID WEEK PointsAwarded719181 1 1719181 2 1719181 3 1719181 4 1719181 6 1719181 7 1719181 8 1719181 9 1719181 10 1719181 11 0My problem is it is skipping week 5 because there is not a related record for week 5 in the memberStreakPick table. So what I would like to do is join it in a way that it would return these resultsMEMBERID WEEK PointsAwarded719181 1 1719181 2 1719181 3 1719181 4 1719181 5 0719181 6 1719181 7 1719181 8 1719181 9 1719181 10 1719181 11 0Thanks for any help. |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2005-05-15 : 20:48:44
|
How about this:select msp.memberID, r.Week,(case when msp.PointsAwarded > 0 AND rd.Finish < 11 AND rd.Finish > 0 then 1 ELSE 0 END) PointsAwardedFROMRace rLEFT OUTER JOINraceDriver rd ON r.raceid=rd.RaceDriverIDINNER JOINMemberStreakPick mspon rd,RaceDriverID = msp.RaceDriverIDwhere memberID = 719181 order by r.week I'm assuming that Race hase EVERY week in it...AndyThere's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
adrox
Starting Member
17 Posts |
Posted - 2005-05-16 : 08:48:29
|
| Your assumption is correct, race does have every week in it. This query of yours returns the same results as mine. The user does not have a record for week 5 in the other two tables (raceDriver and memberStreakPick) so I need to put a place holder in the query results with memberID, week, and ptsAwarded.If the user didnt have a pick like in this case the result set should put this as a place holder:719181, 5, 0 |
 |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2005-05-16 : 08:55:08
|
| Look into using a "TALLY table" to control joining to missing data....(left join)Search here for instances/examples. |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2005-05-16 : 20:42:22
|
| If the race table has every week in it, we should get every week if get the join right. What happens if you change the INNER JOIN to a LEFT OUTER JOIN? I think the query I posted is what you want, but since you say it is returning the same thing, I must be missing something. Post your table info and some sample data. We'll get this...AndyThere's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
adrox
Starting Member
17 Posts |
Posted - 2005-05-17 : 09:11:48
|
| This query:select msp.memberID, r.Week,(case when msp.PointsAwarded > 0 AND rd.Finish < 11 AND rd.Finish > 0 then 1 ELSE 0 END) PointsAwardedFROM MemberStreakPick msp INNER JOIN raceDriver rd ON rd.RaceDriverID = msp.RaceDriverIDLEFT OUTER JOIN Race r ON r.raceID = rd.raceIDwhere memberID = 719181 order by r.weekReturns this:MEMBERID WEEK PointsAwarded719181 1 1719181 2 1719181 3 1719181 4 1719181 6 1719181 7 1719181 8 1719181 9 1719181 10 1719181 11 0and this select msp.memberID, r.Week,(case when msp.PointsAwarded > 0 AND rd.Finish < 11 AND rd.Finish > 0 then 1 ELSE 0 END) PointsAwardedFROMRace rLEFT OUTER JOINraceDriver rd ON r.RaceID=rd.RaceIDINNER JOINMemberStreakPick mspon rd.RaceDriverID = msp.RaceDriverIDwhere memberID = 719181 order by r.weekalso returns this:MEMBERID WEEK PointsAwarded719181 1 1719181 2 1719181 3 1719181 4 1719181 6 1719181 7 1719181 8 1719181 9 1719181 10 1719181 11 0Skiping week five.Table info:Race:RaceID, Week, Name, WhenStart(There is a record here for every week and race of the season, 36 weeks)RaceDriver:RaceDriverID, DriverID, RaceID, Finish(each week a new raceDriverID is created based on DriverID's that are in the race)MemberStreakPick:MemberID, RaceDriverID, PointsAwarded(flag 1 yes, 0 No)(If the user makes a pick for this race it is stored here)MemberStreakMemberID, totalPoints(main user info table) |
 |
|
|
adrox
Starting Member
17 Posts |
Posted - 2005-05-17 : 09:19:23
|
| Sorry should of added this:Race:RaceID, Week, Name, WhenStart1001, 1, Subway 500, 2/22/2005 06:00:00 PM1002, 2, ..., ...1003, 3, .., ...1004, 4, ..., ...1005, 5, .., ...1006, 6,.., ...(There is a record here for every week and race of the season, 36 weeks)RaceDriver:RaceDriverID, DriverID, RaceID, Finish(this is one driver for the 6 weeks)104525, 1020, 1001, 11104324, 1020, 1002, 1106565, 1020, 1003, 15109792, 1020, 1004, 11111241, 1020, 1006, 9113242, 1020, 1006, 40(each week a new raceDriverID is created based on DriverID's that are in the race)MemberStreakPick:MemberID, RaceDriverID, PointsAwarded(flag 1 yes, 0 No)(user picks driver above for 5 of the 6 weeks)719181,104525, 1719181,104324, 1719181,106565, 1719181,109792, 1719181,113242, 1(If the user makes a pick for this race it is stored here, in this case the user has no record for week 5)MemberStreakMemberID, totalPoints719181, 650(main user info table) |
 |
|
|
adrox
Starting Member
17 Posts |
Posted - 2005-05-17 : 12:11:50
|
| And using left joins on both produce the same result |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2005-05-18 : 07:39:13
|
Ok Adrox,I'll look at it tonight. Gotta go to the doc today for some tests... AndyThere's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
adrox
Starting Member
17 Posts |
Posted - 2005-05-19 : 10:48:23
|
| Still having trouble with this. Anyones imput would be helpful.Thanks |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2005-05-19 : 11:38:32
|
Hey Adrox,This work for you?select msp.memberID, r.Week,(case when msp.PointsAwarded > 0 AND rd.Finish < 11 AND rd.Finish > 0 then 1 ELSE 0 END) PointsAwardedFROM race rleft outer JOIN raceDriver rd ON rd.RaceID = r.raceidleft outer JOIN MemberStreakPick msp ON rd.racedriverID = msp.racedriveridand msp.memberid='719181'order by r.week AndyBTW: when you post table info, if you do it as code it makes it easier for everyone to help you... There's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2005-05-19 : 12:56:22
|
Let's break it down, "Dr. Cross Join"-style:1) Don't worry about the final output and blank rows yet. make sure your calculations are good to go. Keep in mind you want to return 1 row per combination of MemberID/RaceID. Don't worry about filtering for a particular memberID just yet, worry about writing the SELECT with a GROUP BY on MemberID/RaceID that returns the results you want. Let's refer to this select as "MemberRacePoints":Select M.MemberID, RD.RaceID, M.PointsAwardedfrom MemberStreakPick Minner join RaceDriver RDon RD.RaceDriverID = M.RaceDriverIDwhere RD.Finish between 1 and 10 and M.PointsAwarded = 1 2) Now, put that first SELECT aside and now think about your final results. You need to consider the population of all members and all races, and return an indication if that combination resulted in points awarded. To consider all members, all races, a cross join is used:select M.MemberID, R.RaceID, R.Weekfrom Members Mcross join Races R It's pretty simple what it is doing. Don't worry about calculations in this step, worry about returning the rows you need to produce the results you are looking for. Also don't worry about a particular MemberID just yet.At this point you have two very simple queries that are easy to troubleshoot, there's no worries about nulls and left outer joins or formatting, you have just broken it down into small, simple steps.3) Now put it all together -- all you need to do take your previous SELECT, which returns all combos you need to consider, and do a LEFT OUTER JOIN to "MemberRacePoints", and filter for the MemberID you want:Select M.MemberID, R.RaceID, R.Week, ISNULL(MRP.PointsAwarded,0) as PointsAwardedfrom Members Mcross join Races Rleft outer join (... the SQL for MemberRacePoints goes here ...) MRPon R.RaceID = MRP.RAceID and M.MemberID = MRP.MemberIDwhere M.MemberID = @MemberID As always, remember never to include columns from outer tables in your WHERE clause, since that turns the outer join into an inner join.- Jeff |
 |
|
|
steamngn
Constraint Violating Yak Guru
306 Posts |
Posted - 2005-05-19 : 15:16:48
|
Hey Jeff! DOH! Didn't even think about CROSS JOIN! AndyThere's never enough time to type code right, but always enough time for a hotfix... |
 |
|
|
adrox
Starting Member
17 Posts |
Posted - 2005-05-24 : 08:40:13
|
| Thanks Dr. Cross Join. That was the solution that I was looking for.@dam |
 |
|
|
|
|
|
|
|