【发布时间】:2014-01-01 00:22:49
【问题描述】:
有谁知道为什么我无法在此查询中对TotalSales 进行分组,如果可以,我该如何解决:
select coalesce(Author_ID, 'All Authors') as Author_ID
, case when Author_ID is null then ' ' else coalesce(Book_ID, 'All Books') end as Book_ID
, TotalQuantity
, coalesce(TotalSales, 'No Sales') as TotalSales
from (
select author_id as Author_ID
, book_id as Book_ID
, sum(quantity) as TotalQuantity
, sum(quantity * order_price) as TotalSales
from a_bkinfo.book_authors
join a_bkorders.order_details using (book_id)
where author_sequence = 1
group by Author_id, Book_ID, TotalSales with rollup
) tbl;
当作者没有图书销售时,我想在 TotalSales 下包含“无销售”。这是更新的版本。我不肯定这是正确的,但我确实有输出似乎可以解决问题。这里是:
select coalesce(Author_ID, 'All Authors') as Author_ID
, case when Author_ID is null then ' ' else coalesce(Book_ID, 'All Books') end as Book_ID
, NumOrders
, coalesce(TotalSales, 'No Sales') as TotalSales
from ( select author_id as Author_ID
, book_id as Book_ID
, count(Distinct order_id) AS NumOrders
,(Select sum(quantity * order_price) from a_bkorders.order_details) as TotalSales
from a_bkorders.order_headers
join a_bkorders.order_details using (order_id)
join a_bkinfo.book_authors using (book_id)
where author_sequence = 1
group by Author_ID, Book_ID, TotalSales with rollup) tbl;
【问题讨论】:
-
看起来您不需要在
GROUP BY中包含TotalSales。查看您的查询,这没有任何意义。只需从内部选择中删除它即可。 -
您能进一步解释一下吗?我在创建子查询时遇到问题。我应该将此查询放在外部查询中吗??