[英]MySQL Average Column On Pivot Table
我有一张桌子,像这样
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(借款人)时,我得到的都是零。 如果我尝试做count(borrower)/ 13,这是月数,它没有给我想要的结果。 理想情况下,零应该为空,并且应该根据实际值的数量(非空)计算平均值。基本上我的问题是。 如何添加平均列以执行此数据透视表
谢谢
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
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.