2013-04-12 81 views
1
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 =年/月/日使用嵌套的WHERE子句?

我想合計過去2周濾除creative_id的其中場imps的總和小於500。我顯然沒有這樣做,但已經嘗試作爲額外的HAVING子句以及子查詢。我究竟做錯了什麼?

回答

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; 
+0

集團通過這裏寫不會給他正確的總和。他需要一個我相信能夠繼續選擇ymd的子查詢。 – rivarolle

+0

我認爲'ymd'在選擇列表中並不重要,因爲驅動程序是'creative_id' –

+0

同意的,但OP需要查看他是否需要歷史數據(保留ymd)或不需要:) – rivarolle

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; 
+0

明天我會試試這個。謝謝。 – ChrisArmstrong