我有查询与输出,其中前4个元素将是相同的,其他的不同。我想将这些元素保存在数组中。例如,我有以下输出:

其中,sid、name、time_from、time_to是相同的值,而id、type、count、from具有不同的值。
我有疑问:
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.idwhere取决于device_id,我选择此设备上的所有广告除以广告所在的广告时间表。哪个输出是:

但我想要这样的东西:

我真的为我糟糕的英语感到抱歉。
发布于 2017-08-16 22:43:15
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.idhttps://stackoverflow.com/questions/45713382
复制相似问题