5

The best way to solve SQL if Condition

 2 years ago
source link: https://www.codesd.com/item/the-best-way-to-solve-sql-if-condition.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The best way to solve SQL if Condition

advertisements

I have a problem this is my table item

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

item A have begin qty 100 at first month, and balance qty at month 12 = 110 balance updating every month

i need to call a procedure to get data for every month report

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

so beginning value = balance last month any way to make this stored procedure simple? not using a lot of 'IF'

it's better using case ?

need some idea.. thanks


u can used Case statement to avoid the if statement

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK