【问题标题】:How to add multiple columns in current select statement as a new field?如何在当前选择语句中添加多个列作为新字段?
【发布时间】:2020-10-16 11:36:13
【问题描述】:

我有以下疑问:

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

【问题讨论】:

    标签: sql sql-server tsql select pivot


    【解决方案1】:

    最简单的选择是子查询:

    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
    

    作为问题和查询的立场,您无法避免枚举您想要sum() 的列。

    但是,可能可以构建一个额外的sum() 表达式,将现有sum()s 中的所有单独条件与or 结合在一起。这看起来像:

    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
    

    我们可以尝试通过分解某些条件来缩短此时间。根据您的实际数据,可能会有更简洁的选项来缩短条件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多