I have recently taken over running the internal leagues at the squash club I play at
I was hoping to put these online for members to view and add results as required
The league structure follows the below format with 6 leagues
League 1
| | John | Mark | Peter | Martin | Paul |
|:------:|:----:|:----:|:-----:|:------:|:----:|
| John | NULL | 3 | 0 | 1 | 2 |
| Mark | 0 | NULL | 1 | 3 | 0 |
| Peter | 3 | 3 | NULL | 1 | 3 |
| Martin | 3 | 1 | 3 | NULL | 2 |
| Paul | 3 | 3 | 0 | 3 | NULL |
League 2
etc etc
I have designed the table structure as
CREATE TABLE [dbo].[Results](
[ResultId] [int] IDENTITY(1,1) NOT NULL,
[LeagueId] [int] NOT NULL,
[Player1Id] [int] NOT NULL,
[Player2Id] [int] NOT NULL,
[Player1Result] [int] NULL,
[Player2Result] [int] NULL)
CREATE TABLE [dbo].[Players](
[PlayerId] [int] IDENTITY(1,1) NOT NULL,
[UserId] [int] NOT NULL,
[FirstName] [nvarchar](150) NULL,
[LastName] [nvarchar](150) NULL)
CREATE TABLE [dbo].[Leagues](
[LeagueId] [int] IDENTITY(1,1) NOT NULL,
[LeagueName] [nvarchar](50) NULL)
I am trying to write a query which gives me the output of each divsion in one query rather than several to give me the output
can anyone help with the query?
what i have so far is
select p.FirstName, p1.player2result, p2.player2result, p3.player2result, p4.player2result
from
(select player2Result from Results p1 where p.playerId = p1.Player2Id
union
select player2Result from Results p2 where p.playerId = p2.Player2Id
union
select player2Result from Results p3 where p.playerId = p3.Player2Id
union
select player2Result from Results p4 where p.playerId = p4.Player2Id) as opResult
LEFT JOIN Players p on opResult.Player2Result = p.PlayerId
GROUP BY p.FirstName, p1.player2result, p2.player2result, p3.player2result, p4.player2result