現在、私はMySQL 5.6.30を使用しています。ここで最小金額から最大金額のグループを複数の列で作成する
名前がtemp_work
+----+-----------+----------+----------+--------+---------+-------+---------+
| id | client_id | account | category | t_year | t_month | t_day | amount |
+----+-----------+----------+----------+--------+---------+-------+---------+
| 1 | 100 | Saving | deposit | 2016 | 12 | 14 | 100.84 |
| 2 | 100 | Checking | withdraw | 2016 | 12 | 15 | 300.24 |
| 3 | 100 | Checking | deposit | 2016 | 12 | 29 | 60.00 |
| 4 | 101 | Saving | Withdraw | 2016 | 12 | 29 | 245.16 |
| 5 | 100 | Saving | Withdraw | 2016 | 12 | 30 | 2200.00 |
| 6 | 100 | Checking | Withdraw | 2016 | 12 | 30 | 2372.16 |
| 7 | 100 | Saving | deposit | 2016 | 12 | 30 | 4327.00 |
| 8 | 101 | Checking | Withdraw | 2017 | 1 | 3 | 80.00 |
| 9 | 101 | Checking | Withdraw | 2017 | 1 | 3 | 1033.45 |
| 10 | 100 | Saving | Withdraw | 2017 | 1 | 3 | 1339.16 |
| 11 | 100 | Checking | deposit | 2017 | 1 | 4 | 140.00 |
| 12 | 100 | Checking | Withdraw | 2017 | 1 | 4 | 216.73 |
| 13 | 101 | Checking | Withdraw | 2017 | 1 | 4 | 1261.72 |
+----+-----------+----------+----------+--------+---------+-------+---------+
であると私はclient_idの、アカウント、カテゴリ、t_year、t_monthによって最大量群への最低額からランクを必要とするテーブル内のデータがあります。
だから、これは私が期待したものであるか、類似した
+-----------+----------+----------+--------+---------+---------+------+
| client_id | account | category | t_year | t_month | amount | rank |
+-----------+----------+----------+--------+---------+---------+------+
| 100 | Checking | deposit | 2016 | 12 | 60.00 | 1 |
| 100 | Checking | deposit | 2017 | 1 | 140.00 | 1 |
| 100 | Checking | withdraw | 2016 | 12 | 300.24 | 1 |
| 100 | Checking | withdraw | 2016 | 12 | 2327.16 | 2 |
| 100 | Checking | Withdraw | 2017 | 1 | 216.73 | 1 |
| 100 | Saving | deposit | 2016 | 12 | 100.84 | 1 |
| 100 | Saving | deposit | 2016 | 12 | 4327.00 | 2 |
| 100 | Saving | Withdraw | 2016 | 12 | 2200.00 | 1 |
| 100 | Saving | Withdraw | 2017 | 1 | 1339.16 | 1 |
| 101 | Checking | Withdraw | 2017 | 1 | 80.00 | 1 |
| 101 | Checking | Withdraw | 2017 | 1 | 1033.45 | 2 |
| 101 | Checking | Withdraw | 2017 | 1 | 1261.72 | 3 |
| 101 | Saving | Withdraw | 2016 | 12 | 245.16 | 1 |
+-----------+----------+----------+--------+---------+---------+------+
ここでは私の最初の試みである:
Select
tw1.client_id, tw1.account, tw1.category, tw1.t_year, tw1.t_month, tw1.amount
,@rownum = case when @tmonth <> tw1.t_month then 0 else @rownum + 1 end as ranking
,@tmonth := t_month as cmonth
From
(Select @rownum := 0 from dual) as r,
(Select @tmonth := 0 from dual) as m,
(Select client_id, account, category, t_year, t_month, amount
From temp_work as tw
Order by client_id, account, category, t_year, t_month, amount) as tw1
2回目の試行:
Select tw1.client_id, tw1.account, tw1.category, tw1.t_year, tw1.t_month, tw1.amount
,@rownum = case when concat(@tyear, '-', @tmonth) <> concat(t_year, '-', t_month) then 0 else @rownum + 1 end as rank
,@tyear := t_year as cyear
,@tmonth := t_month as cmonth
From
(Select @rownum := 0) as r,
(Select @tyear := 0) as y,
(Select @tmonth := 0) as m,
(
Select client_id, account, category, t_year, t_month, amount
From temp_work
Order by tw.client_id, tw.account, tw.category, t_year, t_month, tw.amount
) as tw1
しかし、上記のクエリは私に与えます以下のような同じ結果です。
+-----------+----------+----------+--------+---------+---------+------+
| client_id | account | category | t_year | t_month | amount | rank |
+-----------+----------+----------+--------+---------+---------+------+
| 100 | Checking | deposit | 2016 | 12 | 60.00 | 1 |
| 100 | Checking | deposit | 2017 | 1 | 140.00 | 1 |
| 100 | Checking | withdraw | 2016 | 12 | 300.24 | 1 |
| 100 | Checking | withdraw | 2016 | 12 | 2327.16 | 0 |
| 100 | Checking | Withdraw | 2017 | 1 | 216.73 | 1 |
| 100 | Saving | deposit | 2016 | 12 | 100.84 | 1 |
| 100 | Saving | deposit | 2016 | 12 | 4327.00 | 0 |
| 100 | Saving | Withdraw | 2016 | 12 | 2200.00 | 1 |
| 100 | Saving | Withdraw | 2017 | 1 | 1339.16 | 1 |
| 101 | Checking | Withdraw | 2017 | 1 | 80.00 | 1 |
| 101 | Checking | Withdraw | 2017 | 1 | 1033.45 | 0 |
| 101 | Checking | Withdraw | 2017 | 1 | 1261.72 | 0 |
| 101 | Saving | Withdraw | 2016 | 12 | 245.16 | 1 |
+-----------+----------+----------+--------+---------+---------+------+
この問題を解決するためのヒントを教えてください。
ありがとうございました。
で動作するように本当に難しいスキーマです。あなたの金額が+/-である場合、彼らの行動に応じて、預金金額が+で、引出し金額が - の方が良い場合がよくあります。これを整理するには多くのアプローチがありますが、[総勘定元帳](https://en.wikipedia.org/wiki/General_ledger)は開始するのに適しています。トランザクションにもネイティブの 'DATE'型を使用することをお勧めします。独立した月/年の列は非常に扱いにくいです。 – tadman
[MySQLのランク関数](http://stackoverflow.com/questions/3333665/rank-function-in-mysql)の可能な複製 – jordiburgos