数据透视表上的MySQL平均列

时间:2016-08-12 19:34:14

标签: mysql sql

我有一张类似这样的表

loanprocessorusername     borrower     fundingdate
jonsmith                  borrower2    8/10/15
jonsmith                  borrower2    8/10/16
username3                 borrower4    9/9/15

依此类推。 基本上我的任务是创建一个数据透视表,以计算贷款处理器用户名在给定年份内按月完成的贷款数量。我正在使用借款人获得计数,实际上可以在任何领域。 PK是贷款号码

无论如何,这是我走了多远:

SELECT loanprocessorusername, 
   Count(CASE 
           WHEN Month(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 8 
                AND Year(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 2015 
         THEN 
           borrower 
           ELSE NULL 
         END)      AS Aug2015, 
   Count(CASE 
           WHEN Month(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 9 
                AND Year(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 2015 
         THEN 
           borrower 
           ELSE NULL 
         END)      AS Sep2015, 
   Count(CASE 
           WHEN Month(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 10 
                AND Year(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 2015 
         THEN 
           borrower 
           ELSE NULL 
         END)      AS Oct2015, 
   Count(CASE 
           WHEN Month(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 11 
                AND Year(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 2015 
         THEN 
           borrower 
           ELSE NULL 
         END)      AS Nov2015, 
   Count(CASE 
           WHEN Month(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 12 
                AND Year(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 2015 
         THEN 
           borrower 
           ELSE NULL 
         END)      AS Dec2015, 
   Count(CASE 
           WHEN Month(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 1 
                AND Year(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 2016 
         THEN 
           borrower 
           ELSE NULL 
         END)      AS Jan2016, 
   Count(CASE 
           WHEN Month(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 2 
                AND Year(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 2016 
         THEN 
           borrower 
           ELSE NULL 
         END)      AS Feb2016, 
   Count(CASE 
           WHEN Month(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 3 
                AND Year(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 2016 
         THEN 
           borrower 
           ELSE NULL 
         END)      AS Mar2106, 
   Count(CASE 
           WHEN Month(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 4 
                AND Year(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 2016 
         THEN 
           borrower 
           ELSE NULL 
         END)      AS Apr2016, 
   Count(CASE 
           WHEN Month(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 5 
                AND Year(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 2016 
         THEN 
           borrower 
           ELSE NULL 
         END)      AS May2016, 
   Count(CASE 
           WHEN Month(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 6 
                AND Year(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 2016 
         THEN 
           borrower 
           ELSE NULL 
         END)      AS Jun2016, 
   Count(CASE 
           WHEN Month(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 7 
                AND Year(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 2016 
         THEN 
           borrower 
           ELSE NULL 
         END)      AS Jul2016, 
   Count(CASE 
           WHEN Month(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 8 
                AND Year(Str_to_date(status_fundingdate, '%m/%d/%Y')) = 2016 
         THEN 
           borrower 
           ELSE NULL 
         END)      AS Aug2016, 
   Count(borrower) AS GrandTotal 
FROM   dataset 
WHERE  loan_lienposition = 'first' 
GROUP  BY loanprocessorusername; 

我想为平均值添加最后一列,但是当我尝试做avg(借用者)时,我得到全部为零。如果我试图计算(借款人)/ 13这是几个月它没有给我想要的结果。 理想情况下,零应为null,并且应根据实际值的数量计算平均值(非null) 基本上我的问题是。如何添加平均列执行此数据透视表

由于

1 个答案:

答案 0 :(得分:0)

SELECT loanprocessorusername, 
       CONCAT(
               Month(Str_to_date(status_fundingdate, '%m/%d/%Y'),
               Year(Str_to_date(status_fundingdate, '%m/%d/%Y')
             ) AS MY,
       COUNT(1) AS COunt_Of_Loans_per_each_user
  FROM table 
GROUP BY 1,2
;