PostgreSQL查询:在分组之前,使用json字段筛选条目
我创建了一个具有以下模式的Postgres表PostgreSQL查询:在分组之前,使用json字段筛选条目,sql,postgresql,Sql,Postgresql,我创建了一个具有以下模式的Postgres表 create table exam_info ( "venue" text not null, //value: "A", "B", "C", "D" "time" int, //epoch time "examData" jsonb not null ); 下
create table exam_info
(
"venue" text not null, //value: "A", "B", "C", "D"
"time" int, //epoch time
"examData" jsonb not null
);
下面是examData列的一些示例条目
{"id":"e73kf", "data":{"subject":"Science","year":"grade5"}}
{"id":"e3dsa", "data":{"subject":"English","year":"grade3"}}
我想知道,对于一门特定的学科,在特定的时间范围内,每个地点举办了多少次考试
subject venue count
"Science" A 10
"Science" B 5
"Science" C 7
"Science" D 11
当我试图在下面的查询中添加一个条件,其中exam=Science时,出现了一个错误列exam not exist
SELECT
"examData" -> 'data' -> 'subject' as exam,
venue,
count(venue)
FROM
exam_info
WHERE
time >= ' ' AND time < ' '
GROUP BY
exam,venue
谢谢大家 在评论中,您指的是与原始问题不同的另一个问题, 无论如何,您不能在where子句中引用EXAME,因为您不能在where子句中使用列别名,但您可以通过构建列的方式引用它:
SELECT
"examData" -> 'data' -> 'subject' as exam,
venue,
count(venue)
FROM
exam_info
WHERE
time >= ' ' AND time < ' '
and "examData" -> 'data' -> 'subject' ='"Math"' -- <--
GROUP BY
exam,venue
你们组的考试栏目是什么?您的表中没有exam列,请删除它,这样您的查询就可以正常工作了。您在“选择”部分中选择的所有“考试、地点”列名均可在分组中使用注意:thta计数*将略快于countvenue@eshirvana检查SELECT部分,我已经将json字段主题定义为exam。查询本身运行良好,错误仅在我想要添加条件时显示,其中exam=Science是的,查询本身运行良好,错误仅在我想要添加条件时显示,其中exam=Science,我想知道如何将此条件添加到上述查询中。谢谢!现在开始工作了@欢迎光临