【发布时间】:2020-08-05 12:49:01
【问题描述】:
我正在尝试使用 SUM 函数在 PostgresQL 中进行查询,以获得总共 3 种不同的行类型(根、动态、测试)。我第一次尝试使用 Sum() 函数,第二次尝试使用 Count() 函数;可悲的是,两者都没有工作。我预计会出现语法错误(因为我是使用 SQL 的初学者),但我不确定它是什么以及如何修复它!
第一次尝试 Sum() 函数:
SELECT
sum(case when "exerciseType" = 'ROOT') as total_root_exercises,
sum(case when "exerciseType" = 'DYNAMIC') as total_dynamic_exercises,
sum(case when "exerciseType" = 'TEST') as total_test_exercises
FROM exer
GROUP BY "exerciseType"
第二次尝试使用 Count() 函数:
select
count(*) as total_root_exercises
where "exerciseType" = 'ROOT',
count(*) as total_Dynamic_exercises
where "exerciseType" in('DYNAMIC'),
count(*) as total_test_exercises
where "exerciseType" in('TEST')
FROM exer
我可以在这方面寻求帮助吗?谢谢你:)
【问题讨论】:
-
通常情况下与docs一起使用。提供的答案是它的替代方案。
标签: sql postgresql count sum pivot