Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncorrect calculation of Select with case statement [CORE3565] #3919

Closed
firebird-automations opened this issue Jul 31, 2011 · 7 comments
Closed

Comments

@firebird-automations
Copy link
Collaborator

Submitted by: Dmitry Lendel (dmitryle)

Query
select
Rem.iddepot,
Rem.idcommodity,
case :qq1
when 1 then Rem.costprice
else null end as f1,
case :qq2
when 1 then Rem.saleprice
else null end as f2,
case :qq3
when 1 then Rem.usebydate
else null end as f3,
case :qq4
when 1 then Rem.idcolor
else null end as f4,
case :qq5
when 1 then Rem.barcode
else null end as f5,
case :qq6
when 1 then Rem.param1
else null end as f6,
case :qq7
when 1 then Rem.param2
else null end as f7,
Sum(Rem.QUANTITY)
from remainder Rem
where IDDate<=:LIDDate
group by 1,2,3,4,5,6,7,8,9
having Sum(Rem.QUANTITY)>0

returns uncorrect result if fields idcolor or usebydate have null and non null values
for example 0,0,null,1,0,0 and so on

if to change query with code
case :qq3
when 1 then coalesce(Rem.usebydate,current_date )
else null end as f3,
case :qq4
when 1 then coalesce(Rem.idcolor,0)
else null end as f4,

result will correct
I can send database with example.

@firebird-automations
Copy link
Collaborator Author

Commented by: Sean Leyne (seanleyne)

You haven't said specifically what the expected results should be for a given condition.

Further, although the database will be appreciated, should it be needed, you need to provide specific details to reproduce, the current details are not enough for that purpose.

It seems that you believe that if a REM.{field} is NULL that the results should have a non-NULL value, you are mistaken. SQL defines very specific rules for the handling of NULLs.

@firebird-automations
Copy link
Collaborator Author

Commented by: @hvlad

I understand nothing. Please provide DDL, some data and expected results.

@firebird-automations
Copy link
Collaborator Author

Commented by: Dmitry Lendel (dmitryle)

Test database is here http://www.bagel.com.ua/download/testdata.zip
Run Query
Select Sum(FF.F2*FF.Quantity)
From (
select
Rem.iddepot,
Rem.idcommodity,
case :qq1
when 1 then Rem.costprice
else null end as f1,
case :qq2
when 1 then Rem.saleprice
else null end as f2,
case :qq3
when 1 then Rem.usebydate
else null end as f3,
case :qq4
when 1 then Rem.idcolor
else null end as f4,
case :qq5
when 1 then Rem.barcode
else null end as f5,
case :qq6
when 1 then Rem.param1
else null end as f6,
case :qq7
when 1 then Rem.param2
else null end as f7,
Sum(Rem.QUANTITY) as Quantity
from remainder Rem
where IDDate<=:LIDDate
group by 1,2,3,4,5,6,7,8,9
having Sum(Rem.QUANTITY)>0
) AS FF

with params qq1 = 1 qq2=2 qq3 is null qq4 is null qq5 is null qq6 is null qq7 is null LIDDate=current_date
Result 137499.705249999999

with params qq1 = 1 qq2=2 qq3=1 qq4=1 qq5 is null qq6 is null qq7 is null LIDDate=current_date
Result 137668.580249999999

@firebird-automations
Copy link
Collaborator Author

Commented by: Dmitry Lendel (dmitryle)

Select Sum(SalePrice*Quantity) from Remainder
Result 137499.705249999999

@firebird-automations
Copy link
Collaborator Author

Commented by: Dmitry Lendel (dmitryle)

I am sorry. I found my mistake. Thank you.

@firebird-automations
Copy link
Collaborator Author

Modified by: @dyemanov

status: Open [ 1 ] => Resolved [ 5 ]

resolution: Won't Fix [ 2 ]

@firebird-automations
Copy link
Collaborator Author

Modified by: @pcisar

status: Resolved [ 5 ] => Closed [ 6 ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant