SELECT id, date, trafficchannelid, final, compressedreport, createdat, updatedat,…
- User
SELECT id, date, trafficchannelid, final, compressedreport, created_at, updated_at, details, domains FROM ( SELECT reports.id AS id, reports.date AS date, reports.trafficchannelid AS trafficchannelid, reports.final AS final, reports.compressedreport AS compressedreport, reports.created_at AS created_at, reports.updated_at AS updated_at, JSON_AGG( JSON_BUILD_OBJECT( ‘campaignid’, campaignid, ‘funneltemplateid’, funneltemplateid, ‘campaignidrt’, campaignidrt, ‘adGroups’, ( SELECT JSON_AGG( JSON_BUILD_OBJECT( ‘adgroupid’, report_adgroups.adgroupid, ‘ads’, ( SELECT JSON_BUILD_OBJECT( ‘adid’, report_ads.adid, ‘adname’, report_ads.adname, ‘adgroupname’, report_ads.adgroupname, ‘campaignname’, report_ads.campaignname, ‘date’, report_ads.date, ‘total_spent’, report_ads.total_spent, ‘total_click’, report_ads.total_click, ‘total_impressions’, report_ads.total_impressions, ‘revenue’, report_ads.revenue, ‘estimate’, report_ads.estimate, ‘feedsource’, report_ads.feedsource, ‘clickdetail’, ( SELECT JSON_Build_OBJECT( ‘track_id’, report_ads_clickdetail.track_id, ‘domain_id’, report_ads_clickdetail.domain_id ) FROM report_ads_clickdetail WHERE report_ads_clickdetail.report_ads_id = report_ads.id LIMIT 1 ) ) FROM report_ads WHERE report_ads.report_adgroups_id = report_adgroups.id LIMIT 1 ) ) ) FROM report_adgroups WHERE report_adgroups.report_details_id = report_details.id LIMIT 1 ) ) ) AS details, JSON_AGG( JSON_BUILD_OBJECT( ‘campaigns’, report_domains.campaigns, ‘domainid’, report_domains.domainid, ‘name’, report_domains.name, ‘weight’, report_domains.weight, ‘feedsource’, report_domains.feedsource ) ) AS domains, ROW_NUMBER() OVER (PARTITION BY reports.date ORDER BY reports.created_at DESC) AS rn FROM reports LEFT JOIN report_details on report_details.report_id = reports.id LEFT JOIN report_adgroups on report_adgroups.report_details_id = report_details.id LEFT JOIN report_ads on report_ads.report_adgroups_id = report_adgroups.id LEFT JOIN report_ads_clickdetail on report_ads_clickdetail.report_ads_id = report_ads.id LEFT JOIN report_conversion on report_conversion.report_ads_clickdetail_id = report_ads_clickdetail.id LEFT JOIN report_domains on report_domains.reportid = reports.id WHERE reports.date >= CAST(‘2023-09-21’ AS DATE) AND reports.trafficchannelid = ‘1985930498438577’ GROUP BY reports.id, reports.date, reports.trafficchannelid ) subquery WHERE rn = 1;
on this query I want the field ads to be a json_agg. How may I do that?
- Assistant
To make the 'ads' field a JSON array, you can modify the innermost subquery as follows:
SELECT adgroup_report.adgroupid, JSON_AGG( JSON_BUILD_OBJECT( 'adid', report_ads.adid, 'adname', report_ads.adname, 'adgroupname', report_ads.adgroupname, 'campaignname', report_ads.campaignname, 'date', report_ads.date, 'total_spent', report_ads.total_spent, 'total_click', report_ads.total_click, 'total_impressions', report_ads.total_impressions, 'revenue', report_ads.revenue, 'estimate', report_ads.estimate, 'feedsource', report_ads.feedsource, 'clickdetail', ( SELECT JSON_Build_OBJECT( 'track_id', report_ads_clickdetail.track_id, 'domain_id', report_ads_clickdetail.domain_id ) FROM report_ads_clickdetail WHERE report_ads_clickdetail.report_ads_id = report_ads.id LIMIT 1 ) ) ) AS ads FROM report_adgroups AS adgroup_report LEFT JOIN report_ads ON report_ads.report_adgroups_id = adgroup_report.id LEFT JOIN report_ads_clickdetail ON report_ads_clickdetail.report_ads_id = report_ads.id GROUP BY adgroup_report.adgroupid - User
Can you send me the whole query with the solution added in to it?
- Assistant
- User
- Assistant
- User
ERROR: syntax error at or near "FROM" LINE 47: FROM report_adgroups AS adgroup_report
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.