我在按周结束、月至今和年至今的总计进行分组时遇到了问题。目前在我的查询中,我得到了这个输出
(No column name) Received App # Received App $ (No column name) Loan Closed # Loan Closed $
Applications Received 2 $500,000.00 New Loans Closed 1 $250,000.00
而不是上面的,我想得到这样的输出
Applications Received 2 $500,000.00
New Loans Closed 1 $250,000.00
请看下面我的查询:
select
'Applications Received',
count(case when pd.Status_four_Dt between '1/1/2014' and '2/1/2014' then pd.loan_amt end) as 'Received App #',
sum(case when pd.Status_four_Dt between '1/1/2014' and '2/1/2014' then pd.loan_amt end) as 'Received App $',
'New Loans Closed',
count(case when pd.Status_ten_Dt between '1/1/2014' and '2/1/2014' then pd.loan_amt end) as 'Loan Closed #',
sum(case when pd.Status_ten_Dt between '1/1/2014' and '2/1/2014' then pd.loan_amt end) as 'Loan Closed $'
from pipe_deal pd
left join pipe_quote pq
on pd.id = pq.deal_id
where
pd.record_type_lkup ='dl'
and pd.company_identity_id = 2
and ((pd.status_lkup_id in(14,16,17,18,19,20,21,22,23) and pq.active_yn = 'y') or (pd.status_lkup_id = 13 and pq.active_yn is null) or (pd.status_lkup_id = 24 and (pq.active_yn = 'y' or pq.active_yn is null)))
and pd.delete_yn = 'N'
and pd.Status_One_Dt > '01/01/2008'
and (pd.EMP_OFFICE_LKUP_Id <> 192 or pd.EMP_OFFICE_LKUP_Id is null)
--group by
--(case
--when pd.Status_four_Dt>0 then 'Received Application'
--when pd.Status_ten_Dt>0 then 'Loans Closed'
--end)
所以基本上,收到的申请和关闭的新贷款合计了前一周结束的一周的数量,我只是无法按照输出应该喜欢的方式对其进行分组 收到的申请 2 $500,000.00 已关闭的新贷款 1 $250,000.00
我想要一种方法来对收到的申请和关闭的新贷款进行分组,将它们放在行而不是列中。
请帮忙。
最佳答案
我对您的数据结构不够熟悉,无法为您提供准确的查询,但您可以使用 UNION ALL 完成此操作。
您将只想选择应用程序收到的数据(看起来是 COUNT(*) 和 SUM(pd.loan_amt) WHERE pd.Status_four_Dt BETWEEN 等),然后将该结果与给您的结果合并为您提供申请的那个(看起来是 COUNT(*) 和 SUM(pd.loan_amt) WHERE pd.Status_ten_Dt 之间...等)。您可以只为第一列选择文本,例如,“收到的申请”AS Description、COUNT(*) 和 SUM(loan amount) WHERE pd.Status_four_Dt BETWEEN 等,然后将结果与关闭的新贷款的结果合并。
有道理吗?
关于mysql - 按总计 SQL 对周结束数字进行分组;每月;年初至今的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21634700/