-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQL_Statements.sql
More file actions
140 lines (116 loc) · 3.7 KB
/
Copy pathMySQL_Statements.sql
File metadata and controls
140 lines (116 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
create table gogames character set gbk (
game_date DATE not null,
game_name VARCHAR (160) not null,
black_player VARCHAR(32) not null,
white_player VARCHAR(32) not null,
raw_result VARCHAR(16) not null,
raw_steps VARCHAR(16) not null,
result int not null default -1,
steps int not null default 0,
primary key (game_date, game_name, black_player, white_player, raw_result, raw_steps),
);
desc gogames;
alter table gogames character set gbk;
alter table gogames alter result set default 0;
insert into gogames values (
'1921-07-14', '1921日本临时比赛', '金井茂', '本因坊秀哉', '白中盘胜', '169手', '2', '169'
);
LOAD DATA LOCAL INFILE "C:\\Users\\FinixLei\\Desktop\\Level\\FocusFinalData.txt" INTO TABLE gogames;
--------------------------------------------------
create table dimension_result (
result int not null primary key,
description VARCHAR(32) not null);
alter table dimension_result character set gbk;
insert into dimension_result values ('0', '和棋');
insert into dimension_result values ('1', '黑胜');
insert into dimension_result values ('2', '白胜');
insert into dimension_result values ('-1', '胜负未知');
---------------------------------------------------
create view gogamesview as
select g.game_date, g.game_name, g.black_player, g.white_player,
dr.description, g.raw_result, g.raw_steps
from gogames as g inner join dimension_result as dr
where g.result = dr.result;
select * from gogamesview;
---------------------------------------------------
drop procedure WinRateProc;
DELIMITER //
CREATE PROCEDURE WinRateProc(IN start_date DATE, IN end_date DATE,
IN player VARCHAR(32) character set GBK,
OUT win_num int, OUT total_num int, OUT win_rate double)
BEGIN
select count(*) into win_num from rankinglist.gogames
where
game_date >= start_date and game_date <= end_date and
(
(black_player = player and result = 1) or
(white_player = player and result = 2)
);
select count(*) into total_num from rankinglist.gogames
where
game_date >= start_date and game_date <= end_date and
(black_player = player or white_player = player) and
(result = 0 or result = 1 or result = 2);
set win_rate=win_num/total_num;
select player, win_num, total_num, win_rate;
END
//
DELIMITER ;
call WinRateProc('1900-01-01', '2013-12-31', '金志锡', @win_num, @total_num, @win_rate);
-------------------------------------------------------------------------
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `AllPlayersWinRate`()
BEGIN
set @count=0;
select (@count := @count + 1) as seq,
rate_t.player, rate_t.win_num, rate_t.all_num, rate_t.win_rate
from
(
select win_t.player as player, win_t.num as win_num, (win_t.num + notwin_t.num) as all_num,
win_t.num/(win_t.num + notwin_t.num) as win_rate
from
(
select (b_win.num + w_win.num) as num, b_win.black_player as player
from
(
select count(*) as num, black_player
from gogames
where result = 1
group by black_player
) b_win
inner join
(
select count(*) as num, white_player
from gogames
where result = 2
group by white_player
) w_win
on b_win.black_player = w_win.white_player
) win_t
inner join
(
select (b_notwin.num + w_notwin.num) as num, b_notwin.black_player as player
from
(
select count(*) as num, black_player
from gogames
where result = 0 or result = 2
group by black_player
) b_notwin
inner join
(
select count(*) as num, white_player
from gogames
where result = 0 or result = 1
group by white_player
) w_notwin
on b_notwin.black_player = w_notwin.white_player
) notwin_t
on win_t.player = notwin_t.player
where win_t.num + notwin_t.num >= 50
order by 4 desc
) rate_t;
END
//
DELIMITER ;
call AllPlayersWinRate();