SQL where子句子查询

时间:2017-09-06 11:09:10

标签: mysql sql subquery

有人可以向我解释为什么我的工作不对吗?

select distinct campaignid
from(
select
day,
campaignid,
campaignname,
sum(campaign_impressions) as impressions,
sum(installs) as installs,
sum(revenue) as revenue
from bi.mobile_united_net t
where (campaignid in ( select TOP 14 distinct campaignid
                            from bi.mobile_united_net                    
                            where campaignid <> '3600961'
                            group by 1
                            order by sum(campaign_impressions) desc
                            ) 
        or  campaignid = '1433407')

group by 1,2,3
order by 1,2);

因为我预计只会有15个广告系列,但会获得408个广告系列。

enter image description here

谢谢, 陈

1 个答案:

答案 0 :(得分:0)

这是按1,2,3分组,即day,campaignid,campaignname,,因此每天和广告系列的每个组合(即您的15个广告系列中的某个广告系列发生的每一天)都会获得一行

要只获得15行,您需要从选择中删除“日期”,然后按“广告系列,广告系列名称”分组

select distinct campaignid
from(
select
campaignid,
campaignname,
sum(campaign_impressions) as impressions,
sum(installs) as installs,
sum(revenue) as revenue
from bi.mobile_united_net t
where (campaignid in ( select TOP 14 distinct campaignid
                            from bi.mobile_united_net                    
                            where campaignid <> '3600961'
                            group by 1
                            order by sum(campaign_impressions) desc
                            ) 
        or  campaignid = '1433407')

group by campaignid,campaignname    
order by 1,2);

我还建议使用列名而不是数字来进行分组和排序,这样可以更容易阅读和维护。