On Tue, 7 Oct 2014 12:15:09 +0300
"Tony Papadimitriou" <to...@acm.org> wrote:

> Is there any an equivalent function to the MySQL 
> IF(condition,true_expr,false_expr) function?
> 
> For example, SELECT AGE,IF(AGE < 3,"BABY",IF(AGE <
> 18,"CHILD","ADULT"));
> 
> If not, please add to wish list :)

You can always solve problems like this by putting the data in a table:

create table stages
( age int primary key
, stage string not null
);
insert into stages values (3, 'baby'), (18, 'child'), (999, 'adult');

create view vstages as
select A.age, stage from (
       select a.age, min(s.age) as threshold
       from ages as a join stages as s
       on a.age <= s.age
       group by a.age
) as A join stages as s
on A.threshold = s.age;

$ sqlite3 -echo db <<< 'select * from vstages;'
select * from vstages;
age         stage     
----------  ----------
1           baby      
2           baby      
3           baby      
4           child     
5           child     
6           child     
7           child     
8           child     
9           child     
10          child     
11          child     
12          child     
13          child     
14          child     
15          child     
16          child     
17          child     
18          child     
19          adult     
20          adult     
21          adult     

Another form of the query joins the "stages" table to itself to create
a range, and uses BETWEEN to choose the label.  

HTH.  

--jkl
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to