【发布时间】:2016-08-12 19:34:14
【问题描述】:
我有一张这样的桌子
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(borrower) 时,我得到的都是零。如果我尝试做 count(borrower)/13 这是月数,它不会给我想要的结果。 理想情况下,零点应为空,平均值应根据实际值的数量(非空)计算 基本上我的问题是。我如何添加一个平均列做这个数据透视表
谢谢
【问题讨论】:
-
听起来 NULLIF(ColumnName,0) 对你有用 SUM(ValueColumnName) / IFNULL(NULLIF(COUNT(NULLIF(ColumnName,0)),0),1)
-
嗯..我会试试看。您提到的 SUM 函数需要应用于数值正确的列,例如 count。有没有办法可以汇总数据透视表中的现有列?
-
是的,总和必须是数字列。你提到你想要平均认为它必须是一个数字列......平均是某事物的总和除以出现次数。老实说,我无法说出您想要的平均值,所以我不肯定如何回答比一般指导更多的答案。如果您想发布示例数据和期望的结果,我可以更具体地回答您的问题
-
count(borrower)/13应该可以工作。为什么你认为没有? -
count(borrower)/13 确实有效,但是我希望仅根据正值计算平均值。这意味着 0 值不会影响平均值。