简体   繁体   中英

How to combine two separate sql queries into two separate columns?

I am working on a SQL data base query in metabase. I have two separate queries which I wish to output as two separate columns, sorted by name. After performing union it seems to throw all values into one column duplicating the names. What is the best way to achieve the two column output(one per query)?

SELECT "source"."name" AS "name", "source"."count" AS "count"

FROM (SELECT "source"."count" AS "count", "source"."name" AS "name", "source"."count" AS "count_2", ("source"."count" * 1.5) AS "a" FROM (SELECT "marketing_campaign__via__campa"."name" AS "name", count(*) AS "count" FROM "public"."event_event"
LEFT JOIN "public"."event_event" "Event Event - Source Event" ON "public"."event_event"."source_event_id" = "Event Event - Source Event"."id" LEFT JOIN "public"."marketing_campaign" "marketing_campaign__via__campa" ON "public"."event_event"."campaign_id" = "marketing_campaign__via__campa"."id"
WHERE "public"."event_event"."status" = 'Queued'
GROUP BY "marketing_campaign__via__campa"."name"
ORDER BY "marketing_campaign__via__campa"."name" ASC) "source") "source"

union all


SELECT marketing_campaign.name,

    cast(sum((event_event.status='Opt-in')::int) as decimal) / nullif(sum((event_event.status='Sent')::int), 0)* 100 as "Opt-in Rate (Sent)"
FROM event_event
JOIN marketing_campaign ON event_event.campaign_id = marketing_campaign.id
WHERE marketing_campaign.is_archived=false [[AND {{date_created}}]]
GROUP BY marketing_campaign.name

LIMIT 1048576

Instead of union all you need to join both the table on name column then select second columns individually from both tables. I am considering the fact that name columns are same as both tables. If there is any name present in one table and absent in another it will not be shown in the result

select t1.name,t1.count ,t2."Opt-in Rate (Sent)" ,t1.count*t2."Opt-in Rate (Sent)" as predicted
from (
SELECT "source"."name" AS "name", "source"."count" AS "count"

FROM (SELECT "source"."count" AS "count", "source"."name" AS "name", "source"."count" AS "count_2", ("source"."count" * 1.5) AS "a" FROM (SELECT "marketing_campaign__via__campa"."name" AS "name", count(*) AS "count" FROM "public"."event_event"
LEFT JOIN "public"."event_event" "Event Event - Source Event" ON "public"."event_event"."source_event_id" = "Event Event - Source Event"."id" LEFT JOIN "public"."marketing_campaign" "marketing_campaign__via__campa" ON "public"."event_event"."campaign_id" = "marketing_campaign__via__campa"."id"
WHERE "public"."event_event"."status" = 'Queued'
GROUP BY "marketing_campaign__via__campa"."name"
ORDER BY "marketing_campaign__via__campa"."name" ASC) "source") "source") t1 inner join
(
SELECT marketing_campaign.name,

    cast(sum((event_event.status='Opt-in')::int) as decimal) / nullif(sum((event_event.status='Sent')::int), 0)* 100 as "Opt-in Rate (Sent)"
FROM event_event
JOIN marketing_campaign ON event_event.campaign_id = marketing_campaign.id
WHERE marketing_campaign.is_archived=false [[AND {{date_created}}]]
GROUP BY marketing_campaign.name
)t2 on t1.name=t2.name
LIMIT 1048576

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM