我有以下疑问:
select
sum(case when c.BILLINGCLASSIFICATION = 'ACT Payment' AND c.TransType2 = 'Customer' THEN convert(float,c.Balance) ELSE 0 END) AS [FTI-ACT Payment],
sum(case when c.BILLINGCLASSIFICATION = 'Bulk' AND c.TransType2 = 'Customer' THEN convert(float,c.Balance) ELSE 0 END) As [FTI-Bulk],
sum(case when c.BILLINGCLASSIFICATION = 'Dedicated' AND c.TransType2 = 'Customer' THEN convert(float,c.Balance) ELSE 0 END) As [FTI-DED],
sum(case when c.BILLINGCLASSIFICATION = 'FMS' AND c.TransType2 = 'Customer' THEN convert(float,c.Balance) ELSE 0 END) As [FTI-FMS],
sum(case when c.BILLINGCLASSIFICATION = 'Managed Service' AND c.TransType2 = 'Customer' THEN convert(float,c.Balance) ELSE 0 END) As [FTI-MS],
sum(case when c.BILLINGCLASSIFICATION not in ('ACT Payment','Bulk','Dedicated','FMS','Managed Service') AND c.TransType2 = 'Customer' THEN convert(float,c.Balance) ELSE 0 END) As [FTI-Other],
sum(case when c.BILLINGCLASSIFICATION not in ('ACT Payment','Corrected') and c.Balance>0 AND c.TransType2 = 'Customer' THEN convert(float,c.Balance) ELSE 0 END) As [FTI-DR],
sum(case when c.BILLINGCLASSIFICATION not in ('ACT Payment','Cancelling') and c.Balance < 0 AND c.TransType2 = 'Customer' THEN convert(float,c.Balance) ELSE 0 END) As [FTI-CR],
sum(case when c.VOUCHER like 'ARP%' and c.LedgerJournalACType = 'Bank' and c.TransType2 = 'Payment' THEN convert(float,c.Balance) ELSE 0 END) As [New Payments]
from [AX2cTest].[dbo].[CUSTTRANS_V] c
使用此输出:
FTI-ACT Payment FTI-Bulk FTI-DED FTI-FMS FTI-MS FTI-Other FTI-DR FTI-CR New Payments
-122995.14 114521.67 728830.9 2793.46 53137.07 3000 902352.58 -69.48 0
我需要添加一个平衡字段,简单地添加所有这些字段。不过,这只是一个相当长的查询的示例。我试图避免将每个冗长的语句添加到(col1+col2)格式中,这似乎会减慢查询速度,而且很快就会查询生产环境。这是我唯一的选择吗?谢谢
1条答案
按热度按时间xn1cxnb41#
最简单的选项是子查询:
从问题和查询的Angular 来看,没有办法避免枚举要查询的列
sum()
.不过,也有可能建立一个额外的
sum()
与…结合在一起的表情or
现有系统中的所有个别条件sum()
s。这看起来像:我们可以通过分解一些条件来缩短这个过程。根据您的实际数据,可以使用更整洁的选项来缩短条件。