SQL查询 - UNION查询中的SUM和AVG

时间:2012-07-20 10:36:49

标签: sql

鉴于以下表格:

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 

以下查询平均两个磁盘空间类别(数据库空间和站点空间)下特定网站的一个月活动:

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

UUID:783C3502-19B9-EFA7-D6B8874219EF6734

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') 
        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') 
        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    53.0000        0.0000     53.0000
7       2012     0.0000     2002.7273   2002.7273

UUID:B4E82054-C09F-4338-50C809C7515E755B

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

到目前为止,一切都很好。

但是,当我按如下方式传递多个站点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` 

它产生以下结果:

month   year     dbsize      sitesize   totalsize
-------------------------------------------------
7       2012    31.5000        0.0000     31.5000
7       2012     0.0000     1225.6818   1225.6818

对于多个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`

1 个答案:

答案 0 :(得分:0)

如果您希望按网站引用进行平均分区,则需要将子网站引用添加到组子句中。