MYSQL查询 - 如何在同一查询中按总计,当前年份和过去12个月对数据求和(按列总计1)?

时间:2014-05-11 11:38:18

标签: mysql sql sql-server sum

我在我的网站中使用自定义类型的帖子,需要进行一些总结一些数据的查询(整数列)。我能够做到但是按月,当年和过去12个月,但不是在一起(3列)。

换句话说:

我目前有以下3个查询,我想加入一个(三列)。

1)查询1(数据总和除以)

    SELECT
        wp_postmeta.meta_value AS 'Tipo de aeronave',
        COUNT(wp_posts.ID) AS 'Total'
    FROM 
        wp_postmeta
    LEFT JOIN 
        wp_posts ON wp_postmeta.post_id = wp_posts.ID
    WHERE 
        wp_posts.post_type = 'ocorrencia'
        AND wp_posts.post_status = 'publish' 
        AND wp_postmeta.meta_key = 'tipo_de_aeronaves_envolvidas' 
    GROUP BY 
        wp_postmeta.meta_value

此查询生成:

Tipo de aeronave                       Total (all data)
["Civil estrangeira"]                1
["Civil nacional","Militar nacional"]    3
["Civil nacional"]                      157
["Militar nacional"]                 38
["Nao aplicavel"]                        76

第二个查询是(过去12个月的总和数据):

    SELECT
    wp_postmeta.meta_value AS 'Tipo de aeronave',
    COUNT(wp_posts.ID) AS 'Total'

    FROM wp_postmeta
        left JOIN wp_posts
        ON wp_postmeta.post_id = wp_posts.ID

    WHERE wp_posts.post_type = 'ocorrencia'
    AND wp_posts.post_status = 'publish' 
    AND wp_postmeta.meta_key = 'tipo_de_aeronaves_envolvidas' 
    AND wp_posts.post_date >= (CURRENT_DATE - INTERVAL 12 month) 

    GROUP BY wp_postmeta.meta_value

这会产生:

    Tipo de aeronave                    Total (12 months)
    ["Civil estrangeira"]                   1
    ["Civil nacional","Militar nacional"]   3
    ["Civil nacional"]                  157
    ["Militar nacional"]                    38
    ["Nao aplicavel"]                   73

第三个查询是(总和当前年份的数据):

    SELECT
    wp_postmeta.meta_value AS 'Tipo de aeronave',
    COUNT(wp_posts.ID) AS 'Total'

    FROM wp_postmeta
    left JOIN wp_posts
    ON wp_postmeta.post_id = wp_posts.ID

    WHERE wp_posts.post_type = 'ocorrencia'
    AND wp_posts.post_status = 'publish' 
    AND wp_postmeta.meta_key = 'tipo_de_aeronaves_envolvidas' 
    AND YEAR(wp_posts.post_date) = YEAR(CURRENT_DATE)

    GROUP BY wp_postmeta.meta_value

这会产生:

    Tipo de aeronave                    Total (current year)
    ["Civil nacional","Militar nacional"]   1
    ["Civil nacional"]                  59
    ["Militar nacional"]                    19
    ["Nao aplicavel"]                   19

最后,我想要的是一个加入所有结果的查询!

    Tipo de aeronave              Total (all data) Total (last 12 months) Total (current year)
     ["Civil estrangeira"]                   1               1                  0
     ["Civil nacional","Militar nacional"]    3              3                  1
    ["Civil nacional"]                  157            157                  59
    ["Militar nacional"]                    38              38                  19
    ["Nao aplicavel"]                   73              73                  19

很多!

1 个答案:

答案 0 :(得分:1)

SELECT wp_postmeta.meta_value AS Tipo de aeronave , COUNT(wp_posts.ID) AS Total , COUNT( CASE WHEN YEAR(wp_posts.post_date) = YEAR(CURRENT_DATE) THEN 1 END ) AS TOT_CURR_YR , COUNT( CASE WHEN wp_posts.post_date >= (CURRENT_DATE - INTERVAL 12 month) THEN 1 END ) AS TOT_LAST_12_MONTHS FROM wp_postmeta LEFT JOIN wp_posts ON wp_postmeta.post_id = wp_posts.ID WHERE wp_posts.post_type = 'ocorrencia' AND wp_posts.post_status = 'publish' AND wp_postmeta.meta_key = 'tipo_de_aeronaves_envolvidas' GROUP BY wp_postmeta.meta_value