SQL - 百分比

时间:2016-03-02 15:56:16

标签: mysql sql

我需要进行查询,这会导致百分比。

我目前的查询如下:

var Add = {

onReady: function() {

    $(document).on("click", ".add-new", function() {

        // close other open text inputs
        $('.add-new-text').slideUp('normal');

        // if not open, then do so
        if($(this).next().is(':hidden') == true) {
            $(this).next().slideDown('normal');
        }

        $(".text-response").focus();

    });

    // close junk on page load
    $('.add-new-text').hide();

},

addText: function() {

    $(".text-response").keypress(function(e) {
        if (e.which == 13) {
            var response= $(".text-response").val();
            var newHTML = '<div class="small-12 column">\
                                <div class="callout panel notes selected"\
                                  data-response-text="' + response + '"\
                                  data-response-check=""\
                                  data-status-flag="false">\
                                    <div class="remove-response"></div>\
                                    ' + response + '\
                                </div>\
                            </div>';
            $(newHTML).insertBefore( $(this).parents().eq(2) );
            $(".text-response").val('');
            $('.add-new-text').slideUp('normal');
        }
    });

},

removeText: function() {

    $(document).on("click", ".remove-response", function() {

        $(this).parents().eq(1).remove();


    });

}

};

$(document).ready(Add.onReady);
$(document).ready(Add.addText);
$(document).ready(Add.removeText);

如果我开始查询,结果会显示:

select COUNT(CREATE_WEEKDAY),
       CREATE_WEEKDAY,
       COUNT(CREATE_WEEKDAY) * 100 /
from call_view
WHERE (create_month = MONTH(NOW() - INTERVAL 1 MONTH))
AND (create_year = YEAR(NOW() - INTERVAL 1 MONTH))
AND CUSTOMER_COMPANY_NAME = "Company"
group by CREATE_WEEKDAY

依旧......

我希望最后一列显示

COUNT(CREATE_WEEKDAY) |  CREATE_WEEKDAY  |      COUNT(CREATE_WEEKDAY) * 100
111                        Friday              11100
225                        MONDAY              22500

- &GT;&GT;最后一部分应该是所有"COUNT(CREATE_WEEKDAY) * 100 / SUM(COUNT(CREATE_WEEKDAY))" 的{​​{1}}。在示例中给出SUM

但是这段代码不起作用。我感谢任何帮助。

干杯

2 个答案:

答案 0 :(得分:0)

以下是使用Sub-query

的一种方法

首先确定给定过滤器的总计数(where子句)

SELECT Count(*) 
FROM   call_view 
WHERE  ( create_month = Month(Now() - INTERVAL 1 month) ) 
       AND ( create_year = Year(Now() - INTERVAL 1 month) ) 
       AND customer_company_name = 'Company' 

然后在分母中使用此查询来查找百分比

SELECT Count(create_weekday), 
       create_weekday, 
       Count(create_weekday) * 100 / (SELECT Count(*) 
                                      FROM   call_view 
                                      WHERE 
       ( create_month = Month(Now() - 
                        INTERVAL 1 month) ) 
       AND ( create_year = Year( 
             Now() - INTERVAL 1 month) ) 
       AND customer_company_name = 
           'Company') 
FROM   call_view 
WHERE  ( create_month = Month(Now() - INTERVAL 1 month) ) 
       AND ( create_year = Year(Now() - INTERVAL 1 month) ) 
       AND customer_company_name = "Company" 
GROUP  BY create_weekday 

答案 1 :(得分:0)

加入计算总数的子查询。

select COUNT(*),
       CREATE_WEEKDAY,
       COUNT(*) * 100 / total_count
from call_view
CROSS JOIN (SELECT COUNT(*) AS total_count
            FROM call_view
            WHERE create_month = MONTH(NOW() - INTERVAL 1 MONTH)
            AND create_year = YEAR(NOW() - INTERVAL 1 MONTH)
            AND customer_company_name = "Company") AS x
WHERE (create_month = MONTH(NOW() - INTERVAL 1 MONTH))
AND (create_year = YEAR(NOW() - INTERVAL 1 MONTH))
AND CUSTOMER_COMPANY_NAME = "Company"
group by CREATE_WEEKDAY