--
You received this message because you are subscribed to the Google Groups "firebird-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebird-suppo...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/firebird-support/9be8c601-8d7d-cb61-f23f-6aab7b483012%40lawinegevaar.nl.
Hi Johan,
CREATE TABLE TEST (
N VARCHAR(10),
V INTEGER
);
INSERT INTO TEST (N, V)
VALUES ('Person1', 80);
INSERT INTO TEST (N, V)
VALUES ('Person1', 70);
INSERT INTO TEST (N, V)
VALUES ('Person1', 50);
INSERT INTO TEST (N, V)
VALUES ('Person2', 88);
INSERT INTO TEST (N, V)
VALUES ('Person2', 84);
INSERT INTO TEST (N, V)
VALUES ('Person2', 83);
INSERT INTO TEST (N, V)
VALUES ('Person2', 70);
INSERT INTO TEST (N, V)
VALUES ('Person3', 90);
INSERT INTO TEST (N, V)
VALUES ('Person4', 56);
INSERT INTO TEST (N, V)
VALUES ('Person4', 45);
CREATE DESCENDING INDEX TEST_IDX1 ON TEST (N, V);
with
persons as (
select distinct tp.n pers
from test tp)
select p.pers,
coalesce((select first 1 t1.v
from test t1
where t1.n = p.pers
order by t1.v desc), 0) +
coalesce((select first 1 skip 1 t1.v
from test t1
where t1.n = p.pers
order by t1.v desc), 0)
from persons p
PERS ADD
Person1 150
Person2 172
Person3 90
Person4 101
or, if your table is big enough and distinct natural read is too slow you need an another (ascending) index:
CREATE INDEX TEST_IDX2 ON TEST (N);
and the query is:
with recursive persons as (
select tx1.n pers
from (select first 1 t1.n
from test t1
where t1.n > ''
order by t1.n) tx1
union all
select
(select first 1 t2.n
from test t2
where t2.n > persons.pers
order by t2.n)
from persons
where persons.pers is not null)
select p.pers,
coalesce((select first 1 t1.v
from test t1
where t1.n = p.pers
order by t1.v desc), 0) +
coalesce((select first 1 skip 1 t1.v
from test t1
where t1.n = p.pers
order by t1.v desc), 0)
from persons p
where p.pers is not null
András
--
You received this message because you are subscribed to the Google Groups "firebird-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
firebird-suppo...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/firebird-support/b6ff52f5-383d-4ae2-ba21-994af925b4ben%40googlegroups.com.