Sybase SQL Anywhere - Ayuda, select sin top ni order by

 
Vista:

Ayuda, select sin top ni order by

Publicado por Mty (1 intervención) el 14/11/2014 19:01:24
Hola!! tengo un problema, tengo una tabla solo con 3 columnas fecha_registro, folio y cambio, el folio es un numero consecutivo y el cambio una letra C o vacio, necesito encontrar el maximo y minimo dentro del folio antes y despues de cada cambio, es decir, teniendo los siguientes valores

FECHA FOLIO CAMBIO
20141105 1
20141105 2 C
20141105 3
20141106 4
20141106 5 C
20141107 6 C
20141108 7
20141109 8
20141109 9
20141109 10 C

el resultado debe ser:

MIN MAX COUNT
1 1 1
3 4 2
7 9 3

no puedo utilizar el order by ya que el query debe resultar una tabla derivada que se utilizara posteriormente, y la funcion top no la acepta la version de sybase, en los foros encontre la siguiente forma de obtener el minimo pero por el order by igualmente no puedo utilizarlo

SELECT fecha, folio, cambio ,MIN(fecha)
FROM mi tabla
GROUP BY folio,cambio
HAVING folio=min(folio)
ORDER BY folio


espero puedan ayudarme muchisimas gracias
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Ayuda, select sin top ni order by

Publicado por Gustavo (6 intervenciones) el 08/03/2015 07:59:02
=====TABLA===== ============================================
create table testA (
fecha date not null ,
folio int not null ,
cambio char(1) null
)
=====CONTENIDO DE LA TABLA =================================
insert into testA values('2014-11-05',1,null)
insert into testA values('2014-11-05',2,'C')
insert into testA values('2014-11-05',3,null)
insert into testA values('2014-11-06',4,null)
insert into testA values('2014-11-06',5,'C')
insert into testA values('2014-11-07',6,'C')
insert into testA values('2014-11-08',7,null)
insert into testA values('2014-11-09',8,null)
insert into testA values('2014-11-09',9,null)
insert into testA values('2014-11-09',10,'C')
=====VISTA ==================================================
create view v_testA_cambio as
select '1970-01-01' fecha,0 folio_inicial,'C' cambio,(select min(folio) from testA where cambio='C' and folio>0) folio_final union
select *,case when (select min(folio) from testA where cambio='C' and folio>a.folio) is null then (select max(folio) from testA)+1 else (select min(folio) from testA where cambio='C' and folio>a.folio) end from testA a where cambio='C'
=====CONSULTA FINAL ========================================
select min(folio),max(folio),count(*) from v_testA_cambio a join testA b on b.folio between a.folio_inicial and a.folio_final
where b.cambio is null
group by folio_inicial
order by folio_inicial




Espero te sea de utilidad, leelo y comprendelo, saludos..
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar