【问题标题】:SQL conditional addition or subtractionSQL条件加减法
【发布时间】:2009-09-15 21:40:11
【问题描述】:

我有一张表,我需要在其中对某个字段进行一些简单的数学运算。我在 SQL 中尝试了嵌套的 SELECTS、IF/THEN 或 CASE 语句,但无济于事。

示例表:
wYear, yMonth, Account, Usage, Serv_code
2008, 9, 12345, 1000, 香蕉
2008, 9, 12345, 5000, 香蕉
2008, 9, 12345, 4000, 苹果
2007, 5, 54321, 1500, 香蕉


我需要按 wYear、yMonth、Account 分组并添加“Usage” WHERE Serv_code = 'Banana'...

选择 wYear、yMonth、Account、SUM(Usage) 作为“Totals”
来自我的表
WHERE Serv_code = '香蕉'
GROUP BY wYear, yMonth, Account

但是我碰壁的地方是 - 如果“Serv_code”确实等于“Apple”(WHERE Serv_code = 'Apple'),我需要从 SUM(Usage)中减去“Apple”Usage。

使用上表的示例结果:
2008, 9, 12345, 2000
2007, 5, 54321, 1500

当谈到 SQL 时,我是个 hack,答案很简单,但那是其中之一。

【问题讨论】:

  • 如果您包含用于测试数据的 create table 和 insert 语句,则回答此类问题会更​​容易。

标签: sql


【解决方案1】:

您可以根据 Serv_code 中的值使用 case 语句获取正值或负值:

select wYear, yMonth, Account, sum(Usage * case Serv_code when 'Apple' then -1 else 1 end) as "Totals"
from myTable
where Serv_code in ('Banana', 'Apple')
group by wYear, yMonth, Account

【讨论】:

    【解决方案2】:

    我希望这是你想要的:

    SELECT 
        banana.wYear, 
        banana.yMonth, 
        banana.Account, 
        totals_banana - totals_apple AS totals
    FROM
    (
        SELECT wYear, yMonth, Account, SUM(Usage) as "totals_banana"
        FROM myTable 
        WHERE Serv_code = 'Banana'
        GROUP BY wYear, yMonth, Account
    ) banana
    INNER JOIN 
    (
        SELECT wYear, yMonth, Account, SUM(Usage) as "totals_apple"
        FROM myTable 
        WHERE Serv_code = 'Apple'
        GROUP BY wYear, yMonth, Account
    ) apple 
    ON 
         banana.wYear = apple.wYear 
         AND banana.yMonth = apple.yMonth 
         AND banana.Account = apple.Account
    

    【讨论】:

      【解决方案3】:
      SELECT wYear, yMonth, Account, SUM(case when Serv_code  = 'Banana' then
      Usage when Serv_Code = 'Apple' then -Usage else 0 end) as "Totals"
      FROM myTable
      
      GROUP BY wYear, yMonth, Account
      

      测试友好版本

      Declare @myTable table (wYear int, ymonth int, Account varchar(20), 
      Usage int, Serv_Code varchar(20))
      
      Insert into @mytable values (2008, 9, '12345', 1000, 'Banana')
      Insert into @mytable values (2008, 9, '12345', 5000, 'Banana')
      Insert into @mytable values (2008, 9, '12345', 4000, 'Apple')
      Insert into @mytable values (2007, 5, '54321', 1500, 'Banana')
      
      SELECT wYear, yMonth, Account, SUM(case when Serv_code  = 'Banana' then
      Usage when Serv_Code = 'Apple' then -Usage else 0 end) as "Totals"
      FROM @myTable
      
      GROUP BY wYear, yMonth, Account
      
      
      wYear   yMonth  Account Totals
      2007    5   54321   1500
      2008    9   12345   2000
      

      【讨论】:

        【解决方案4】:

        我怀疑它的效率,但这应该可以解决问题

        SELECT 
            wYear, yMonth, Account, 
            (
              (SELECT SUM(Usage) FROM myTable AS m 
                WHERE m.wYear=wYear AND 
                      m.yMonth=yMonth AND 
                      m.Account=Account AND 
                      m.Serv_code='Banana') -
              (SELECT SUM(Usage) FROM myTable AS m 
                WHERE m.wYear=wYear AND 
                      m.yMonth=yMonth AND 
                      m.Account=Account AND 
                      m.Serv_code='Apple')
           ) as "Totals"
        FROM myTable 
        GROUP BY wYear, yMonth, Account
        

        【讨论】:

          【解决方案5】:

          也许这可行 -

          选择 wYear、yMonth、Account、 SUM(当 Usage = 'Apple' then usage*-1 else Usage end)为“Totals” 从我的表 WHERE Serv_code = '香蕉' GROUP BY wYear, yMonth, Account

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-12-26
            • 2019-10-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-05-14
            • 1970-01-01
            相关资源
            最近更新 更多