Subject | Re: [firebird-support] adding positive and negative values in differents columns |
---|---|
Author | masotti |
Post date | 2009-04-14T12:06:17Z |
Hi,
Sergio H. Gonzalez ha scritto:
is negative and you'll have a SUM of negative numbers. What will can go
wrong is NULL data: SUM ignores NULL, but don't know what happens with
SUM Abs(D.importe) with a nullable field.
To be sure, I'd change in
SELECT
D.id,
Sum(case when D.importe > 0 then D.importe else 0 end) as positive,
-Sum(case when D.importe < 0 then D.importe else 0 end) as negative
FROM MyTable D GROUP BY D.id ORDER BY D.id
or
SELECT
D.id,
Sum(case when D.importe > 0 then D.importe else 0 end) as positive,
Sum(case when D.importe < 0 then -D.importe else 0 end) as negative
FROM MyTable D GROUP BY D.id ORDER BY D.id
Ciao.
Mimmo.
Sergio H. Gonzalez ha scritto:
> Something wrong with my question? probably in not too clear... All I'd like toI'd change only use of Abs(), it's not needed: you are sure that number
> know if this is the *best* way of slpitting a sum column in two, acording on its
> value (> 0 or < 0)
>
> Thanks, and sorry if I'm not clear enough!!
>
>
>> SELECT
>> D.id,
>> Sum(case when D.importe > 0
>> then D.importe
>> else 0 end) as positive,
>> Sum(case when D.importe < 0
>> then Abs(D.importe)
>> else 0 end) as negative
>> FROM
>> MyTable D
>> GROUP BY
>> D.id
>> ORDER BY
>> D.id
>>
is negative and you'll have a SUM of negative numbers. What will can go
wrong is NULL data: SUM ignores NULL, but don't know what happens with
SUM Abs(D.importe) with a nullable field.
To be sure, I'd change in
SELECT
D.id,
Sum(case when D.importe > 0 then D.importe else 0 end) as positive,
-Sum(case when D.importe < 0 then D.importe else 0 end) as negative
FROM MyTable D GROUP BY D.id ORDER BY D.id
or
SELECT
D.id,
Sum(case when D.importe > 0 then D.importe else 0 end) as positive,
Sum(case when D.importe < 0 then -D.importe else 0 end) as negative
FROM MyTable D GROUP BY D.id ORDER BY D.id
Ciao.
Mimmo.