使用json格式创建查询

时间:2016-11-04 12:27:47

标签: java json postgresql

我正在尝试将json对象结果值设为

{
    year: 2015,
    count : 10,
    children[{
        month: Jan,
        count: 1,
        children: [{
            date: 01,
            count: 1
        },
        {
            date: 11,
            count: 1
        }]
    },
    {
        month: feb,
        count: 2,
        children: []
    }]
}

从日期算起。我已经完成了部分年份和日期。对于一个月,我需要检索与月份有关的孩子。

select row_to_json(t) 
from (  select EXTRACT(year FROM pr_created_on) as year, (select array_to_json(array_agg(row_to_json(jd)))
  from (
    select count(project_id), EXTRACT(month FROM pr_created_on) as month,(select array_to_json(array_agg(row_to_json(js)))
  from (
    select to_char(pr_created_on, 'dd') as date,EXTRACT(month FROM pr_created_on) as month, count(project_id) from projects 
    where  EXTRACT(year FROM pr_created_on) = '2015'  and to_char(pr_created_on, 'dd') between '01' and '30' group by month,date order by date  
  ) js) as children
    from projects 
    where EXTRACT(month FROM pr_created_on) between '01'  and '12' AND EXTRACT(year FROM pr_created_on) = '2015' group by month      
  ) jd
) as children from projects j  where EXTRACT(year FROM pr_created_on) = '2015' group by year) as t` 

1 个答案:

答案 0 :(得分:0)

有什么理由不能将结果作为数组然后转换为json?

例如,在PHP ...

{name:"Apple", val:8}, {name:"Banana", val:9}