Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

select su tabella con valore riga precedente

43 views
Skip to first unread message

GIulia

unread,
May 9, 2016, 5:23:06 PM5/9/16
to
Ho una tabella non ordinata di n righe con la generica righa cosi composta

indice valore
f(n) x

con f(n) monotona crescente f(0) =0

e vorrei ottenere n righe cosi composte:

f(n) f(n-1) valore


ad esempio
12 30
25 3
24 40
40 50

e ottenere

12 0 30
24 12 40
25 24 3
40 25 50

e cosi via.

Qualche idea in sql puro?

Giulia

Giacomo Degli Esposti

unread,
May 10, 2016, 5:11:43 AM5/10/16
to
Che DB stai usando? In MS SQL puoi fare qualcosa del genere
(non so se e' "puro" o sfrutta qualche peculiarita' del server MS...)



create table _tabella_( f_n integer not null primary key , valore integer );

insert into _tabella_
select 12, 30
union all
select 25, 3
union all
select 24, 40
union all
select 40, 50 ;


select T.f_n, isnull(max(T1.f_n),0) as f_n_1, T.valore
from _tabella_ T left join _tabella_ T1 on t.f_n > T1.f_n
group by T.f_n, T.valore
order by t.f_n;



ciao
Giacomo




Giulia

unread,
May 10, 2016, 6:18:22 AM5/10/16
to

> Che DB stai usando? In MS SQL puoi fare qualcosa del genere
> (non so se e' "puro" o sfrutta qualche peculiarita' del server MS...)
>
Mysql

Giacomo Degli Esposti

unread,
May 10, 2016, 9:33:15 AM5/10/16
to
On Tuesday, May 10, 2016 at 12:18:22 PM UTC+2, Giulia wrote:
> > Che DB stai usando? In MS SQL puoi fare qualcosa del genere
> > (non so se e' "puro" o sfrutta qualche peculiarita' del server MS...)
>
> Mysql

hai provato con la mia query a vedere se funziona o da qualche errore?

ciao
Giacomo

Giulia

unread,
May 10, 2016, 10:22:05 AM5/10/16
to
Incorrect parameter count in the call to native function 'isnull'


Giacomo Degli Esposti

unread,
May 10, 2016, 11:38:46 AM5/10/16
to
Metti coalesce() al posto di isnull()

ciao
Giacomo

Giulia

unread,
May 13, 2016, 8:08:24 AM5/13/16
to
> Metti coalesce() al posto di isnull()

SELECT T.f_n, coalesce( max( T1.f_n ) , 0 ) AS f_n_1, T.valore
FROM _tabella_ AS T
LEFT JOIN _tabella_ AS T1 ON T.f_n > T1.f_n
GROUP BY T.f_n, T.valore
ORDER BY T.f_n




f_n f_n_1 valore
12 0 30
24 12 40
25 24 3
40 25 50


Ora devo valutare la velocita su 600000 elementi!!


Giulia

Giulia

unread,
May 13, 2016, 8:36:38 AM5/13/16
to
> Ora devo valutare la velocita su 600000 elementi!!
>
>
> Giulia

Sembra estremamente inefficiente , per adesso la soluzione di crearmi due tabelle in memoria
con la colonna n esplicitata e gią con le clause yyyy applicate


SET @rn=0;
CREATE TEMPORARY TABLE IF NOT EXISTS
t (INDEX(rn))
ENGINE = memory
AS ( select f_n,@rn:=@rn+1 AS rn FROM xxxx where yyyy )

CREATE TEMPORARY TABLE IF NOT EXISTS
t2 (INDEX(rn))
ENGINE = memory
AS ( select f_n,@rn:=@rn+1 AS rn FROM xxxx where yyyy )

select t2.f_n,Valore from t,t2 where t.rn = t2.rn-1;



e' a confronto praticamente istantanea...








0 new messages