sql-server - 解决 SQL if 条件的最佳方法

标签 sql-server stored-procedures

我有问题 这是我的表项

Item |Begin | in01 | in02 | ..| in12 | out01 | out02 |..|out12 | Balance
A    | 100  | 10   | 20   | ..| 10   |   5   | 10    |..| 5    | 110

项目 A 在第一个月的开始数量为 100,在第 12 个月的余额数量 = 110 余额每月更新

我需要调用一个过程来获取每个月报告的数据

sample for report for month 01
  Beginning| In |   Out | balance           
      100  | 10 |   5   | 105       

 sample for report for month 02
  Beginning| In |   Out | balance           
     105   | 20 |   10  |  115      
 sample for report for month 12
  Beginning    | In |   Out | balance           
balance month 11   | xx |   xx  |  11 + in - out

所以期初值=上月余额 有什么方法可以使这个存储过程简单吗?不使用很多“如果”

使用 case 更好?

需要一些想法..谢谢

最佳答案

你可以使用Case语句来避免if语句

select
case when beginning value = balance then 1st Option else 2nd Option as Test
from tblTest

关于sql-server - 解决 SQL if 条件的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32195268/

相关文章:

performance - pl/sql存储过程…执行时间在哪里?

mysql - 检查数据库中是否存在表 - 存储过程 - MySQL

sql-server - 上次执行存储过程的时间

sql-server - SQL Server/Azure DB - 添加 JOIN 只会使 180~ 行的查询速度减慢几秒

c# - 参数验证失败。不可能为所有参数提供有效值。 (rsParameterError) sql 报告 2008

sql-server - 当涉及多个聚合时, 'Worktable' 的读取计数异常巨大

sql-server - 获取表中所有组的相互值 (SQL Server)

sql-server - Spark 和 MSSQL 插入速度

c# - ASP.NET SQL Server 存储过程返回消息

mysql - 工作台在以下查询中引发错误。为什么?