[Lazarus] SQL question

Martin lazarus at mfriebe.de
Mon Feb 8 14:20:05 CET 2010


On 08/02/2010 13:12, Ondrej wrote:
> Here's the SQL statement without the CTE:
>
> select
>   a.type,
>   (
>     select
>       count(*) count_2008
>     from table
>     where (type = a.type) and (extract(year from date) = 2008)
>   ),
>   (
>     select
>       count(*) count_2009
>     from table
>     where (type = a.type) and (extract(year from date) = 2009)
>   )
> from table a;
>    

Do you need the subqueries?

select
    a.type,
    sum( if(extract(year from date) = 2008, 1, 0) ) c2008,
    sum( if(extract(year from date) = 2009, 1, 0) ) c2009
from Table a
WHERE a.date between '1.1.2008' and '31.12.2009';









More information about the Lazarus mailing list