2

私は 3 つのテーブルを持っています: tbl_Player、tbl_MatchDetails、tbl_MatchStat といくつかのデータ:

tbl_Player

CREATE TABLE [dbo].[tbl_Player](
    [PlayerID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NULL,
 CONSTRAINT [PK_tbl_Player] PRIMARY KEY CLUSTERED 
(
    [PlayerID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[tbl_Player] ON
INSERT [dbo].[tbl_Player] ([PlayerID], [Name]) VALUES (1, N'L Messi')
INSERT [dbo].[tbl_Player] ([PlayerID], [Name]) VALUES (2, N'C Ronaldo')
INSERT [dbo].[tbl_Player] ([PlayerID], [Name]) VALUES (3, N'Neymar')
INSERT [dbo].[tbl_Player] ([PlayerID], [Name]) VALUES (4, N'Rooney')
INSERT [dbo].[tbl_Player] ([PlayerID], [Name]) VALUES (5, N'K Aguero')
INSERT [dbo].[tbl_Player] ([PlayerID], [Name]) VALUES (6, N'Gomez')
SET IDENTITY_INSERT [dbo].[tbl_Player] OFF

tbl_MatchDetails

CREATE TABLE [dbo].[tbl_MatchDetails](
    [MatchID] [int] IDENTITY(1,1) NOT NULL,
    [Tournament] [varchar](50) NULL,
    [TeamA] [varchar](50) NULL,
    [TeanB] [varchar](50) NULL,
 CONSTRAINT [PK_tbl_MatchDetails] PRIMARY KEY CLUSTERED 
(
    [MatchID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[tbl_MatchDetails] ON
INSERT [dbo].[tbl_MatchDetails] ([MatchID], [Tournament], [TeamA], [TeanB]) VALUES (1, N'CL', N'Real', N'Barca')
INSERT [dbo].[tbl_MatchDetails] ([MatchID], [Tournament], [TeamA], [TeanB]) VALUES (2, N'CL', N'Barca', N'United')
INSERT [dbo].[tbl_MatchDetails] ([MatchID], [Tournament], [TeamA], [TeanB]) VALUES (3, N'League', N'Barca', N'Real')
INSERT [dbo].[tbl_MatchDetails] ([MatchID], [Tournament], [TeamA], [TeanB]) VALUES (4, N'CL', N'M City', N'Bayern')
INSERT [dbo].[tbl_MatchDetails] ([MatchID], [Tournament], [TeamA], [TeanB]) VALUES (5, N'League', N'Barca', N'Valencia')
INSERT [dbo].[tbl_MatchDetails] ([MatchID], [Tournament], [TeamA], [TeanB]) VALUES (6, N'League', N'Real', N'Betis')
SET IDENTITY_INSERT [dbo].[tbl_MatchDetails] OFF

tbl_MatchStat

CREATE TABLE [dbo].[tbl_MatchStat](
    [StatID] [int] IDENTITY(1,1) NOT NULL,
    [MatchID] [int] NULL,
    [PlayerID] [int] NULL,
    [Goals] [int] NULL,
    [Minutes] [int] NULL,
 CONSTRAINT [PK_tbl_MatchStat] PRIMARY KEY CLUSTERED 
(
    [StatID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[tbl_MatchStat] ON
INSERT [dbo].[tbl_MatchStat] ([StatID], [MatchID], [PlayerID], [Goals], [Minutes]) VALUES (1, 1, 1, 3, 92)
INSERT [dbo].[tbl_MatchStat] ([StatID], [MatchID], [PlayerID], [Goals], [Minutes]) VALUES (2, 1, 2, 1, 92)
INSERT [dbo].[tbl_MatchStat] ([StatID], [MatchID], [PlayerID], [Goals], [Minutes]) VALUES (3, 2, 1, 2, 85)
INSERT [dbo].[tbl_MatchStat] ([StatID], [MatchID], [PlayerID], [Goals], [Minutes]) VALUES (4, 2, 4, 2, 93)
INSERT [dbo].[tbl_MatchStat] ([StatID], [MatchID], [PlayerID], [Goals], [Minutes]) VALUES (5, 3, 1, 1, 35)
INSERT [dbo].[tbl_MatchStat] ([StatID], [MatchID], [PlayerID], [Goals], [Minutes]) VALUES (6, 3, 2, 0, 90)
INSERT [dbo].[tbl_MatchStat] ([StatID], [MatchID], [PlayerID], [Goals], [Minutes]) VALUES (7, 4, 6, 2, 93)
INSERT [dbo].[tbl_MatchStat] ([StatID], [MatchID], [PlayerID], [Goals], [Minutes]) VALUES (8, 4, 5, 0, 15)
INSERT [dbo].[tbl_MatchStat] ([StatID], [MatchID], [PlayerID], [Goals], [Minutes]) VALUES (9, 5, 3, 2, 25)
INSERT [dbo].[tbl_MatchStat] ([StatID], [MatchID], [PlayerID], [Goals], [Minutes]) VALUES (10, 5, 1, 4, 91)
INSERT [dbo].[tbl_MatchStat] ([StatID], [MatchID], [PlayerID], [Goals], [Minutes]) VALUES (11, 6, 2, 3, 88)
SET IDENTITY_INSERT [dbo].[tbl_MatchStat] OFF

このデータから、トーナメントごとの各プレーヤーの概要を示す select ステートメントが必要でした。たとえば、このデータからトーナメント「CL」を選択すると、クエリの結果は次のようになります。

名前 マッチプレーした合計ゴール 最高得点 ハットトリック
L メッシ 2 5 3 117 1
C・ロナウド 1 1 1 92 0
ルーニー 1 2 2 93 0         
K アグエロ 1 0 0 15 0
ゴメス 1 2 2 93 0
4

1 に答える 1

1

試す

SELECT p.Name, 
       COUNT(m.MatchID) MatchPlayed, 
       SUM(s.Goals) TotalGoals,
       MAX(s.Goals) HighestScore,
       SUM(s.Minutes) Minutes,
       SUM(CASE WHEN s.Goals >= 3 THEN 1 ELSE 0 END) Hattrick
  FROM tbl_MatchDetails m JOIN tbl_MatchStat s
    ON m.MatchID = s.MatchID JOIN tbl_Player p
    ON s.PlayerID = p.PlayerID
 WHERE m.Tournament = 'CL'
 GROUP BY p.Name

出力:

| | 名前 | マッチプレー | TOTALGOALS | 最高得点 | 分 | ハットトリック |
-------------------------------------------------- --------------------------
| | C・ロナウド | 1 | 1 | 1 | 92 | 0 |
| | ゴメス | 1 | 2 | 2 | 93 | 0 |
| | K・アグエロ | 1 | 0 | 0 | 15 | 0 |
| | L・メッシ | 2 | 5 | 3 | 177 | 1 |
| | ルーニー | ルーニー | 1 | 2 | 2 | 93 | 0 |

これがSQLFiddleのデモです

于 2013-06-09T08:06:09.650 に答える