回答編集履歴

2

sample2

2017/03/23 06:16

投稿

yambejp
yambejp

スコア114843

test CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
 
18
18
 
19
- # sample
19
+ # sample1
20
20
 
21
21
  とりあえずデイリーランキングのサンプルです
22
22
 
@@ -105,3 +105,27 @@
105
105
 
106
106
 
107
107
  ```
108
+
109
+
110
+
111
+ # sample2
112
+
113
+ 月で集計したランキング
114
+
115
+
116
+
117
+ ```SQL
118
+
119
+ select t1.uid,t1.point,count(*) as rank
120
+
121
+ from (select uid,sum(point) as point from score where d between '2017-03-01' and '2017-03-31' group by uid) as t1
122
+
123
+ inner join (select uid,sum(point) as point from score where d between '2017-03-01' and '2017-03-31' group by uid) as t2
124
+
125
+ on t1.point<=t2.point
126
+
127
+ group by t1.uid
128
+
129
+ order by rank;
130
+
131
+ ```

1

sample

2017/03/23 06:16

投稿

yambejp
yambejp

スコア114843

test CHANGED
@@ -13,3 +13,95 @@
13
13
  その辺の仕様がわからないとどうしようもないですね。
14
14
 
15
15
  ちなみにランキングは定番の書き方があります。
16
+
17
+
18
+
19
+ # sample
20
+
21
+ とりあえずデイリーランキングのサンプルです
22
+
23
+
24
+
25
+ - 元データ
26
+
27
+ ```SQL
28
+
29
+ create table score(id int primary key auto_increment,uid int,d date,point int,unique(uid,d));
30
+
31
+ insert into score(uid,d,point) values
32
+
33
+ (1,'2017-03-01',100),
34
+
35
+ (2,'2017-03-01',110),
36
+
37
+ (3,'2017-03-01',120),
38
+
39
+ (1,'2017-03-10',100),
40
+
41
+ (2,'2017-03-10',200),
42
+
43
+ (3,'2017-03-10',80),
44
+
45
+ (4,'2017-03-10',50),
46
+
47
+ (1,'2017-03-20',300),
48
+
49
+ (2,'2017-03-20',200),
50
+
51
+ (4,'2017-03-20',130),
52
+
53
+ (5,'2017-03-20',120),
54
+
55
+ (6,'2017-03-20',120),
56
+
57
+ (7,'2017-03-20',140);
58
+
59
+ ```
60
+
61
+
62
+
63
+ - とりあえず3/20のランキング
64
+
65
+ ```SQL
66
+
67
+ select uid,point,(select count(*)+1 from score as t2 where t1.point<t2.point and t1.d=t2.d) rank
68
+
69
+ from score as t1
70
+
71
+ where d='2017-03-20'
72
+
73
+ order by rank;
74
+
75
+ ```
76
+
77
+
78
+
79
+ - 3/10から3/20へのアップダウン
80
+
81
+ ```SQL
82
+
83
+ select t3.uid,t3.point,t3.rank,t4.point as prepoint,t4.rank as prerank
84
+
85
+ ,case when t4.rank is null then 'up' when t3.rank<t4.rank then 'up' when t3.rank=t4.rank then '-' else 'down' end as shift
86
+
87
+ from(select uid,point,(select count(*)+1 from score as t2 where t1.point<t2.point and t1.d=t2.d) rank
88
+
89
+ from score as t1
90
+
91
+ where d='2017-03-20') as t3
92
+
93
+ left join (
94
+
95
+ select uid,point,(select count(*)+1 from score as t2 where t1.point<t2.point and t1.d=t2.d) rank
96
+
97
+ from score as t1
98
+
99
+ where d='2017-03-10'
100
+
101
+ ) as t4 on t3.uid=t4.uid
102
+
103
+ order by rank;
104
+
105
+
106
+
107
+ ```