You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
DDL:
recreate table tb(q int, v int); commit;
insert into tb
with
x as(
select 1 q, 1 v from rdb$database union all
select 1 q, 7 v from rdb$database
)
select * from x;
commit;
----
recreate table tv(v int, n varchar(10), c varchar(10) ); commit;
insert into tv
with
x as(
select 1 v, 'a1' n, 'r' c from rdb$database union all
select 7 v, 'a7' n, 'b' c from rdb$database
)
select * from x;
commit;
QUERY:
with
tc as(
select
tb.q, tb.v, tv.c,
dense_rank () over (partition by tb.q, tv.c order by tb.v) rk,
--dense_rank () over (partition by tv.c order by tb.v) rk,
case when tv.c = 'r' then tv.n end r
from tb
join tv on tv.v = tb.v
where tb.q=1
)
select q, rk , max(r)
from tc
where 0=0 -- <<<<<<<< NOTE: this is always true and can NOT have any effect on result of query!
group by q, rk
One more sample, but in this case the 'dummy condition' like WHERE 0=0 does not help.
the problem is in PARTITION BY clause in sum()over():
DDL:
recreate table tv(id int primary key); commit;
recreate table tb(vid int references tv, qid int); commit;
insert into tv values(10);
insert into tv values(20);
insert into tv values(30);
insert into tv values(40);
commit;
insert into tb values(20,100);
insert into tb values(20,200);
insert into tb values(20,300);
insert into tb values(30,400);
commit;
QUERY:
select id,max(qid) qid_max
from(
select v.*, b.*
from tv v
left join(
select b.*
,sum(1)over(partition by qid) dummy -- this lead to WRONG result (no rows)
--,sum(qid)over() dummy -- this is OK
from tb b
) b on v.id=b.vid
)
--where 0=0 -- does NOT help in this case!
group by id
having max(qid) is null;
RESULT: no rows (wrong!).
Correct result:
ID QID_MAX
=== ============
10 <null>
40 <null>
PS. No effect if we remove FOREIGN KEY constraint.
Submitted by: @pavel-zotov
DDL:
recreate table tb(q int, v int); commit;
insert into tb
with
x as(
select 1 q, 1 v from rdb$database union all
select 1 q, 7 v from rdb$database
)
select * from x;
commit;
----
recreate table tv(v int, n varchar(10), c varchar(10) ); commit;
insert into tv
with
x as(
select 1 v, 'a1' n, 'r' c from rdb$database union all
select 7 v, 'a7' n, 'b' c from rdb$database
)
select * from x;
commit;
QUERY:
with
tc as(
select
tb.q, tb.v, tv.c,
dense_rank () over (partition by tb.q, tv.c order by tb.v) rk,
--dense_rank () over (partition by tv.c order by tb.v) rk,
case when tv.c = 'r' then tv.n end r
from tb
join tv on tv.v = tb.v
where tb.q=1
)
select q, rk , max(r)
from tc
where 0=0 -- <<<<<<<< NOTE: this is always true and can NOT have any effect on result of query!
group by q, rk
UNION ALL
select q, rk , max(r)
from tc
group by q, rk;
RESULT:
============ ===================== ========
1 1 <null>
1 1 a1
(two rows in this output should be equal but they aren`t)
The text was updated successfully, but these errors were encountered: