我有这张桌子,我需要用max(balance)
打印Account_NO
,但我无法弄清楚如何获得我想要的结果。
+-------------+------------+--------+------+
| CUST_ID | ACCOUNT_NO |BALANCE |STATUS|
+-------------+------------+--------+------+
| 6363251425 | 40 | 110000 | A |
+-------------+------------+--------+------+
| 6262521111 | 22 | 51000 | D |
+-------------+------------+--------+------+
| 1111254215 | 19 | 504211 | A |
+-------------+------------+--------+------+
| 6363251425 | 99 | 3620 | A |
+-------------+------------+--------+------+
| 9999999999 | 31 | 1000 | A |
+-------------+------------+--------+------+
| 6363251425 | 47 | 3000 | D |
+-------------+------------+--------+------+
| 9999999999 | 23 | 303333 | D |
+-------------+------------+--------+------+
| 1111254215 | 45 | 85424 | A |
+-------------+------------+--------+------+
| 9999999999 | 49 | 632555 | A |
+-------------+------------+--------+------+
所以结果就像那样
+-----------------+------------+
| MAX(BALANCE) | ACCOUNT_NO |
+-----------------+------------+
| 632555 | 49 |
+-----------------+------------+
我怎样才能得到这个结果?
答案 0 :(得分:1)
您可以使用子查询将表连接到自身以获取max(Balance)
并使用该值获取Account_No
:
select t1.Balance, t1.Account_No
from <table> t1
inner join
(
select max(Balance) as MaxBalance
from <table>
) t2
on t1.Balance = t2.MaxBalance
这是demo。通过使用子查询,您可以使用聚合函数max()来获得最高余额,然后将其连接回表以获取关联帐户。
答案 1 :(得分:0)
从内部查询中获取最大值,然后将其与原始表连接以获取相应的帐号将可以解决问题。如果您的多个帐户具有相同的最大值,您将同时在最终输出中进行帐户
试试这个: -
Select a.ACCOUNT_NO, a.BALANCE AS MAX_BALANCE
from
YOUR_TABLE_NAME a
inner join
(
Select max(BALANCE) as max_balance
from
YOUR_TABLE_NAME
) b
on a.BALANCE=b.max_balance
我的输出
ACCOUNT_NO MAX_BALANCE
49 632555
如果您有任何问题,请告诉我