我试图让这句话返回多行(每个广告系列ID一行)。此代码最初仅用于接受一个广告系列ID,但我需要让它接受多个广告系列ID。以下代码段仅返回1行,但应返回6行。我也认为这种说法可能更有效率。
SELECT * FROM
(SELECT
campaignID,
count(ctaS.engagementID) AS numEngagements,
count(distinct(ctaS.targetID)) AS numTargets,
SUM(engagementType='email') AS numEmails,
SUM(engagementType='call') as numCalls,
SUM(engagementType='meeting') as numMeetings,
count(distinct(ctaC.email)) AS numEngagementsByEmail,
count(distinct(ctaS.ipAddress)) AS numEngagementsByIP
FROM engagements AS ctaS
LEFT JOIN engagements_content AS ctaC ON ctaS.engagementID = ctaC.engagementID
WHERE ctaS.campaignID IN ('639ba3c9d86726f2ef5a6ca3','2bce996549ed7acb','f91491361140ccd81a7ab8bb','3d4d9a45bba4b8ef9c22edbc','VhFfwsAnxr9OBfRvGWKsyr7H','0819c9ff210c24f9efcb16fc')
AND ctaS.is_active = 1
AND ctaS.targetID <> ''
AND ctaS.engagementType <> ''
AND ctaS.ipAddress NOT IN ('75.43.48.211')
) as tbl1,
(SELECT
t1.numTimesIndexLoaded,
t1.numTimesIndexLoadedUnique,
t1.numTimesResultsLoaded,
t1.numTimesResultsLoadedUnique,
t1.numTimesCTALoaded,
t1.numTimesCTALoadedUnique
FROM
(SELECT sum(ctaSL.page = 'index') AS numTimesIndexLoaded,
sum(ctaSL.page = 'results') AS numTimesResultsLoaded,
sum(ctaSL.page = 'cta') AS numTimesCTALoaded,
count(DISTINCT CASE WHEN ctaSL.page = 'index' THEN ctaSL.ip_address END) AS numTimesIndexLoadedUnique,
count(DISTINCT CASE WHEN ctaSL.page = 'results' THEN ctaSL.ip_address END) AS numTimesResultsLoadedUnique,
count(DISTINCT CASE WHEN ctaSL.page = 'cta' THEN ctaSL.ip_address END) AS numTimesCTALoadedUnique
FROM stats_page_loads AS ctaSL
WHERE ctaSL.campaign_id IN ('639ba3c9d86726f2ef5a6ca3','2bce996549ed7acb','f91491361140ccd81a7ab8bb','3d4d9a45bba4b8ef9c22edbc','VhFfwsAnxr9OBfRvGWKsyr7H','0819c9ff210c24f9efcb16fc')
AND ctaSL.is_active = 1
AND ctaSL.ip_address NOT IN ('75.43.48.211')
) t1
) as tbl2,
(SELECT tblA.numFacebookClicks, tblB.numTwitterClicks
FROM
(SELECT count(target_id) AS numFacebookClicks
FROM stats_social_clicks AS sc
WHERE sc.social_network = 'facebook'
AND sc.campaign_id IN ('639ba3c9d86726f2ef5a6ca3','2bce996549ed7acb','f91491361140ccd81a7ab8bb','3d4d9a45bba4b8ef9c22edbc','VhFfwsAnxr9OBfRvGWKsyr7H','0819c9ff210c24f9efcb16fc')
AND sc.is_active = 1
) AS tblA
JOIN
(SELECT count(target_id) AS numTwitterClicks
FROM stats_social_clicks AS sc
WHERE sc.social_network = 'twitter'
AND sc.campaign_id IN ('639ba3c9d86726f2ef5a6ca3','2bce996549ed7acb','f91491361140ccd81a7ab8bb','3d4d9a45bba4b8ef9c22edbc','VhFfwsAnxr9OBfRvGWKsyr7H','0819c9ff210c24f9efcb16fc')
AND sc.is_active = 1
) AS tblB
) as tbl3
WHERE campaignID IN ('639ba3c9d86726f2ef5a6ca3','2bce996549ed7acb','f91491361140ccd81a7ab8bb','3d4d9a45bba4b8ef9c22edbc','VhFfwsAnxr9OBfRvGWKsyr7H','0819c9ff210c24f9efcb16fc')