简体   繁体   中英

SELECT the customer who has ordered the greatest quantity of Products?

I'm trying to make query to find the Customer who has ordered the greatest quantity of Products from the following table!

mysql> select * from ORDERS;
+---------+---------+------------+-----+
| CUSTNUM | PRODNUM | DATE       | QTY |
+---------+---------+------------+-----+
|  125216 |    2323 | 2016-03-21 |   2 |
|  136101 |    2357 | 2016-03-21 |   5 |
|  136101 |    2357 | 2016-10-12 |   1 |
|  136101 |    2357 | 2016-11-25 |   5 |
|  136101 |    3737 | 2016-10-12 |  10 |
|  136101 |    9193 | 2016-11-25 |   5 |
|  182764 |    2357 | 2015-03-21 |  12 |
|  182764 |    2357 | 2016-05-12 |  10 |
|  212836 |    3737 | 2015-09-16 |   6 |
|  455566 |    4143 | 2016-02-09 |  10 |
|  455566 |    4143 | 2016-05-12 |  10 |
+---------+---------+------------+-----+

expected result

+-------------+------------------+
| CUSTNUM     | quantity_ordered |
+-------------+------------------+
| 136101      |               26 |
+-------------+------------------+

Thanks in advance for help.

Use group by clause.

For more info, Please refer some tutorials

Or read the official docs

SELECT  CUSTNUM, SUM(QTY) s FROM ORDERS GROUP BY CUSTNUM 
ORDER BY s DESC LIMIT 1 

SQLfiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM