努力将数据集从行旋转到列
我有一个数据集,其中包含表示同一实体的不同统计数据的多个记录。
示例:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这将重现您列出的预期结果:
如果您需要处理各种指标,您可能必须使用 动态 SQL。您需要获取唯一的指标值列表(除非您有外键引用的类型代码表):
然后,在执行之前连接每个指标的
SUM(CASE ...
询问。This will reproduce the expected result you listed:
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):
Then, concatenate the
SUM(CASE ...
for each metric before executing the query.