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

Bad alias table choice joining CTE [CORE3829] #4171

Closed
firebird-automations opened this issue Apr 24, 2012 · 12 comments
Closed

Bad alias table choice joining CTE [CORE3829] #4171

firebird-automations opened this issue Apr 24, 2012 · 12 comments

Comments

@firebird-automations
Copy link
Collaborator

Submitted by: Umberto Masotti (umasotti)

Duplicates CORE3611

Attachments:
TEST.fbk
test.sql

Following query incorrectly report sum (t1.qq) in column sum(t2.qq) when in CTE corresponding field is result of count(*), count(1) or sum(1).
Workaround is in CTE sum(iif(<anycondition>, 1, 1)).
Don't know if already solved, as found nothing related in tracker.
I'll upload test database.

with totalk (kk1, variant, tt, qq, mm, ff, f1, f2, f3, f4, f5) as
(select ll.kk1, ll.variant,
sum (iif(ll.selector_y_n='Y', 1, 0)),
count(*), -- <<<< problem here
-- count(1), -- <<<< this doesn't work either
-- sum(1), -- <<<<< this neither
-- sum(iif(ll.variant=1,1,1)), -- <<<< workaround
sum (iif(ll.selector_m_f='M' and ll.selector_y_n='Y', 1, 0)),
sum (iif(ll.selector_m_f='F' and ll.selector_y_n='Y', 1, 0)),
sum(iif(ll.selector_1_5='1' and ll.selector_y_n='Y', 1, 0)),
sum(iif(ll.selector_1_5='2' and ll.selector_y_n='Y', 1, 0)),
sum(iif(ll.selector_1_5='3' and ll.selector_y_n='Y', 1, 0)),
sum(iif(ll.selector_1_5='4' and ll.selector_y_n='Y', 1, 0)),
sum(iif(ll.selector_1_5='5' and ll.selector_y_n='Y', 1, 0))
from testcte ll
group by 1, 2 )
select
ff.kk1, ff.descrkk,
sum(http://t1.tt) "TT 1",
sum(http://t2.tt) "TT 2",
sum(t1.qq) "QQ 1", -- <<<< check this value
sum(t2.qq) "QQ 2", -- <<<< and this value
sum(http://t1.mm) "MM 1",
sum(http://t2.mm) "MM 2",
sum(t1.ff) "FF 1",
sum(t2.ff) "FF 2",
sum(t1.f1) "G1 1",
sum(t2.f1) "G1 2",
sum(t1.f2) "G2 1",
sum(t2.f2) "G2 2",
sum(t1.f3) "G3 1",
sum(t2.f3) "G3 2",
sum(t1.f4) "G4 1",
sum(t2.f4) "G4 2",
sum(t1.f5) "G5 1",
sum(t2.f5) "G5 2"
from testmain ff left outer join
totalk t1 on t1.kk1=ff.kk1 and t1.variant = 1
left outer join
totalk t2 on t2.kk1=ff.kk1 and t2.variant = 2
group by 1, 2;

@firebird-automations
Copy link
Collaborator Author

Modified by: Umberto Masotti (umasotti)

Attachment: TEST.fbk [ 12137 ]

@firebird-automations
Copy link
Collaborator Author

Modified by: Umberto Masotti (umasotti)

Attachment: test.sql [ 12138 ]

@firebird-automations
Copy link
Collaborator Author

Commented by: Umberto Masotti (umasotti)

To clarify, columns QQ1 and QQ 2 returns incorrectly same values, whereas should be completely different. Particularly, QQ 2 has same value of QQ 1 when should be different.

@firebird-automations
Copy link
Collaborator Author

Commented by: Umberto Masotti (umasotti)

Sean, count(*) is for each group of GROUP BY, and if you extract the query in CTE, you should see different values for the same KK1 field but different VARIANT field.
So in the main part of the query each join sums rows tagged for value 1 of VARIANT (in alias T1) and rows tagged with value 2 of the same fields (in alias T2). That sum is different with actual data or I've completely misunderstood use of CTE!

@firebird-automations
Copy link
Collaborator Author

Commented by: @asfernandes

Please test the latest 2.5.2 snapshot. There was CTE fixes in it, for example CORE3683.

@firebird-automations
Copy link
Collaborator Author

Commented by: @dyemanov

Adriano, the bug is reproducible with the current SVN checkout.

@firebird-automations
Copy link
Collaborator Author

Modified by: @asfernandes

Version: 2.5.0 [ 10221 ]

@firebird-automations
Copy link
Collaborator Author

Modified by: Sean Leyne (seanleyne)

Link: This issue duplicates CORE3611 [ CORE3611 ]

@firebird-automations
Copy link
Collaborator Author

Modified by: Sean Leyne (seanleyne)

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

resolution: Duplicate [ 3 ]

@firebird-automations
Copy link
Collaborator Author

Modified by: @pcisar

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

@firebird-automations
Copy link
Collaborator Author

Modified by: @pavel-zotov

QA Status: No test

@firebird-automations
Copy link
Collaborator Author

Modified by: @pavel-zotov

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

QA Status: No test => Done successfully

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