"Karthik" <[EMAIL PROTECTED]> wrote in
message news:[EMAIL PROTECTED]
> A question about "case when"
>
> i have the following table
> create table test(
>    id integer primary key,
>    category text,
>    rating integer
> )
> the "rating" column takes value 1 to 5,
> I need to get number of 1 to 5 rated records for each category and for
> this i used the query,
> select
>    category,
>    sum(case when rating=1 then 1 else 0 end) as onestar,
>    sum(case when rating=2 then 1 else 0 end) as twostar,
>    sum(case when rating=3 then 1 else 0 end) as threestar,
>    sum(case when rating=4 then 1 else 0 end) as fourstar,
>    sum(case when rating=5 then 1 else 0 end) as fivestar
> from test1
>    group by category
>
> i also tried the following query which also worked
> select
>    category,
>    sum(rating=1) as onestar,
>    sum(rating==2) as twostar,   //both =and == work
>    sum(rating=3) as threestar,
>    sum(rating=4) as fourstar,
>    sum(rating=5) as fivestar
> from test1
>    group by category
>
> which of the queries is better? are the two queries just the same
> behind the scenes?

They are essentially the same. Comparison operators return 1 for true 
and 0 for false.

Igor Tandetnik



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

Reply via email to