Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Incorrect result using UNLIST and 2 CTE #8718

Closed
@sim1984

Description

SQL> show version;
ISQL Version: WI-T6.0.0.1093 Firebird 6.0 9a5e091
Server version:
Firebird/Windows/AMD/Intel/x64 (access method), version "WI-T6.0.0.1093 Firebird 6.0 9a5e091"
Firebird/Windows/AMD/Intel/x64 (remote server), version "WI-T6.0.0.1093 Firebird 6.0 9a5e091/tcp (DESKTOP-MCH5AOF)/P20:C"
Firebird/Windows/AMD/Intel/x64 (remote interface), version "WI-T6.0.0.1093 Firebird 6.0 9a5e091/tcp (DESKTOP-MCH5AOF)/P20:C"
on disk structure version 14.0

Query with correct result:

with recursive
 t(n) as (
 select 0 from rdb$database
 union all
 select n + 1 from t where n < 9
 ),
 t2(n) as (
 select 100000 * t1.n + 10000 * t2.n + 1000 * t3.n + 100 * t4.n + 10 * t5.n + t6.n
 from t t1, t t2, t t3, t t4, t t5, t t6
 )
select sum(n), count(*) from t2;
 SUM COUNT
============================================= =====================
 499999500000 1000000

Let's try to rewrite this example using UNLIST:

with
 t(n) as (
 select n from unlist('0,1,2,3,4,5,6,7,8,9' returning int) as u(n)
 ),
 t2(n) as (
 select 100000 * t1.n + 10000 * t2.n + 1000 * t3.n + 100 * t4.n + 10 * t5.n + t6.n
 from t t1, t t2, t t3, t t4, t t5, t t6
 )
select sum(n), count(*) from t2;
 SUM COUNT
============================================= =====================
 <null> 1000000

Why does SUM return NULL?

Interestingly, if you rewrite this query without using the second CTE (t2), the result will be correct.

with
 t(n) as (
 select n from unlist('0,1,2,3,4,5,6,7,8,9' returning int) as u(n)
 )
select sum(100000 * t1.n + 10000 * t2.n + 1000 * t3.n + 100 * t4.n + 10 * t5.n + t6.n), count(*)
from t t1, t t2, t t3, t t4, t t5, t t6;
 SUM COUNT
============================================= =====================
 499999500000 1000000

Metadata

Metadata

Type

Projects

No projects

Milestone

No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      AltStyle によって変換されたページ (->オリジナル) /