2017-08-16 81 views
1

阵列不同的输出我有查询与输出其中前4个元素将是相同的和其他人不同。我想将这些元素保存在数组中。 例如我有输出: enter image description here保存从选择

其中SID,名称,time_from,time_to是相同的值和标识,类型,计数,从,到具有不同的值。

我有查询:

select asch.id as sid, 
     asch.name, 
     asch.time_from, 
     asch.time_to, 
     array_agg(ad.id) as ad_id, 
     array_agg(ad.type) as ad_type, 
     array_agg(ad.count)as ad_count, 
     array_agg(ad.from)as ad_from,array_agg(ad.to) as ad_to 
from ad ad 
     join ad_ad_group aag on aag.ad_id=ad.id 
     join ad_group ag on ag.id=aag.ad_group_id 
     join ad_schedule asch on asch.ad_group_id=ag.id 
     join ad_shedule_device asd on asd.ad_schedules_id=asch.id 
     join device d on d.device_id=asd.devices_id 
where d.device_id=4 
group by asch.id 

哪里取决于DEVICE_ID林在其广告所在的广告shedule选择这个设备鸿沟的所有广告。 其输出是:

enter image description here

,但我想是这样的:

enter image description here

我真的对不起我的英语不好。

回答

0
select asch.id as sid, 
    asch.name, 
    asch.time_from, 
    asch.time_to, 
    jsonb_agg(jsonb_build_object(
     'ad_id'::text, ad.id, 
     'ad_type'::text, ad.type, 
     'ad_count'::text, ad.count, 
     'ad_from'::text, ad.from, 
     'ad_to'::text,ad.to 
    )) 
from ad ad 
    join ad_ad_group aag on aag.ad_id=ad.id 
    join ad_group ag on ag.id=aag.ad_group_id 
    join ad_schedule asch on asch.ad_group_id=ag.id 
    join ad_shedule_device asd on asd.ad_schedules_id=asch.id 
    join device d on d.device_id=asd.devices_id 
where d.device_id=4 
group by asch.id 
+0

它给了我错误: 错误:功能jsonb_build_object(未知,整数,未知字符改变,未知的,整数的,未知的,日期不详,日期)不存在 LINE 5:jsonb_agg(jsonb_build_object( ^ **********错误********** 错误:函数jsonb_build_object(unknown,integer,unknown,character varying,unknown,integer,unknown,date,unknown,date)不存在 SQL状态:42883 – Hanka

+0

@Hanka:编辑 –

+0

还是错误:功能jsonb_build_object不存在 问题是,我的PostgreSQL 9.3版本。 – Hanka