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 |
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2007-10-08 : 08:38:49
|
Hi,I am designing a system where 2 users can 'battle' each other. Other users vote who is the winner, and from there I show results etc.So far I've come up with a 2 table system as shown below. I'm not 100% sure if my design is ideal, specifically the "winner_userID" in "tblBattleVotes", if anyone thinks I should do this differently I'd love to hear.One thing I'd like to integrate as well is some sort of authorization where the opponent has to agree to be challenged, any suggestions on that are much appreciated too!I want to have a query that gets passed a userID, and from there it returns all the users battles and their win percentage. (battles in which they where a challenger or opponent)Can anyone offer assistance on this query? I'm stuck at this but also looking for any insight / suggestions to see if I am taking the best approach.Thanks for any assistance!! Much appreciated ! :)mike123CREATE PROCEDURE [dbo].[select_battleResults] ( @userID int )AS SET NOCOUNT ON SELECT challenger_userID,opponent_userID, challenger_totalWins (not sure best way to get this value), challenger_winPercentage (again?), opponent_winPercentage (or this one) FROM tblBattlesWHERE challenger_userID = @userID OR opponent_userID = @userID CREATE TABLE [dbo].[tblBattles]( [battleID] [int] IDENTITY(1,1) NOT NULL, [challenger_userID] [int] NOT NULL, [opponent_userID] [int] NOT NULL) ON [PRIMARY]CREATE TABLE [dbo].[tblBattle_Votes]( [voteID] [int] IDENTITY(1,1) NOT NULL, [battleID] [int] NOT NULL, [voter_userID] [int] NOT NULL, [winner_userID] [int] NOT NULL, [voteDate] [datetime] NOT NULL) ON [PRIMARY] |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-08 : 08:41:17
|
How do you know if it is a "win"? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-08 : 08:45:42
|
[code]SELECT b.BattleID, SUM(CASE WHEN @UserID = bv.winner_userID THEN 1.0 -- Or 100.0 if you want percentage spelled out ELSE 0.0 END) / COUNT(*) AS WinningPercentageFROM tblBattles AS bINNER JOIN tblBattle_Votes AS bv ON bv.BattleID = b.BattleIDWHERE @UserID IN (b.challenger_userID, b.opponent_userID)GROUP BY b.BattleID[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-08 : 08:48:51
|
You can extend that to know percentage when either Opponent or ChallengerSELECT b.BattleID, CASE WHEN @UserID = b.challenger_userID THEN 'Challenger' ELSE 'Opponent' END AS BattleType, SUM(CASE WHEN @UserID = bv.winner_userID THEN 1.0 ELSE 0.0 END) / COUNT(*) AS WinningPercentageFROM tblBattles AS bINNER JOIN tblBattle_Votes AS bv ON bv.BattleID = b.BattleIDWHERE @UserID IN (b.challenger_userID, b.opponent_userID)GROUP BY b.BattleID, CASE WHEN @UserID = b.challenger_userID THEN 'Challenger' ELSE 'Opponent' END E 12°55'05.25"N 56°04'39.16" |
 |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2007-10-08 : 08:52:50
|
quote: Originally posted by Peso How do you know if it is a "win"? E 12°55'05.25"N 56°04'39.16"
Hi Peso,In "tblBattle_Votes" I have the column "winner_userID" which is where I declare the winner. There is a winner on every 'battle'. I know who the winner is by checking this userID to the original battle, I guess I could also perhaps store a value for 0 for challenger winning and 1 for opponent winning, I just decided to do it this way for some reason.Reviewing your query suggestions now... thanks so much!!mike123 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-08 : 08:57:30
|
Is this system available online?If so, please add me so I can see the system where I have suggested at least 50% of the "hard code"  E 12°55'05.25"N 56°04'39.16" |
 |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2007-10-08 : 09:26:25
|
quote: Originally posted by Peso Is this system available online?If so, please add me so I can see the system where I have suggested at least 50% of the "hard code"  E 12°55'05.25"N 56°04'39.16"
Hey Peso,You have definately contributed a huge amount, you've been an amazing help! I'd love to get you access to the design on my local machine, I will see what I can do and keep you posted! As soon as I get something I will email you. ( I've been developing on the live machine, thats probably a sql guru no-no )The second query is exactly what I was looking for, working perfectlyThanks again!! mike123 |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-10-08 : 09:42:28
|
| Peso:You light-sabre is in the post mate!! |
 |
|
|
|
|
|
|
|