gpt4 book ai didi

mysql - 我需要使用 group by 和 rollup 进行查询方面的帮助

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

我正在尝试显示图书订单数据,在第一列中显示订单年份,在第二列中显示月份。另外,还可以按月和按年显示总计以及总计。此外,显示消息“年度总计”和“总计”而不是空值。结果按年份和月份排序。

我不断收到错误(“字段列表”中的未知列“order_date”)任何人都可以帮助我吗?

 select coalesce(year(order_date), 'Grand Total') as Year
, case when year(order_date) is null then ' ' else coalesce(month(order_date), 'Year Total') end as
Month
, AmntDue
, NumberOfBooksPurch
from (
select year(order_date) as Year
, month(order_date) as Month
, sum(quantity * order_price) as AmntDue
, count(order_id) as NumberOfBooksPurch
from a_bkorders.order_headers
join a_bkorders.order_details using (order_id)
group by year(order_date), month(order_date), order_id with rollup
) tbl;

最佳答案

order_date 是原始表中的一个值,但子查询不会返回它,因此您无法在外部查询中引用它。使用子查询返回的别名:

select coalesce(Year, 'Grand Total') as Year
, case when Year is null then ' ' else coalesce(Month, 'Year Total') end as
Month
, AmntDue
, NumberOfBooksPurch
from (
select year(order_date) as Year
, month(order_date) as Month
, sum(quantity * order_price) as AmntDue
, count(order_id) as NumberOfBooksPurch
from a_bkorders.order_headers
join a_bkorders.order_details using (order_id)
group by Year, Month, order_id with rollup
) tbl;

关于mysql - 我需要使用 group by 和 rollup 进行查询方面的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20560082/

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