- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
目前,我正在使用 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 |
+-----------+----------+----------+--------+---------+---------+------+
请给我解决这个问题的提示。
非常感谢。
最佳答案
尝试重新排列您的 Order by 语句,使您的“金额”成为 order by 中的第二个值。这将使您更接近您想要的。
按 client_id、金额、账户、类别、t_year、t_month 排序
您列出列的顺序将决定它们的排序顺序。
更多信息:https://dev.mysql.com/doc/refman/5.7/en/sorting-rows.html
关于mysql - 按多列创建从最小金额到最大金额的排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43217393/
我正在尝试创建一个可以识别金额(美元)的正则表达式。问题是数据是在扫描的 PDF 文件上通过 OCR 生成的,因此数据不精确: $可以用S表示 .可以表示为, 1可以用l或I表示 5可以用S表示 例子
在写输入用到input的时候,经常出现以下几种情况: 只能输入某。栗子:只能输入数字,只能输入字母(大写,小写)只能输入某固定格式。栗子:只能输入金额,只能输入小数且最多保留2位不能输入某。栗子:
我们正在开发旅游网站,用于预订航类、酒店、汽车等。它是基于产品的软件。客户(购买我们软件的)将成为“主要代理机构”。他的总交易将以 INR(印度卢比货币)为单位。航类、酒店或汽车预订总额仅以“印度卢比
以下操作有什么区别吗? (将当前日期提前 160 天) Calendar c = Calendar.getInstance(); c.add(Calendar.DAY_OF_WEEK,
假设有 5 个桶 (1 - 5),并且为每个桶分配一个(整数)值。例如 > bucket = 1:5 > value = c(14, 12, 9, 20, 7) > data.frame(bucket
我正在尝试使用 MYSQL 查询对每月和每年以及该月的总收入进行分组,我尝试了多次,但似乎总是出错。 我当前的查询: Month Year
我正在创建一个 PHP 表单,其中包含客户的信息和他们想要花费的金额,当点击提交按钮时,详细信息将发送到我的电子邮件地址。 这就是我遇到的问题。然后我想向他们发送指向 PayPal 帐户的链接以完成购
我想获取总交易量超过50000的用户 SELECT sum(tractions.amount) as total , user_id FROM `tractions` where `total`
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
我有一段固定的文本,每次文本进入滚动的 div 时我都会尝试添加不同的类。我用起来没问题。但是如果我向固定文本添加偏移量,例如 top: 400px 我需要在 JS 中抵消这个偏移量。但我似乎无法弄清
我有下表 create table supplier_paid_details( id bigint(10) NOT NULL AUTO_INCREMENT, payment_mode
我正在练习 java 银行帐户中的一些简单任务,需要有关代码块的建议,如何编写?这是一个例子,如果用户输入字符串或一些字母而不是数字来打印“请输入数字”,如何输入 Else if block amou
我正在使用 angularJS 在 mvc-5 中创建一个基于 Web 的应用程序,我在表中得到了金额总和 Total: {{totalAmount}} 我像这样从 Controller 获取金额 $
我的数据就是这样返回的。我需要返回列表中的所有值,确保时间格式和票价金额按照我的解释进行纠正。我想删除票价中的逗号以及出发和到达中的 AM & PM。提前谢谢了。因为大约有 3 个航类代码,总共有 1
我想计算一下 数量 * 比率 = 金额 金额 - 折扣 + 税费 = 账单金额 账单金额+四舍五入= Netty 我知道这很容易完成,但问题是任何人都可以通过检查元素更改该值。为了阻止这种情况,我必须
基本上,我正在 LINQ 中寻找一种方法来选择列表中的第一个(比如说 3 个)分组对象。 例如,列表可能包含: {“AAA”、“AAA”、“AAA”、“AAA”、“BBB”、“BBB”、“CCC”、“
我正在尝试按类别对金额进行求和,但存在基于引用编号的重复金额,并且我只想为每个引用包含 1 个金额。大约有100K个不同的引用号,全线有4个差异量。 我正在分析的数据如下所示: reference |
我有一个表,每个 user_id 有很多行 我正在尝试按 user_id 对行进行分组并对它们的金额进行求和 这是表结构 Name Type Collation Attributes
如何查询实际金额 1.00 以内的金额列? 例如,如果 AmountPaid = 7.75,我想返回 Amount 在 6.75 - 8.75 之间的所有结果。 我知道我忽略了一些简单的事情,但到目前
我是 C# 的新手,正在为我尝试创建的程序而苦苦挣扎。我希望我能尽我所能提出这个问题。根据我的任务,我们将在 Visual Basic 中创建一个用于创建帐户的 Windows 窗体。出于我的问题的目
我是一名优秀的程序员,十分优秀!