如何在当前select语句中添加多列作为新字段?

xxhby3vn  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(358)

我有以下疑问:

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)格式中,这似乎会减慢查询速度,而且很快就会查询生产环境。这是我唯一的选择吗?谢谢

xn1cxnb4

xn1cxnb41#

最简单的选项是子查询:

select
    t.*,
    [FTI-ACT Payment] + [FTI-Bulk] + [FTI-DED] + [FTI-FMS] + [FTI-MS] + [FTI-Other] + [FTI-DR] + [FTI-CR] + [New Payments] AS total
from (
    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
) t

从问题和查询的Angular 来看,没有办法避免枚举要查询的列 sum() .
不过,也有可能建立一个额外的 sum() 与…结合在一起的表情 or 现有系统中的所有个别条件 sum() s。这看起来像:

sum(case 
    when 
        (c.BILLINGCLASSIFICATION = 'ACT Payment' AND c.TransType2 = 'Customer')
        or (c.BILLINGCLASSIFICATION = 'Bulk' AND c.TransType2 = 'Customer')
        or (c.BILLINGCLASSIFICATION = 'Dedicated' AND c.TransType2 = 'Customer')
        or (c.BILLINGCLASSIFICATION = 'FMS' AND c.TransType2 = 'Customer')
        or (c.BILLINGCLASSIFICATION =  'Managed Service' AND c.TransType2 = 'Customer')
        or (c.BILLINGCLASSIFICATION not in ('ACT Payment','Bulk','Dedicated','FMS','Managed Service') AND c.TransType2 = 'Customer')
        or (c.BILLINGCLASSIFICATION not in ('ACT Payment','Corrected') and c.Balance > 0 AND c.TransType2 = 'Customer')
        or (c.BILLINGCLASSIFICATION not in ('ACT Payment','Cancelling') and c.Balance < 0 AND c.TransType2 = 'Customer')
        or ( c.VOUCHER like 'ARP%' and c.LedgerJournalACType = 'Bank' and c.TransType2 = 'Payment')
    then convert(float,c.Balance) 
    else 0 
end) as total

我们可以通过分解一些条件来缩短这个过程。根据您的实际数据,可以使用更整洁的选项来缩短条件。

相关问题