SQL查询-联合查询中的总和和平均值

SQL查询-联合查询中的总和和平均值,sql,Sql,鉴于下表: SITE ---- id name reference ---------------------------- 1 AAM 783C3502-19B9-EFA7-D6B8874219EF6734 3 AOC B4E82054-C09F-4338-50C809C7515E755B SERVICE ------- id name type --------------------

鉴于下表:

SITE
----
id   name          reference
----------------------------
1    AAM           783C3502-19B9-EFA7-D6B8874219EF6734
3    AOC           B4E82054-C09F-4338-50C809C7515E755B

SERVICE
-------
id   name               type
-----------------------------------
1    Outbound data      Web Hosting
2    Inbound data       Web Hosting
3    API Data           API Traffic
4    Site Space         Disk Space
5    DB Space           Disk Space

USAGE
-------
site   service   timeperiod     detail
-----------------------------------------------
1      4         477             21997
1      5         477                53
1      4         479              1991
1      5         479                53
3      4         477               448
3      5         477                10
3      4         479               448
....

TIMEPERIOD
----------
id     year   month    day    when
-----------------------------------------
477    2012   7        2      2012-07-02 
479    2012   7        3      2012-07-03 
以下查询在两个磁盘空间类别DB space和site space下对特定网站一个月内的活动进行平均:

如果我分别在每个站点UUID上运行union查询

UUID:783C3502-19B9-EFA7-D6B8874219EF6734

UUID:B4E82054-C09F-4338-50C809C7515E755B

到目前为止一切都很好

但是,当我传递多个站点UUID时,如下所示:

    SELECT `month`,`year`, AVG(`usage`.detail) as dbsize, 0 as sitesize, AVG(`usage`.detail) as totalsize 
FROM site 
INNER JOIN `usage` ON site.reference 
        IN ('783C3502-19B9-EFA7-D6B8874219EF6734','B4E82054-C09F-4338-50C809C7515E755B') 
    AND `usage`.site = site.id 
INNER JOIN service 
        ON service.name = 'DB Space' 
        AND service.id = `usage`.service 
INNER JOIN timeperiod 
        ON timeperiod.when > '2012-07-01' 
            AND timeperiod.when < '2012-07-31' 
    AND `usage`.timeperiod = timeperiod.id
WHERE `usage`.site = site.id 
 GROUP BY `month`,`year` 

UNION 

SELECT `month`,`year`, 0 as dbsize, AVG(`usage`.detail) as sitesize, AVG(`usage`.detail) as totalsize 
FROM site 
INNER JOIN `usage` ON site.reference 
        IN ('783C3502-19B9-EFA7-D6B8874219EF6734','B4E82054-C09F-4338-50C809C7515E755B') 
    AND `usage`.site = site.id 
INNER JOIN service 
        ON service.name = 'Site Space'
    AND service.id = `usage`.service 
INNER JOIN timeperiod 
        ON timeperiod.when > '2012-07-01' 
            AND timeperiod.when < '2012-07-31' 
    AND `usage`.timeperiod = timeperiod.id 
WHERE `usage`.site = site.id 
GROUP BY `month`,`year` 
多个uuiid的结果甚至比我单独传递id的结果还要少。这是因为处理多个UUID的查询是对单个UUID查询的结果求平均值

我希望多UUID查询将各个UUID的结果相加。 我当然希望这是有道理的

编辑 这是我使用的总和查询的类型:

SELECT `month`,`year`, SUM(dbsize) as dbsize, SUM(sitesize) as sitesize, SUM(totalsize) as totalsize 
FROM ( 
    SELECT `month`,`year`, AVG(`usage`.detail) as dbsize, 0 as sitesize, AVG(`usage`.detail) as totalsize 
    FROM site 
    INNER JOIN `usage` ON site.reference IN ('783C3502-19B9-EFA7-D6B8874219EF6734','B4E82054-C09F-4338-50C809C7515E755B') 
        AND `usage`.site = site.id 
    INNER JOIN service ON service.name = 'DB Space' AND service.id = `usage`.service 
    INNER JOIN timeperiod ON timeperiod.when > '2012-07-01' AND timeperiod.when < '2012-07-31' 
        AND `usage`.timeperiod = timeperiod.id
    WHERE `usage`.site = site.id 
     GROUP BY `month`,`year` 

    UNION 

    SELECT `month`,`year`, 0 as dbsize, AVG(`usage`.detail) as sitesize, AVG(`usage`.detail) as totalsize 
    FROM site 
    INNER JOIN `usage` ON site.reference IN ('783C3502-19B9-EFA7-D6B8874219EF6734','B4E82054-C09F-4338-50C809C7515E755B') 
        AND `usage`.site = site.id 
    INNER JOIN service ON service.name = 'Site Space'
        AND service.id = `usage`.service 
    INNER JOIN timeperiod ON timeperiod.when > '2012-07-01' AND timeperiod.when < '2012-07-31' 
        AND `usage`.timeperiod = timeperiod.id 
    WHERE `usage`.site = site.id 
    GROUP BY `month`,`year` 
) as x 
GROUP BY `month`,`year`

如果要按站点引用平均分区,则需要将站点引用添加到group by子句中

SELECT `month`,`year`, 
        AVG(`usage`.detail) as dbsize, 0 as sitesize, 
        AVG(`usage`.detail) as totalsize 
    FROM site 
    INNER JOIN `usage` 
        ON site.reference IN ('B4E82054-C09F-4338-50C809C7515E755B') 
        AND `usage`.site = site.id 
    INNER JOIN service 
        ON service.name = 'DB Space' 
            AND service.id = `usage`.service 
    INNER JOIN timeperiod 
        ON timeperiod.when > '2012-07-01' 
            AND timeperiod.when < '2012-07-31' 
        AND `usage`.timeperiod = timeperiod.id
    WHERE `usage`.site = site.id 
     GROUP BY `month`,`year` 

    UNION 

    SELECT `month`,`year`, 0 as dbsize, 
        AVG(`usage`.detail) as sitesize, 
        AVG(`usage`.detail) as totalsize 
    FROM site 
    INNER JOIN `usage` 
        ON site.reference IN ('B4E82054-C09F-4338-50C809C7515E755B') 
        AND `usage`.site = site.id 
    INNER JOIN service ON service.name = 'Site Space'
        AND service.id = `usage`.service 
    INNER JOIN timeperiod 
        ON timeperiod.when > '2012-07-01' 
            AND timeperiod.when < '2012-07-31' 
        AND `usage`.timeperiod = timeperiod.id 
    WHERE `usage`.site = site.id 
    GROUP BY `month`,`year` 
month   year     dbsize      sitesize   totalsize
-------------------------------------------------
7       2012    10.0000        0.0000     10.0000
7       2012     0.0000      448.6364    448.6364
    SELECT `month`,`year`, AVG(`usage`.detail) as dbsize, 0 as sitesize, AVG(`usage`.detail) as totalsize 
FROM site 
INNER JOIN `usage` ON site.reference 
        IN ('783C3502-19B9-EFA7-D6B8874219EF6734','B4E82054-C09F-4338-50C809C7515E755B') 
    AND `usage`.site = site.id 
INNER JOIN service 
        ON service.name = 'DB Space' 
        AND service.id = `usage`.service 
INNER JOIN timeperiod 
        ON timeperiod.when > '2012-07-01' 
            AND timeperiod.when < '2012-07-31' 
    AND `usage`.timeperiod = timeperiod.id
WHERE `usage`.site = site.id 
 GROUP BY `month`,`year` 

UNION 

SELECT `month`,`year`, 0 as dbsize, AVG(`usage`.detail) as sitesize, AVG(`usage`.detail) as totalsize 
FROM site 
INNER JOIN `usage` ON site.reference 
        IN ('783C3502-19B9-EFA7-D6B8874219EF6734','B4E82054-C09F-4338-50C809C7515E755B') 
    AND `usage`.site = site.id 
INNER JOIN service 
        ON service.name = 'Site Space'
    AND service.id = `usage`.service 
INNER JOIN timeperiod 
        ON timeperiod.when > '2012-07-01' 
            AND timeperiod.when < '2012-07-31' 
    AND `usage`.timeperiod = timeperiod.id 
WHERE `usage`.site = site.id 
GROUP BY `month`,`year` 
month   year     dbsize      sitesize   totalsize
-------------------------------------------------
7       2012    31.5000        0.0000     31.5000
7       2012     0.0000     1225.6818   1225.6818
SELECT `month`,`year`, SUM(dbsize) as dbsize, SUM(sitesize) as sitesize, SUM(totalsize) as totalsize 
FROM ( 
    SELECT `month`,`year`, AVG(`usage`.detail) as dbsize, 0 as sitesize, AVG(`usage`.detail) as totalsize 
    FROM site 
    INNER JOIN `usage` ON site.reference IN ('783C3502-19B9-EFA7-D6B8874219EF6734','B4E82054-C09F-4338-50C809C7515E755B') 
        AND `usage`.site = site.id 
    INNER JOIN service ON service.name = 'DB Space' AND service.id = `usage`.service 
    INNER JOIN timeperiod ON timeperiod.when > '2012-07-01' AND timeperiod.when < '2012-07-31' 
        AND `usage`.timeperiod = timeperiod.id
    WHERE `usage`.site = site.id 
     GROUP BY `month`,`year` 

    UNION 

    SELECT `month`,`year`, 0 as dbsize, AVG(`usage`.detail) as sitesize, AVG(`usage`.detail) as totalsize 
    FROM site 
    INNER JOIN `usage` ON site.reference IN ('783C3502-19B9-EFA7-D6B8874219EF6734','B4E82054-C09F-4338-50C809C7515E755B') 
        AND `usage`.site = site.id 
    INNER JOIN service ON service.name = 'Site Space'
        AND service.id = `usage`.service 
    INNER JOIN timeperiod ON timeperiod.when > '2012-07-01' AND timeperiod.when < '2012-07-31' 
        AND `usage`.timeperiod = timeperiod.id 
    WHERE `usage`.site = site.id 
    GROUP BY `month`,`year` 
) as x 
GROUP BY `month`,`year`