我有一个 Postgres 数据库表foo
,其中有一个score
范围为 0 - 10的列。我想要一个查询来返回分数总数、0 到 3 之间的分数数、4 之间的分数数和 6,以及 7 到 10 之间的分数。类似于以下内容:
SELECT
COUNT(*) as total,
COUNT(
SELECT * from foo where score between 0 and 3;
) as low,
COUNT(
SELECT * from foo where score between 4 and 6;
) as mid,
COUNT(
SELECT * from foo where score between 7 and 10;
) as high
FROM foo;
Run Code Online (Sandbox Code Playgroud)
我试过这个,但SELECT
在COUNT
语句中出现错误。任何想法我怎么能做到这一点?我确定 Postgres 中有一个超级简单的方法。我只是想不出 Google 的正确术语。
我需要从 3 个表中提取数据,并以特定方式打印它。
http://sqlfiddle.com/#!15/59481/8
请参阅此问题以了解查询是如何生成的。小提琴中的版本部分工作,但我需要修改查询以产生不同的输出。
我有一个project_report
存储数据的表keyword
:
1. If a keyword has data for a particular day it will store it in `project_report`
2. A keyword might have no data to store for a particular day.
3. A keyword might store multiple rows of data for a particular day (the data is uniq)
4. A keyword might not have data for a particular day, but it might have for a different day.
Run Code Online (Sandbox Code Playgroud)
我相信要解决我的问题,DB 函数将是最好的方法,因为我希望能够指定一个日期并从project_reports …