lundi 8 juin 2020

Using IF or Case with multiple in SQL Statement

I want to do something like this

this works Select ID, number, cost from table order by number

number can be 2-xtimes but the cost and the same

1 A33  66.50

2 A34  73.50

3 A34  73.50

But I want to have

1 A33  66.50

2 A34  73.50

3 A34  0

I want to change it in the Sql to 0 I tried distinct or if then else.

I want to do something like this

declare @oldcost int;

 Select ID, number, 
    if(cost=@oldcost) then
       cost=0;
    else
       cost=cost;
    end if
    @oldcost=cost;

from table order by number

How can I do it in SQL?

Aucun commentaire:

Enregistrer un commentaire