简体   繁体   English

在Json对象中使用JSON1和SQLite3执行聚合

[英]Perform Aggregations using JSON1 and SQLite3 in Json Objects

I just started using SQLite 3 with JSON1 support. 我刚刚开始使用具有JSON1支持的SQLite 3。

I already have created a database that consists of several attributes. 我已经创建了一个包含几个属性的数据库。 One of these attributes is a json object. 这些属性之一是json对象。

What I want to do is perform aggregations within this object. 我想做的是在此对象内执行聚合。

Running the following query: 运行以下查询:

select json_extract(report, '$.some_attribute')
from table1
group by var1
having date == max(date);

returns the following: 返回以下内容:

[{"count":55,"label":"A"},{"count":99,"label":"B"}, {"count":1,"label":"C"}]
[{"count":29,"label":"A"},{"count":285,"label":"B"},{"count":461,"label":"C"}]
[{"count":6642,"label":"A"},{"count":24859,"label":"B"},{"count":3031,"label":"C"}]
[{"count":489,"label":"A"},{"count":250,"label":"B"},{"count":74,"label":"C"}]

Now, what I want to do is to group by the label key and for example, sum the count key. 现在,我要做的是按标签键分组,例如对计数求和

The output should be something like this: 输出应该是这样的:

[{"label": A, 'count': 7215},
 {"label": B, 'count': 25493},
 {"label": C, 'count': 3567}]

OR this: 或这个:

  A,       B,     C
7215,    25493,  3567

I've tried to implement the latter one like this: 我试图像这样实现后一种:

select sum(A) as A, sum(B) as B, sum(C) as C
from (
select json_extract(report,
                '$.some_attribute[0].count') as A,
       json_extract(report,
                '$.some_attribute[1].count') as B,
       json_extract(report,
                '$.some_attribute[0].count') as C
from table1
group by var1
having date == max(date));

The thing is, how can you be sure that all the objects in the array will be sorted the same way. 问题是,如何确定数组中的所有对象都将以相同的方式排序。 So this may cause problems. 因此,这可能会引起问题。

Any solutions? 有什么办法吗? thanks! 谢谢!

If you "un-nest" the json strings returned from the first json_extract ,as with json_each , it becomes trivial. 如果您“取消嵌套”从第一个json_extract返回的json字符串(与json_each一样) ,它将变得无关紧要。 This worked in my repro: 这在我的再现中起作用:

WITH result as (select jsonString from jsonPlay)
select json_extract(value,'$.label') label,SUM(json_extract(value,'$.count'))
from result,json_each(jsonString) 
group by label

Giving this result: 给出以下结果:

A| 7215
 B| 25493
 C| 3567

Basically, your select json_extract(report, '$.some_attribute') block replaces select jsonString from jsonPlay 基本上,您的select json_extract(report, '$.some_attribute')块替换了select jsonString from jsonPlay

You could use this to "columnize" it, as in your OR option. 您可以使用它来“列”它,如在OR选项中一样。

WITH result as (select jsonString from jsonPlay) 
select SUM(CASE WHEN json_extract(value,'$.label')='A' then json_extract(value,'$.count') END) 'A', 
SUM(CASE WHEN json_extract(value,'$.label')='B' then json_extract(value,'$.count') END) 'B', 
SUM(CASE WHEN json_extract(value,'$.label')='C' then json_extract(value,'$.count') END) 'C'
from result,json_each(jsonString)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM