嗨,我尝试运行此查询:
select
count(id) as Total_books_in_store,
sum(case when type in ('sports', 'music', 'history') then 1 else 0 end) AS Total_sold,
(Total_sold/Total_books_in_store) as Sales_rate
from store
group by total_in_store
当我运行此查询时,它给了我错误:第5:1行表达式不在GROUP BY键中
我有表的ID和类型,但是我尝试计算比率没有成功。
最佳答案
;WITH Calculation AS(
SELECT
count(id) as Total_books_in_store,
sum(case when type in ('sports', 'music', 'history') then 1 else 0 end) AS Total_sold
from @Store
group by total_in_store
)
SELECT
Total_books_in_store,
Total_sold,
(Total_sold/Total_books_in_store) as Sales_rate
FROM Calculation
关于sql - 表达式不在GROUP BY键中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48881716/