##質問内容
ユーザーのスコア(ポイント)を、ランキング用のテーブルを作成し定期的にバッチ処理で集計しようと考えています。
①日、週、月のランキング
②前回との順位の比較(昨日2位、今日1位だった場合「UP」のようなイメージ)
上記のようなことを実現したいのですが、DB設計はどのようにするといいでしょうか?
よろしくお願いします。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答1件
0
ベストアンサー
ユーザーIDごとに順位の基準となるポイントと期日を記録しておけばよいでしょう。
デイリーラインキングはいいとして週と月ってどういう計算ですか?
週は月曜から日曜のデイリーポイントの積み上げなのでしょうか?
それとも最終日(たとえば日曜)の時点のポイントなのでしょうか?
その辺の仕様がわからないとどうしようもないですね。
ちなみにランキングは定番の書き方があります。
sample1
とりあえずデイリーランキングのサンプルです
- 元データ
SQL
1create table score(id int primary key auto_increment,uid int,d date,point int,unique(uid,d)); 2insert into score(uid,d,point) values 3(1,'2017-03-01',100), 4(2,'2017-03-01',110), 5(3,'2017-03-01',120), 6(1,'2017-03-10',100), 7(2,'2017-03-10',200), 8(3,'2017-03-10',80), 9(4,'2017-03-10',50), 10(1,'2017-03-20',300), 11(2,'2017-03-20',200), 12(4,'2017-03-20',130), 13(5,'2017-03-20',120), 14(6,'2017-03-20',120), 15(7,'2017-03-20',140);
- とりあえず3/20のランキング
SQL
1select uid,point,(select count(*)+1 from score as t2 where t1.point<t2.point and t1.d=t2.d) rank 2from score as t1 3where d='2017-03-20' 4order by rank;
- 3/10から3/20へのアップダウン
SQL
1select t3.uid,t3.point,t3.rank,t4.point as prepoint,t4.rank as prerank 2,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 3from(select uid,point,(select count(*)+1 from score as t2 where t1.point<t2.point and t1.d=t2.d) rank 4from score as t1 5where d='2017-03-20') as t3 6left join ( 7select uid,point,(select count(*)+1 from score as t2 where t1.point<t2.point and t1.d=t2.d) rank 8from score as t1 9where d='2017-03-10' 10) as t4 on t3.uid=t4.uid 11order by rank; 12
sample2
月で集計したランキング
SQL
1select t1.uid,t1.point,count(*) as rank 2from (select uid,sum(point) as point from score where d between '2017-03-01' and '2017-03-31' group by uid) as t1 3inner join (select uid,sum(point) as point from score where d between '2017-03-01' and '2017-03-31' group by uid) as t2 4on t1.point<=t2.point 5group by t1.uid 6order by rank;
投稿2017/03/22 09:03
編集2017/03/23 06:16総合スコア114572
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/22 10:27
2017/03/23 03:01 編集
2017/03/23 05:18
2017/03/23 05:20
2017/03/23 05:54
2017/03/23 06:17
2017/03/23 08:41
2017/03/23 08:51
2017/03/31 07:40