使用嵌套的WHERE子句?

时间:2013-04-12 21:07:13

标签: mysql sql

SELECT ymd, 
       creative_id, 
       sum(imps) as imps, 
       sum(booked_revenue_dollars) as booked_rev
  FROM agg_dw_advertiser_publisher_analytics_adjusted
 WHERE buyer_member_id=456
   AND (sum(imps) > 500 WHERE ymd > current_date() - 14) #this part is wrong
 GROUP BY 1,2
HAVING min(ymd) < current_date() - 14
 ORDER BY 2,1 ASC;

ymd =年/月/日

我正在尝试过滤creative_id,其中字段imps的总和在过去两周内总计小于500。我显然没有这样做,但已尝试作为额外的HAVING子句以及子查询。我做错了什么?

2 个答案:

答案 0 :(得分:0)

没有名为嵌套WHERE 的概念。您可以像这样修改HAVING

SELECT creative_id, 
       sum(imps) as imps, 
       sum(booked_revenue_dollars) as booked_rev
  FROM agg_dw_advertiser_publisher_analytics_adjusted
 WHERE buyer_member_id=456
 GROUP BY creative_id
HAVING sum(imps) > 500 AND min(ymd) > current_date() - 14
 ORDER BY 2,1 ASC;

答案 1 :(得分:0)

像这样的东西(在我的头顶)

SELECT ymd, creative_id, 
       sq.imps as imps,
       sq.booked_rev as booked_rev
  FROM agg_dw_advertiser_publisher_analytics_adjusted a
  INNER JOIN 
      (
       SELECT creative_id, sum(imps) as imps, sum(booked_revenue_dollars) as booked_rev
       FROM agg_dw_advertiser_publisher_analytics_adjusted
       WHERE buyer_member_id=456 and ymd > current_date() - 14
       GROUP BY creative_id
      ) as sq
    ON  a.creative_id = sq.creative_id
 WHERE buyer_member_id=456 and sq.imps > 500 and ymd > current_date() - 14
 ORDER BY ymd,creative_id ASC;