【问题标题】:Creating a rank from the smallest amount to the largest amount group by multiple columns通过多列创建从最小数量到最大数量组的排名
【发布时间】:2017-04-04 20:46:20
【问题描述】:

目前,我使用的是 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 |
+----+-----------+----------+----------+--------+---------+-------+---------+

我需要按客户 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    |
+-----------+----------+----------+--------+---------+---------+------+

请给我提示来解决这个问题。

非常感谢。

【问题讨论】:

  • 这是一个非常难以使用的架构。如果您的金额都是 +/- 取决于他们的操作,通常会更好,其中存款为 +,取款为 -。有很多方法可以组织这个,但general ledger 是一个很好的起点。将本机 DATE 类型也用于事务也是一个好主意。独立的月/年列使用起来非常令人讨厌。
  • Rank function in MySQL的可能重复

标签: mysql sql group-by rank


【解决方案1】:

尝试重新排列您的 Order by 语句,使您的“金额”成为 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

【讨论】:

  • 感谢您的帮助。我试过了,但它不起作用。结果是一样的
【解决方案2】:

考虑一个用于排名的相关计数聚合子查询:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 2021-03-12
    • 2012-07-14
    • 2014-09-14
    相关资源
    最近更新 更多