2017-04-04 68 views
2

目前,我正在使用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 

第二個嘗試:

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 | 
+-----------+----------+----------+--------+---------+---------+------+ 

請給我提示,以解決這個問題。

非常感謝。

+2

這是一個非常困難的模式進行工作。如果你的金額都是+/-,取決於他們的行爲,存款是+和撤回是 - 通常更好。有很多方法來組織這個,但[總帳](https://en.wikipedia.org/wiki/General_ledger)是一個很好的開始。對於事務使用本地'DATE'類型也是一個好主意。獨立的月/年專欄非常令人討厭。 – tadman

+0

[MySQL中的等級函數]的可能重複(http://stackoverflow.com/questions/3333665/rank-function-in-mysql) – jordiburgos

回答

0

考慮一個相關的計數聚合子查詢的排名:

SELECT t.client_id, t.account, t.category, t.t_year, t.t_month, t.amount, 
     (SELECT COUNT(*) FROM temp_work sub 
     WHERE sub.amount <= t.amount 
     AND sub.client_id = t.client_id 
     AND sub.account = t.account 
     AND sub.t_year = t.t_year 
     AND sub.t_month = t.t_Month) AS rank 
FROM temp_work t 
ORDER BY t.client_id, t.account, t.category, t.t_year, t.t_month, t.amount 
+0

美麗!它完美的工作! –

1

嘗試重新排列您的Order by語句以使您的「金額」爲訂單中的第二個值。這會讓你更接近你想要的東西。

Order by client_id, amount, account, category, t_year, t_month

您列出您的列將決定它們的排列順序的順序。

欲瞭解更多信息:https://dev.mysql.com/doc/refman/5.7/en/sorting-rows.html

+0

謝謝你的幫助。我試過了,但它不起作用。結果是一樣的 –