gpt4 book ai didi

mysql - 按 SQL 产生的收入金额对月份进行排名

转载 作者:行者123 更新时间:2023-11-29 16:38:16 25 4
gpt4 key购买 nike

我正在使用的两个表是

Orderdetails
ordernumber, productnumber, productcode, quantityordered, priceEach

Orders
ordernumber, orderdate (labeled as yyyy-mm-dd)

我理解将收入 Sum(QuantityOrdered * PriceEach) 作为 TotalRevenue

难以按月分组并连接两个表。

谢谢

最佳答案

在标准 SQL 中,您会这样做:

select extract(year from o.orderdate) as yyyy,
extract(month from o.orderdate) as mm,
sum(ol.quantityOrdered * ol.priceEach) as total
from orders o join
orderlines ol
on o.ordernumber = ol.ordernumber
group by extract(year from o.orderdate), extract(month from o.orderdate)
order by yyyy, mm;

众所周知,数据库对日期/时间函数的支持多种多样。许多支持更简单的 year()month() 函数。许多人支持其他机制来完成同样的事情。

在 MySQL 中,您可以使用 year()month() 函数:

select year(o.orderdate) as yyyy,
month(o.orderdate) as mm,
sum(ol.quantityOrdered * ol.priceEach) as total
from orders o join
orderlines ol
on o.ordernumber = ol.ordernumber
group by year(o.orderdate),
order by yyyy, mm;

关于mysql - 按 SQL 产生的收入金额对月份进行排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53472023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com