简体   繁体   English

按合计SQL分组以获取周末数字; 每月的; 年初至今的数字

[英]Group by Totals SQL for week ending numbers; monthly; year to date numbers

I am having trouble right way to group by totals for week ending, month to date and year to date. 我在按周结束,本月迄今和本年迄今的总计进行分组时遇到了麻烦。 Currently with my query, I get this output 当前与我的查询,我得到此输出

(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

Instead of above, I would like to get my output like this 而不是上面,我想得到这样的输出

Applications Received 2  $500,000.00 
New Loans Closed      1  $250,000.00 

Please see my query below: 请在下面查看我的查询:

    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)

So basically Applications Received and New Loans Closed totals the number for week ending for the previous week, I am just not able to group it in a way that the output should like Applications Received 2 $500,000.00 New Loans Closed 1 $250,000.00 因此,基本上收到的申请和已关闭的新贷款总额为上周结束的一周的总数,我无法按照输出类似于希望收到的申请2的方式将其分组$ 500,000.00已关闭的新贷款1 $ 250,000.00

I want a way to group applications received and new loans closed where they are put in rows instead of columns. 我想要一种将收到的申请和新的贷款关闭的方式进行分组的方法,它们放在行而不是列中。

Please help. 请帮忙。

I'm not familiar enough with the structure of your data to give you the exact query, but you can accomplish this using UNION ALL. 我对您的数据结构不太熟悉,无法为您提供确切的查询,但是您可以使用UNION ALL来完成此操作。

You'll want to select only the applications received data (which would appear to be COUNT(*) and SUM(pd.loan_amt) WHERE pd.Status_four_Dt BETWEEN etc) and then union that result with the one that gives you the the one that gives you the applications (which appears to be COUNT(*) and SUM(pd.loan_amt) WHERE pd.Status_ten_Dt between... etc). 您只想选择应用程序接收到的数据(看起来像是COUNT(*)和SUM(pd.loan_amt)WHERE pd.Status_four_Dt BETWEEN等),然后将该结果与一个给出的结果相结合。为您提供应用程序(似乎是COUNT(*)和SUM(pd.loan_amt),其中pd.Status_ten_Dt在...之间)。 You can select just text for your first column, eg, "Applications Received" AS Description, COUNT(*) and SUM(loan amount) WHERE pd.Status_four_Dt BETWEEN etc and then union that result with the one for the new loans closed. 您可以仅在第一列中选​​择文本,例如,“ AS Application Description”,“ COUNT(*)”和“ SUM(loan amount)WHERE pd.Status_four_Dt BETWEEN”等,然后将结果与已关闭的新贷款合并。

Make sense? 说得通?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM