对行进行求和而不进行级联

时间:2014-01-31 22:38:31

标签: mysql

我使用以下查询来查询2个表,问题是结果级联和每行的BILLABLE_IMP总和。当我删除VTR_POSITION时,casade我得到的列总和为一行但是BILLABE_IMPS总和并且它不应该。

SELECT
    IMP_DATE,
    CAMPAIGN.CAMPAIGN_NAME,
    SUM(IF(VTR_POSITION LIKE '/a0bc.png',VTR_COUNT,0)) AS VT0,
    SUM(IF(VTR_POSITION LIKE '/t25f.png',VTR_COUNT,0)) AS VT1,
    SUM(IF(VTR_POSITION LIKE '/f50s.png',VTR_COUNT,0)) AS VT2,
    SUM(IF(VTR_POSITION LIKE '/s75t.png',VTR_COUNT,0)) AS VT3,
    SUM(IF(VTR_POSITION LIKE '/o100l.png',VTR_COUNT,0)) AS VT4,
    SUM(CASE
      WHEN BOOKING_TYPE = 'Impression' THEN IMPRESSIONS_WITHOUT_DEFAULTS
      ELSE VIEWS
    END) AS BILLABLE_IMPS
FROM IMPRESSION
        INNER JOIN
    BANNER ON BANNER.BANNER_ID = IMPRESSION.BANNER_ID
        INNER JOIN
    FLIGHT ON BANNER.FLIGHT_ID = FLIGHT.FLIGHT_ID
        INNER JOIN
    CAMPAIGN ON CAMPAIGN.CAMPAIGN_ID = FLIGHT.CAMPAIGN_ID
        INNER JOIN
    PLACEMENT ON PLACEMENT.PLACEMENT_ID = IMPRESSION.PLACEMENT_ID
        INNER JOIN
    PAGE ON PAGE.PAGE_ID = PLACEMENT.PAGE_ID
        INNER JOIN
    WEBSITE ON PAGE.WEBSITE_ID = WEBSITE.WEBSITE_ID
        INNER JOIN
    CUSTOMER ON CUSTOMER.CUSTOMER_ID = IMPRESSION.CUSTOMER_ID
        INNER JOIN 
    VTR_TABLE ON VTR_TABLE.VTR_DATE = IMPRESSION.IMP_DATE AND VTR_TABLE.BANNER_ID = IMPRESSION.BANNER_ID AND VTR_TABLE.PLACEMENT_ID = IMPRESSION.PLACEMENT_ID
WHERE
     FLIGHT.CAMPAIGN_CPM > 0

GROUP BY IMP_DATE, CAMPAIGN.CAMPAIGN_NAME, VTR_POSITION;

0 个答案:

没有答案