2016-11-04 57 views
-1

我试图让JSON对象的结果值作为创建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` 

回答

0

是否有任何理由不能得到结果作为数组,然后转换为json?

例如,在PHP ...

$results = array(
    "year"=>2015, 
    // etc 
); 

json_encode($results); 
+0

我尝试创建D3使用日期为年,月,日一分级柱状图。为此,我需要JSON格式的结果集 – harsith

+0

好吧,所以你使用AJAX检索数据? – AgentSquidflaps

+0

是只使用ajax ... – harsith