У меня следующий запрос, и я использую один и тот же подзапрос 4 раза, что влияет на мою производительность. Есть ли способ повторно использовать подзапрос, только запустив его один раз и сославшись на него в других местах, где он мне нужен?
ниже код ,
спасибо
SELECT adb_product_type.strproduct_type AS strproduct_type, adb_product_cat.strproduct_cat AS strproduct_cat, adb_product.strproduct AS strproduct,
CASE
(SELECT COUNT(DISTINCT adb_campaign_1.campaign_id)
FROM adb_camp_media AS adb_camp_media_1
INNER JOIN adb_campaign AS adb_campaign_1 ON adb_campaign_1.campaign_id = adb_camp_media_1.campaign_id
WHERE adb_camp_media_1.lngproduct = adb_camp_media.lngproduct
AND adb_camp_media_1.lngproduct = adb_camp_media.lngproduct
AND ((adb_campaign_1.start_date >= '2010-01-01' AND adb_campaign_1.start_date <= '2010-01-31')
OR (adb_campaign_1.end_date >= '2010-01-01' AND adb_campaign_1.end_date <= '2010-01-31')) )
WHEN 0 THEN 'No' ELSE 'Yes'
END AS 'YesNo',
CASE
(SELECT COUNT(DISTINCT adb_campaign_1.campaign_id)
FROM adb_camp_media AS adb_camp_media_1
INNER JOIN adb_campaign AS adb_campaign_1 ON adb_campaign_1.campaign_id = adb_camp_media_1.campaign_id
WHERE adb_camp_media_1.lngproduct = adb_camp_media.lngproduct
AND adb_camp_media_1.lngproduct = adb_camp_media.lngproduct
AND ((adb_campaign_1.start_date >= '2010-01-01' AND adb_campaign_1.start_date <= '2010-01-31')
OR (adb_campaign_1.end_date >= '2010-01-01' AND adb_campaign_1.end_date <= '2010-01-31')) )
WHEN 0 THEN '' ELSE
(SELECT COUNT(DISTINCT adb_campaign_1.campaign_id)
FROM adb_camp_media AS adb_camp_media_1
INNER JOIN adb_campaign AS adb_campaign_1 ON adb_campaign_1.campaign_id = adb_camp_media_1.campaign_id
WHERE adb_camp_media_1.lngproduct = adb_camp_media.lngproduct
AND adb_camp_media_1.lngproduct = adb_camp_media.lngproduct
AND ((adb_campaign_1.start_date >= '2010-01-01' AND adb_campaign_1.start_date <= '2010-01-31')
OR (adb_campaign_1.end_date >= '2010-01-01' AND adb_campaign_1.end_date <= '2010-01-31')) )
END AS 'CampaignCount',
CASE
(SELECT COUNT(DISTINCT adb_campaign_1.campaign_id)
FROM adb_camp_media AS adb_camp_media_1
INNER JOIN adb_campaign AS adb_campaign_1 ON adb_campaign_1.campaign_id = adb_camp_media_1.campaign_id
WHERE adb_camp_media_1.lngproduct = adb_camp_media.lngproduct
AND adb_camp_media_1.lngproduct = adb_camp_media.lngproduct
AND ((adb_campaign_1.start_date >= '2010-01-01' AND adb_campaign_1.start_date <= '2010-01-31')
OR (adb_campaign_1.end_date >= '2010-01-01' AND adb_campaign_1.end_date <= '2010-01-31')) )
WHEN 0 THEN '' ELSE
adb_media.name
END AS mediaName
FROM adb_product_type
LEFT JOIN adb_product_cat ON adb_product_cat.lngproduct_type = adb_product_type.lngproduct_type
LEFT JOIN adb_product ON adb_product.lngproduct_cat = adb_product_cat.lngproduct_cat
LEFT JOIN adb_camp_media ON adb_camp_media.lngproduct = adb_product.lngproduct
LEFT JOIN adb_media ON adb_media.media_id = adb_camp_media.media_id
LEFT JOIN adb_camp_media_prod ON adb_camp_media_prod.media_num = adb_camp_media.media_num
LEFT JOIN adb_campaign ON adb_campaign.campaign_id = adb_camp_media.campaign_id
WHERE 1=1 AND (adb_campaign.start_date >= '2010-01-01' AND adb_campaign.start_date <= '2010-01-31')
OR (adb_campaign.end_date >= '2010-01-01' AND adb_campaign.end_date <= '2010-01-31')
OR ( adb_camp_media.campaign_id IS NULL) OR (adb_camp_media_prod.lngproduct IS NULL)
GROUP BY strproduct, mediaName ORDER BY strproduct_type, strproduct_cat, strproduct