Home > Back-end >  I want to select only positive values from SQL data
I want to select only positive values from SQL data

Time:08-02

I want to select only positive values from SQL data.

Example :

Select A, (10-A) as B   
From Table1 where B >= 0

Result will be:

enter image description here

but I want the result with only positive values, like

enter image description here

I tried but I am not getting a proper result.

Please if anyone can help me, it would be of a great help.

Thank you Nag

CodePudding user response:

select A, B
FROM
    (Select A, (10-A) as B From Table1) t
WHERE t.B > 0

You can just go direct

select A, (10-A) as B
FROM
    Table1
WHERE (10 - A) > 0

The difference would be important - if you need ORDER BY

Fiddle

create table table1 (a int);
insert into table1 values (1);
insert into table1 values (2);
insert into table1 values (3);
insert into table1 values (4);
insert into table1 values (20);
insert into table1 values (30);
insert into table1 values (40);

result

A B
1 9
2 8
3 7
4 6

CodePudding user response:

Select A, abs(10-A) as B   
From Table1 
  •  Tags:  
  • sql
  • Related