表达式不在GROUP BY键中

时间:2018-02-20 09:23:34

标签: sql hadoop hive hiveql

您好我尝试运行此查询:

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和类型,但我试图计算比率但没有成功。

1 个答案:

答案 0 :(得分:-1)

;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