分组依据,何时及交叉申请

时间:2019-03-14 01:14:03

标签: sql reporting-services

我在下面有这个查询,它可以正常工作,但是问题是它返回所有记录,在语法和aggregate(sum)活动的情况下,我想要的是按特定的[Type]分组,因此它仅返回特定情况下所有交易的总和。我尝试测试Group by的另一个查询,但随后似乎无法使交叉应用工作。您能告诉我我可能做错了什么吗?或如何将分组依据整合到交叉应用查询中?

交叉应用

    select v.type, left(M.Account, 4) as Entity, m.activity
from Test t cross apply
     (values (case when right(m.account, 9) = '1110.0130' then 'A'
                   when right(m.account, 9) = '1110.0131' then 'B'
                   when right(m.account, 9) = '1110.0132' then 'C'
                   when right(m.account, 9) between '3000.0000' and '3999.9999' then 'D' 
              end)
     ) v(type)
where T.Accounting_Date between '2019-02-01' and '2019-02-28' and
     (Left(M.Account,4) IN (@Entity) OR '' IN (@Entity)) and 
      v.type is not null;

分组依据

Select 

Left(A.Account,4) Entity,Right(A.Account,9) As Base_Account, round(sum(A.Activity),2) As Total, Type

From (

        Select Account, debit+credit AS Activity, Accounting_Date,
            Case When right(account,9) = '1110.0130' then 'A' 
                 When right(account,9) = '1110.0131' then 'B'
                 When Substring(account,6,3) >'000' and Substring(account,6,3) < '200' and right(account,9) between '3000.0000' and '3999.9999' then 'D'
            End AS [Type] From GLT_CURRENT__TRANSACTION 
        Where left(Account,4) = 'XYZ' and Accounting_Date >= '02/01/19' and Accounting_Date <= '02/28/19' AND substring(account,13,4)<'2000') A 

Group By Account, Type

Order by Base_Account

1 个答案:

答案 0 :(得分:0)

我认为您只需要从account中删除group by

select v.type, sum(m.activity)
from Test t cross apply
     (values (case when right(m.account, 9) = '1110.0130' then 'A'
                   when right(m.account, 9) = '1110.0131' then 'B'
                   when right(m.account, 9) = '1110.0132' then 'C'
                   when right(m.account, 9) between '3000.0000' and '3999.9999' then 'D' 
              end)
     ) v(type)
where T.Accounting_Date between '2019-02-01' and '2019-02-28' and
     (Left(M.Account,4) IN (@Entity) OR '' IN (@Entity)) and 
      v.type is not null
group by type