作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用宝藏数据进行数据分析,并且在presto db中遇到合并声明有麻烦。
我如何做一个工会全部。我不了解文档。每当我尝试像这样做UNION时:
SELECT
COUNT(*) AS ReservationsCreated,
resource
FROM
reservation
WHERE
type = 'create'
UNION
SELECT
COUNT(*) AS ReservationsDeleted,
resource
FROM
reservation
WHERE
type = 'delete'
GROUP BY
resource
;
SELECT
COUNT(*) AS ReservationsCreated,
resource
FROM
reservation
WHERE
type = 'create'
UNION
SELECT
COUNT(*) AS ReservationsDeleted,
resource
FROM
reservation
WHERE
type = 'delete'
GROUP BY
resource
;
'"resource"' must be an aggregate expression or appear in GROUP BY clause
最佳答案
如错误所述,查询的第一部分缺少group by
。
SELECT COUNT(*) AS ReservationsCreated, resource
FROM reservation
WHERE type = 'create'
group by resource
UNION ALL
SELECT COUNT(*) AS ReservationsDeleted, resource
FROM reservation
WHERE type = 'delete'
GROUP BY resource
select
resource
,sum(case when type = 'create' then 1 else 0 end) as reservationscreated
,sum(case when type = 'delete' then 1 else 0 end) as reservationsdeleted
from reservation
group by resource
关于sql - Presto上的UNION ALL/UNION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33660135/
我是一名优秀的程序员,十分优秀!