确定下订单最多的客户
Determine customer with most orders placed
我已经在 sql 开发人员中使用两个表来确定哪个客户下的订单最多我得到的订单总数可以计算出每个客户下的订单总数,但无法弄清楚如何只显示最大订单数的那个...
例如-
这将给我一个所有下订单的客户的列表以及他们每个人下的订单数量
SELECT
customer.cust_num, customer.cust_bizname,
COUNT(invoice.inv_num) AS "TOTAL ORDERS"
FROM customer INNER JOIN invoice ON customer.cust_num = invoice.cust_num
GROUP BY customer.cust_num, customer.cust_bizname;
如果我尝试使用最大值并一起计数...
SELECT
customer.cust_num, customer.cust_bizname,
MAX(COUNT(invoice.inv_num)) AS "TOTAL ORDERS"
FROM customer INNER JOIN invoice ON customer.cust_num = invoice.cust_num
GROUP BY customer.cust_num, customer.cust_bizname;
我收到一条错误消息 "not a single-group group function"。
如何有效统计订单数量,只显示下单次数最多的客户?
你不能那样使用 max
。相反,按降序排列并获取第一条记录,如下所示:
SELECT * FROM
(select customer.cust_num, customer.cust_bizname,
COUNT(invoice.inv_num) AS "TOTAL ORDERS"
FROM customer INNER JOIN invoice ON customer.cust_num = invoice.cust_num
GROUP BY customer.cust_num, customer.cust_bizname
ORDER BY "TOTAL ORDERS" desc)
WHERE rownum = 1
将 Order BY
与 ROWNUM
结合使用
select *
from
(SELECT customer.cust_num,
customer.cust_bizname,
COUNT(invoice.inv_num) AS "TOTAL ORDERS"
FROM customer
INNER JOIN invoice
ON customer.cust_num = invoice.cust_num
GROUP BY customer.cust_num, customer.cust_bizname
Order by "TOTAL ORDERS" DESC
)
Where ROWNUM =1
或使用Row_Number()
解析函数
select customer.cust_num,
customer.cust_bizname,
"TOTAL ORDERS"
FROM
(
SELECT Row_number() over(order by COUNT(invoice.inv_num) DESC) As RN
customer.cust_num,
customer.cust_bizname,
COUNT(invoice.inv_num) AS "TOTAL ORDERS"
FROM customer
INNER JOIN invoice
ON customer.cust_num = invoice.cust_num
GROUP BY customer.cust_num, customer.cust_bizname
)
Where RN=1
在 Oracle 12 中,您可以使用 ANSI 标准 fetch first 1 row only
:
SELECT c.cust_num, c.cust_bizname, COUNT(i.inv_num) AS "TOTAL ORDERS"
FROM customer c INNER JOIN
invoice i
ON c.cust_num = i.cust_num
GROUP BY c.cust_num, c.cust_bizname
ORDER BY COUNT(i.inv_num) DESC
FETCH FIRST 1 ROW ONLY;
我已经在 sql 开发人员中使用两个表来确定哪个客户下的订单最多我得到的订单总数可以计算出每个客户下的订单总数,但无法弄清楚如何只显示最大订单数的那个...
例如- 这将给我一个所有下订单的客户的列表以及他们每个人下的订单数量
SELECT
customer.cust_num, customer.cust_bizname,
COUNT(invoice.inv_num) AS "TOTAL ORDERS"
FROM customer INNER JOIN invoice ON customer.cust_num = invoice.cust_num
GROUP BY customer.cust_num, customer.cust_bizname;
如果我尝试使用最大值并一起计数...
SELECT
customer.cust_num, customer.cust_bizname,
MAX(COUNT(invoice.inv_num)) AS "TOTAL ORDERS"
FROM customer INNER JOIN invoice ON customer.cust_num = invoice.cust_num
GROUP BY customer.cust_num, customer.cust_bizname;
我收到一条错误消息 "not a single-group group function"。 如何有效统计订单数量,只显示下单次数最多的客户?
你不能那样使用 max
。相反,按降序排列并获取第一条记录,如下所示:
SELECT * FROM
(select customer.cust_num, customer.cust_bizname,
COUNT(invoice.inv_num) AS "TOTAL ORDERS"
FROM customer INNER JOIN invoice ON customer.cust_num = invoice.cust_num
GROUP BY customer.cust_num, customer.cust_bizname
ORDER BY "TOTAL ORDERS" desc)
WHERE rownum = 1
将 Order BY
与 ROWNUM
select *
from
(SELECT customer.cust_num,
customer.cust_bizname,
COUNT(invoice.inv_num) AS "TOTAL ORDERS"
FROM customer
INNER JOIN invoice
ON customer.cust_num = invoice.cust_num
GROUP BY customer.cust_num, customer.cust_bizname
Order by "TOTAL ORDERS" DESC
)
Where ROWNUM =1
或使用Row_Number()
解析函数
select customer.cust_num,
customer.cust_bizname,
"TOTAL ORDERS"
FROM
(
SELECT Row_number() over(order by COUNT(invoice.inv_num) DESC) As RN
customer.cust_num,
customer.cust_bizname,
COUNT(invoice.inv_num) AS "TOTAL ORDERS"
FROM customer
INNER JOIN invoice
ON customer.cust_num = invoice.cust_num
GROUP BY customer.cust_num, customer.cust_bizname
)
Where RN=1
在 Oracle 12 中,您可以使用 ANSI 标准 fetch first 1 row only
:
SELECT c.cust_num, c.cust_bizname, COUNT(i.inv_num) AS "TOTAL ORDERS"
FROM customer c INNER JOIN
invoice i
ON c.cust_num = i.cust_num
GROUP BY c.cust_num, c.cust_bizname
ORDER BY COUNT(i.inv_num) DESC
FETCH FIRST 1 ROW ONLY;