简体   繁体   中英

SUM a group of SUMS

I am having trouble creating a SUM of two lines in my code that already have SUMS. I end up with an error. Can someone help me with the code? The calculation that I want is SUM(yr_L / yr_CC). I realize I can't use alias's to do calculations, but I can't seem to get this to work.

Below works, without the complex SUM calculation lines ...

SELECT
IF (Artist LIKE '%Bethel%' , 'Bethel', NULL ) as Artist,
COUNT(IF(CCD > 0,1,NULL)) as "c_CC",
ROUND(SUM(CC28) / COUNT(IF(CCD > 0,1,NULL)) * 13,0) as yr_CC,
COUNT(IF(LD > 0,1,NULL)) as c_L,
ROUND(SUM(L28) / COUNT(IF(LD > 0,1,NULL)) * 13,0) as yr_L,
COUNT(IF(FD > 0,1,NULL)) as c_F,
ROUND(SUM(F28) / COUNT(IF(FD > 0,1,NULL)) * 13,0) as yr_F,
COUNT(IF(MTD > 0,1,NULL)) as c_MT,
ROUND(SUM(MT28) / COUNT(IF(MTD > 0,1,NULL)) * 13,0) as yr_MT,
ROUND(SUM(CC28) / COUNT(IF(CCD > 0,1,NULL)) * 13,0) +
    ROUND(SUM(L28) / COUNT(IF(LD > 0,1,NULL)) * 13,0) +
    ROUND(SUM(MT28) / COUNT(IF(MTD > 0,1,NULL)) * 13,0) +
    ROUND(SUM(CC28) / COUNT(IF(CCD > 0,1,NULL)) * 13,0)
    AS yr_Total
FROM praisecharts_reporting.large_sales_report
GROUP BY 1

Here is what I did that creates an error (the equation I added has spaces around it):

SELECT
IF (Artist LIKE '%Bethel%' , 'Bethel', NULL ) as Artist,
COUNT(IF(CCD > 0,1,NULL)) as "c_CC",
ROUND(SUM(CC28) / COUNT(IF(CCD > 0,1,NULL)) * 13,0) as yr_CC,
COUNT(IF(LD > 0,1,NULL)) as c_L,
ROUND(SUM(L28) / COUNT(IF(LD > 0,1,NULL)) * 13,0) as yr_L,

SUM (
        (
            SUM(L28) / COUNT(IF(LD > 0,1,NULL)) -- yr_L
        ) / 
        (
            SUM(CC28) / COUNT(IF(CCD > 0,1,NULL)) -- yr_CC
        )
    )
    AS r_L_C,

COUNT(IF(FD > 0,1,NULL)) as c_F,
ROUND(SUM(F28) / COUNT(IF(FD > 0,1,NULL)) * 13,0) as yr_F,
COUNT(IF(MTD > 0,1,NULL)) as c_MT,
ROUND(SUM(MT28) / COUNT(IF(MTD > 0,1,NULL)) * 13,0) as yr_MT,
ROUND(SUM(CC28) / COUNT(IF(CCD > 0,1,NULL)) * 13,0) +
    ROUND(SUM(L28) / COUNT(IF(LD > 0,1,NULL)) * 13,0) +
    ROUND(SUM(MT28) / COUNT(IF(MTD > 0,1,NULL)) * 13,0) +
    ROUND(SUM(CC28) / COUNT(IF(CCD > 0,1,NULL)) * 13,0)
    AS yr_Total
FROM praisecharts_reporting.large_sales_report
GROUP BY 1

The easiest thing to do here is probably to just repeat the two sums to take the quotient:

SELECT
    IF (Artist LIKE '%Bethel%' , 'Bethel', NULL) AS Artist,
    COUNT(IF(CCD > 0,1,NULL)) AS "c_CC",
    ROUND(SUM(CC28) / COUNT(IF(CCD > 0,1,NULL)) * 13, 0) AS yr_CC,
    COUNT(IF(LD > 0,1,NULL)) as c_L,
    ROUND(SUM(L28) / COUNT(IF(LD > 0,1,NULL)) * 13,0) as yr_L,
    COUNT(IF(FD > 0,1,NULL)) as c_F,
    ROUND(SUM(F28) / COUNT(IF(FD > 0,1,NULL)) * 13,0) as yr_F,
    COUNT(IF(MTD > 0,1,NULL)) as c_MT,
    ROUND(SUM(MT28) / COUNT(IF(MTD > 0,1,NULL)) * 13,0) as yr_MT,
    ROUND(SUM(CC28) / COUNT(IF(CCD > 0,1,NULL)) * 13,0) +
        ROUND(SUM(L28) / COUNT(IF(LD > 0,1,NULL)) * 13,0) +
        ROUND(SUM(MT28) / COUNT(IF(MTD > 0,1,NULL)) * 13,0) +
        ROUND(SUM(CC28) / COUNT(IF(CCD > 0,1,NULL)) * 13,0) AS yr_Total,
    -- new code here:
    ROUND( (SUM(L28) / COUNT(IF(LD > 0,1,NULL))) /
           (SUM(CC28) / COUNT(IF(CCD > 0,1,NULL))), 0) AS new_column
FROM praisecharts_reporting.large_sales_report
GROUP BY 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM