1

次の表があるとします。

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 

次のクエリは、2つのディスクスペースカテゴリ(DBスペースとサイトスペース)で特定のWebサイトの1か月間のアクティビティを平均します。

各サイトの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

IDを個別に渡す場合よりも、複数のUUIIDの結果はさらに少なくなります。これは、複数の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`
4

1 に答える 1

0

サイト参照によって分割された平均を作成する場合は、Site_referenceをgroupby句に追加する必要があります。

于 2012-07-20T11:11:54.427 に答える