JQ聚合和交叉表 [英] JQ Aggregations and Crosstabs

查看:40
本文介绍了JQ聚合和交叉表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据:

[
  {
    "count": 4,
    "trial_status": "Follow up",
    "date": "2015-06-06"
  },
  {
    "count": 3,
    "trial_status": "Hold",
    "date": "2015-06-06"
  },
  {
    "count": 2,
    "trial_status": "Trial",
    "date": "2015-06-06"
  },
  {
    "count": 1,
    "trial_status": "Trial + Confirm",
    "date": "2015-06-06"
  }....

我想转换成

{"2015-06-06": {"Trial + Confirm": 1, "Follow Up": 4, "Hold": 3, "Trial": 2}}

我的任何方法都没有取得太大进展,因此非常感谢您的帮助.

I am not making much headway with any of my approaches so would really appreciate some help.

推荐答案

这应该有效:

group_by(.date) | map({
    key: .[0].date,
    value: map({
        key: .trial_status,
        value: .count
    }) | from_entries
}) | from_entries

关键是使用from_entries建立映射. 这是按名称设置属性"/键的唯一方法.您只需生成将构成对象的键/值对.

The key is to use from_entries to build up the mappings. This is the only way you can set up "properties"/keys by name. You just need to generate the key/value pairs that will make up the object.

圣地亚哥指出,您可以使用特殊语法按名称动态设置属性.这也可以.

Santiago points out that you can dynamically set properties by name with a special syntax. This would work too.

group_by(.date) | map({
    (.[0].date): map({
        (.trial_status): .count
    }) | add
}) | add

这篇关于JQ聚合和交叉表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆