2017-08-16 117 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