- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 reviews
表中有以下数据用于某些项目,使用范围从 0 到 100 的评分系统
+-----------+---------+-------+
| review_id | item_id | score |
+-----------+---------+-------+
| 1 | 1 | 90 |
+-----------+---------+-------+
| 2 | 1 | 40 |
+-----------+---------+-------+
| 3 | 1 | 10 |
+-----------+---------+-------+
| 4 | 2 | 90 |
+-----------+---------+-------+
| 5 | 2 | 90 |
+-----------+---------+-------+
| 6 | 2 | 70 |
+-----------+---------+-------+
| 7 | 3 | 80 |
+-----------+---------+-------+
| 8 | 3 | 80 |
+-----------+---------+-------+
| 9 | 3 | 80 |
+-----------+---------+-------+
| 10 | 3 | 80 |
+-----------+---------+-------+
| 11 | 4 | 10 |
+-----------+---------+-------+
| 12 | 4 | 30 |
+-----------+---------+-------+
| 13 | 4 | 50 |
+-----------+---------+-------+
| 14 | 4 | 80 |
+-----------+---------+-------+
我正在尝试创建一个 bin 大小为 5 的分值直方图。我的目标是为每个项目生成一个直方图。为了创建整个表的直方图,可以使用 width_bucket
.这也可以调整为在每个项目的基础上运行:
SELECT item_id, g.n as bucket, COUNT(m.score) as count
FROM generate_series(1, 5) g(n) LEFT JOIN
review as m
ON width_bucket(score, 0, 100, 4) = g.n
GROUP BY item_id, g.n
ORDER BY item_id, g.n;
但是,结果是这样的:
+---------+--------+-------+
| item_id | bucket | count |
+---------+--------+-------+
| 1 | 5 | 1 |
+---------+--------+-------+
| 1 | 3 | 1 |
+---------+--------+-------+
| 1 | 1 | 1 |
+---------+--------+-------+
| 2 | 5 | 2 |
+---------+--------+-------+
| 2 | 4 | 2 |
+---------+--------+-------+
| 3 | 4 | 4 |
+---------+--------+-------+
| 4 | 1 | 1 |
+---------+--------+-------+
| 4 | 2 | 1 |
+---------+--------+-------+
| 4 | 3 | 1 |
+---------+--------+-------+
| 4 | 4 | 1 |
+---------+--------+-------+
也就是说,不包括没有条目的箱子。虽然我发现这不是一个糟糕的解决方案,但我宁愿拥有所有存储桶,而没有条目的存储桶为 0。更好的是,使用这种结构:
+---------+----------+----------+----------+----------+----------+
| item_id | bucket_1 | bucket_2 | bucket_3 | bucket_4 | bucket_5 |
+---------+----------+----------+----------+----------+----------+
| 1 | 1 | 0 | 1 | 0 | 1 |
+---------+----------+----------+----------+----------+----------+
| 2 | 0 | 0 | 0 | 2 | 2 |
+---------+----------+----------+----------+----------+----------+
| 3 | 0 | 0 | 0 | 4 | 0 |
+---------+----------+----------+----------+----------+----------+
| 4 | 1 | 1 | 1 | 1 | 0 |
+---------+----------+----------+----------+----------+----------+
我更喜欢这种解决方案,因为它对每个项目使用一行(而不是 5n
),这样查询起来更简单,并且可以最大限度地减少内存消耗和数据传输成本。我目前的做法如下:
select item_id,
(sum(case when score >= 0 and score <= 19 then 1 else 0 end)) as bucket_1,
(sum(case when score >= 20 and score <= 39 then 1 else 0 end)) as bucket_2,
(sum(case when score >= 40 and score <= 59 then 1 else 0 end)) as bucket_3,
(sum(case when score >= 60 and score <= 79 then 1 else 0 end)) as bucket_4,
(sum(case when score >= 80 and score <= 100 then 1 else 0 end)) as bucket_5
from review;
虽然这个查询满足了我的要求,但我很好奇是否有更优雅的方法。如此多的 case
语句不容易阅读,并且 bin 标准的更改可能需要更新每个总和。此外,我很好奇此查询可能存在的潜在性能问题。
最佳答案
第二个查询可以重写为使用 ranges使编辑和编写查询更容易一些:
with buckets (b1, b2, b3, b4, b5) as (
values (
int4range(0, 20), int4range(20, 40), int4range(40, 60), int4range(60, 80), int4range(80, 100)
)
)
select item_id,
count(*) filter (where b1 @> score) as bucket_1,
count(*) filter (where b2 @> score) as bucket_2,
count(*) filter (where b3 @> score) as bucket_3,
count(*) filter (where b4 @> score) as bucket_4,
count(*) filter (where b5 @> score) as bucket_5
from review
cross join buckets
group by item_id
order by item_id;
用 int4range(0,20)
构造的范围包括下限并排除上限。
CTE named buckets
仅创建一行,因此交叉连接不会更改 review
表中的行数。
关于postgresql - 生成按列分组的值的直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51384531/
我使用 为 .dat 文件中的一些数据创建了直方图 binwidth=... bin(x,width)=width*floor(x/width) plot 'file' using (bin($1,b
我需要能够在单个直方图上显示多条线,其中每条线都应该由一个参数表示。我有多个服务器,我想监控它们的 CPU 使用率。我的 Kibana 输入数据如下所示: 时间戳 |机器 |姓名 |值(value)
我在 Elasticsearch 中有一个索引,它包含一个简单对象数组(键值,请参见下面的示例)。 文档有时间戳。 我可以在 Kibana 中为每个键值创建单独的直方图(即一个用于 bytes_sen
所以我想出了如何将我的数据下钻到频率表 - Overall.Cond Freq 235 1 0 236 2 0 237
我的目标是使用 gnuplot 5.4 框创建直方图,并用特定的 RGB 值对每个框进行着色(出于测试目的,它是“绿色”,但在最终数据集中将是 #RRGGBB) 我的数据如下所示: 5.800507
我有 chr totgenes FST>0.4 %FST>0.4 exFST>0.4 %exFST>0.4 inFST>0.4 %inFST>0.4 chrtotlen 1 14
我用 matplotlib 创建了一个直方图使用 pyplot.hist()功能。我想在条形图中添加 bin 高度 ( sqrt(binheight) ) 的毒物误差平方根。我怎样才能做到这一点? .
我有兴趣在 R 中创建一个包含两个(或更多)人口的直方图,这意味着 - 我不希望两个直方图共享同一个图形,而是一个包含两种或更多颜色的条形图。 找到下面的图片 - 这就是我想要完成的。 有什么想法吗?
所以,我需要按日期制作数据直方图,但我有 xticlabel 重叠的问题,所以,我试图找到一个解决方案,如何跳过 xtics 以避免重叠。考虑到日期不是整数抽动,我试图以这种方式解决它: .dat 文
给定每小时都有数据点的(电力)市场数据的时间序列,我想显示一个包含每小时数据的所有时间/时间范围平均值的条形图,以便分析师可以轻松地将实际价格与所有时间平均值进行比较(一天中哪个小时最贵/最便宜)。
+----+----+--------+ | Id | M1 | trx | +----+----+--------+ | 1 | M1 | 11.35 | | 2 | M1 | 3.4
所以,我需要按日期制作数据直方图,但我有 xticlabel 重叠的问题,所以,我试图找到一个解决方案,如何跳过 xtics 以避免重叠。考虑到日期不是整数抽动,我试图以这种方式解决它: .dat 文
我有以下示例数据文件,我想在 gnuplot 中将其绘制为直方图 1 1 2 2 4 3 我正在使用以下命令绘制数据:用方框绘制“sample.data”,生成以下图表: ##
我是 Java 编码新手,我正在尝试使用提供给我的以下方法创建直方图。这些注释是对每个方法的说明,稍后我们将使用它们来创建主方法并打印直方图。我已经达到了方法 3,并且能够很好地编译所有内容,但我不确
我有一个由服务器上的程序生成的连续生成的数据(文本文件)。我想将数据绘制为实时图表,就像 powergrid做。这是我的方法: 由于数据是在服务器上以文本文件的形式连续生成的,因此我编写了一个 PHP
我正在尝试通过一个函数使用 D3 创建一个简单的直方图。图表的 y 值作为数组传递给函数,然后函数创建 svg 和条形图。我得到了正确的轴,但条被切断了。 似乎我的矩形 x 值太大而无法放入 svg
有没有办法用 linq 做一个分段直方图?我见过几个示例,您可以在其中计算特定对象的出现次数。是否可以创建一个基于 linq 的直方图来计算两个值之间的一系列对象的出现次数? 我不知道您将如何按一系列
我正在参加初级 Java 类(class),任务是创建一个具有以下输出的直方图程序:(100 和 10 是用户输入)。 有多少个数字? 100 间隔多少? 10 Histogram ---------
如何使用 corePlot 实现直方图。实际上,我正在尝试使用条形图。 在条形图中是否有任何选项可以对我的值进行分组。例如:所以我只能打印 3 条。这样值应该像这样分组: X 0...5: B
我有一个简单的数据集,其中脚本需要时间来完成各个步骤。时间是不可预测的,但主要分组在特定的时间范围内,但我想以十分之一秒的分组来绘制图表。 (我知道这很奇怪,这是一些报告可视化内容的要求)。我可以将我
我是一名优秀的程序员,十分优秀!