views:

43

answers:

1

Hi

below is a sql server table, which is used to hold users prediction for soccer matches. a user can predict either a home win, an away win or a draw/tie for a specific soccer match.

how can i query this table to find the top 3 predictions, the top 3 games must be distinct gameId's

either SQL or LINQ solution would be much appriciated

alt text

+2  A: 

I'll assume you want the strongest predictions, i.e., the games where the most people agree on the outcome.

 var predictions = db.Predictions
                     .GroupBy( g => g.gameId )
                     .Select( g => new 
                      {
                            gameId = g.Key,
                            homeWin = g.Sum( h => h.homeWin ),
                            awayWin = g.Sum( a => a.awayWin ),
                            draw = g.Sum( d => d.draw )
                      })
                     .Select( g => new 
                      {
                            gameId = g.gameId,
                            prediction = g.homeWin > g.awayWin 
                                             ? (g.homeWin > draw 
                                                    ? 'homeWin'
                                                    : 'draw')
                                             : (g.awayWin > g.draw
                                                    ? 'awayWin'
                                                    : 'draw'),
                            strength = g.homeWin > g.awayWin 
                                             ? (g.homeWin > draw 
                                                    ? g.homeWin
                                                    : g.draw) 
                                             : (g.awayWin > g.draw 
                                                    ? g.awayWin 
                                                    : g.draw)
                      })
                     .OrderByDescending( g => g.strength )
                     .Take(3);

You might be able to combine the two selects, but IMO it would hurt the readability of the code.

tvanfosson
kb
thanks tvanfosson your solution works great
kb