努力将数据集从行旋转到列

发布于 2024-10-25 20:21:43 字数 1036 浏览 1 评论 0原文

我有一个数据集,其中包含表示同一实体的不同统计数据的多个记录。

示例:

DEVICE  METRIC           SCORE   WHEN
-------------------------------------------------
devA    reads_per_sec    100    2011-03-01 12:00:00 AM
devA    writes_per_sec   50     2011-03-01 12:00:00 AM
devA    total_per_sec    150    2011-03-01 12:00:00 AM
devB    reads_per_sec    200    2011-03-01 12:00:00 AM
devB    writes_per_sec   50     2011-03-01 12:00:00 AM
devB    total_per_sec    250    2011-03-01 12:00:00 AM
devC    reads_per_sec    300    2011-03-01 12:00:00 AM
...

我想将这个表中的所有内容转移到一个具有这种结构的新表中,

DEVICE  READS  WRITES  TOTAL  WHEN
--------------------------------------------------
devA    100    50      150    2011-03-01 12:00:00 AM
devB    200    50      250    2011-03-01 12:00:00 AM
devC    300    50      150    2011-03-01 12:00:00 AM
...

对于来自数千个时间戳的数千台设备中的每台设备,我都有十多个“指标”。

如果我可以在每个样本周期查看设备的一条记录,那么查询以查看给定设备在给定时间的所有指标分数将会容易得多。

所以,我的问题是,处理和执行此操作的最佳方法是什么?我并不是要要求任何人为我做我的工作,但这超出了我对数据库的了解。

I have a dataset that has mutliple records representing different stats for the same entities.

Example:

DEVICE  METRIC           SCORE   WHEN
-------------------------------------------------
devA    reads_per_sec    100    2011-03-01 12:00:00 AM
devA    writes_per_sec   50     2011-03-01 12:00:00 AM
devA    total_per_sec    150    2011-03-01 12:00:00 AM
devB    reads_per_sec    200    2011-03-01 12:00:00 AM
devB    writes_per_sec   50     2011-03-01 12:00:00 AM
devB    total_per_sec    250    2011-03-01 12:00:00 AM
devC    reads_per_sec    300    2011-03-01 12:00:00 AM
...

I want to pivot everything from this table to a new table that will have this structure

DEVICE  READS  WRITES  TOTAL  WHEN
--------------------------------------------------
devA    100    50      150    2011-03-01 12:00:00 AM
devB    200    50      250    2011-03-01 12:00:00 AM
devC    300    50      150    2011-03-01 12:00:00 AM
...

I have well over a dozen 'metrics' for each of thousands of devices from thousands of timestamps.

It will be far easier to query to see all metric scores for a given device on a given time if I can look at one record for a device for each sample period.

So, my question is, what is the best way to approach this and perform it? I don't mean to ask anyone to do my work for me, but this is beyond my knowledge of databases.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖兔^ω^ 2024-11-01 20:21:43

这将重现您列出的预期结果:

  SELECT t.device,
         SUM(CASE WHEN t.metric = 'reads_per_sec' THEN t.score ELSE 0 END) AS reads,
         SUM(CASE WHEN t.metric = 'writes_per_sec' THEN t.score ELSE 0 END) AS writes,
         SUM(CASE WHEN t.metric = 'total_per_sec' THEN t.score ELSE 0 END) AS total,
         t.when
    FROM YOUR_TABLE t
GROUP BY t.device, t.when
ORDER BY t.device, t.when

如果您需要处理各种指标,您可能必须使用 动态 SQL。您需要获取唯一的指标值列表(除非您有外键引用的类型代码表):

SELECT DISTINCT t.metric
  FROM YOUR_TABLE t

然后,在执行之前连接每个指标的 SUM(CASE ...询问。

This will reproduce the expected result you listed:

  SELECT t.device,
         SUM(CASE WHEN t.metric = 'reads_per_sec' THEN t.score ELSE 0 END) AS reads,
         SUM(CASE WHEN t.metric = 'writes_per_sec' THEN t.score ELSE 0 END) AS writes,
         SUM(CASE WHEN t.metric = 'total_per_sec' THEN t.score ELSE 0 END) AS total,
         t.when
    FROM YOUR_TABLE t
GROUP BY t.device, t.when
ORDER BY t.device, t.when

If you need to deal with the various metrics, you're likely going to have to use dynamic SQL. You'd need to get a unique list of metric values (unless you have a type code table for a foreign key reference):

SELECT DISTINCT t.metric
  FROM YOUR_TABLE t

Then, concatenate the SUM(CASE ... for each metric before executing the query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文