根據訂購日期獲取每個客戶的最后三個最新訂單金額,并按升序排列客戶名稱并獲取前 15 條記錄。
table name: order_table
columns:
order_id
ordered_date
cust_name
product
order_amount
給定資料:
160001 01/01/2020 Richard Books 1120
160008 01/02/2020 Richard Chairs 1340
160016 01/03/2020 Richard Computer 1299
160007 01/04/2020 Richard Desk 2929
160013 03/01/2020 Benny Monitor 1149
160010 03/02/2020 Benny Other Electronics 1449
160004 03/03/2020 Benny Printer 2399
160012 03/04/2020 Benny Software 3599
160017 01/02/2020 Ram Speakers 5110
160023 02/03/2020 Ram Bookcases 2330
所需的樣本輸出:
Benny 3599 2399 1449
Ram 2330 5110
richard 2929 1299 1340
我的代碼:
SELECT
order_table.cust_name,
(SELECT
MAX(order_table.ordered_date)
FROM
order_table
GROUP BY order_table.cust_name
ORDER BY ordered_date DESC
LIMIT 15)
FROM
order_table
ORDER BY ordered_date DESC
LIMIT 15*/
SELECT
MAX(order_table.ordered_date)
FROM
order_table
GROUP BY order_table.cust_name
ORDER BY ordered_date DESC
LIMIT 15;
如何處理此類問題陳述并將其分解以解決此問題?
uj5u.com熱心網友回復:
這應該給出答案,但不是以規定的格式,任何獲得所需輸出的幫助都將受到高度贊賞。
select cust_name,order_amount from order_table where (
select count(*) from order_table as c
where c.cust_name =order_table.cust_name and
c.ordered_date<=order_table.ordered_date
)<=3 order by cust_name, ordered_date desc ;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/334339.html
