SELECT campaign.placement_id as placement_id,
if(placements.name = '' or placements.name is null, 'Other', placements.name) as placement_name,
if(channels.primary_category = '' or channels.primary_category is null, 'Other', channels.primary_category) as category_name,
sum(daily_stats.impressions) as impressions,
sum(if(ads.parent_ad_id IS NULL,daily_stats.clicks,0)) as clicks,
IFNULL (round((sum(if(ads.parent_ad_id IS NULL,daily_stats.clicks,0))*100)/sum(daily_stats.impressions),2), 0.00) as ctr,
IFNULL( sum( if( pc1.channel_id is NULL, if( pc.channel_id is NULL, pubrevenue_advcurrency*ex_rate_advpub, if( pc.model='CPM', impressions*(pc.price_per_unit), if( ads.parent_ad_id IS NULL, daily_stats.clicks, 0 ) *pc.price_per_unit ) ), if( pc1.model='CPM', impressions*pc1.price_per_unit, if( ads.parent_ad_id IS NULL, daily_stats.clicks, 0 ) *pc1.price_per_unit ) ) ) *1000 /sum(impressions) , 0.00) as effective_ecpm 

FROM adplatform.daily_stats as daily_stats 
LEFT JOIN EACH adplatform.campaign as campaign ON daily_stats.campaign_id = campaign.id 
LEFT JOIN EACH adplatform.placements as placements ON campaign.placement_id = placements.id 
LEFT JOIN EACH adplatform.channels as channels ON daily_stats.channel_id = channels.id 
LEFT JOIN EACH adplatform.ads as ads ON daily_stats.ad_id = ads.id 
LEFT JOIN EACH adplatform.channel_price as pc ON daily_stats.channel_id=pc.channel_id   
LEFT JOIN EACH adplatform.channel_price_all as pc1 ON daily_stats.channel_id=pc1.channel_id 

 WHERE 
(
    (
    pc.adtype = daily_stats.adtype and (daily_stats.date>=pc.start_date and daily_stats.date <= pc.end_date)
    )
  OR
    (
    daily_stats.ccode = pc1.ccode and pc1.adtype = daily_stats.adtype and (daily_stats.date>=pc1.start_date and daily_stats.date <= pc1.end_date) 
    )
)

AND
campaign.placement_id IS NOT NULL AND channels.primary_category is NOT NULL AND daily_stats.date >= '2014-04-01' AND daily_stats.date <= '2014-04-02' 

GROUP BY placement_id,placement_name,category_name,impressions,clicks,ctr,effective_ecpm
