Czy ktoś wie dlaczego nie jestem w stanie do grupy TotalSales
w tym zapytaniu, a jeśli tak, w jaki sposób można to naprawić:Klauzula GROUP BY z alisem?
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;
Chciałem to „nie sprzedaży” pod TotalSales gdy autor nie prowadzi sprzedaży książek. Oto zaktualizowana wersja. Nie jestem pewien, że to jest poprawne, ale mam wyjście, które wydaje się rozwiązać problem. Oto ona:
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;
Wygląda na to, że nie musisz uwzględniać opcji 'TotalSales' w' GROUP BY'. Patrząc na twoje zapytanie, to po prostu nie ma sensu. Po prostu zrzuć go z wewnętrznego wyboru. – peterm
Czy możesz wyjaśnić dalej? Mam problem z utworzeniem podzapytania. Czy powinienem umieścić to zapytanie wewnątrz zewnętrznego zapytania? – user3098749