desde hasta

02/05/2006 - 19:19 por claudio alabarce | Informe spam
Hola,

Tengo una consulta,
Tengo la siguiente tabla:

Numeros
940
941
942
943
8000
19075
19076
19077

y quiero devolver un string de la siguiente forma:
940 al 943 - 8000 - 19075 al 19077.

Se puede hacer un SP o Funcion que me devuelva este resultado sin utilizar
cursores?

Muchas gracias.

Script:
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[Table1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Table1]
GO
CREATE TABLE [dbo].[Table1] (
[Numeros] [int] NOT NULL
) ON [PRIMARY]
GO

Insert table1 (Numeros) values(940)
Insert table1 (Numeros) values(941)
Insert table1 (Numeros) values(942)
Insert table1 (Numeros) values(943)
Insert table1 (Numeros) values(8000)
Insert table1 (Numeros) values(19075)
Insert table1 (Numeros) values(19076)
Insert table1 (Numeros) values(19077)

Preguntas similare

Leer las respuestas

#1 Maxi [MVP]
02/05/2006 - 19:48 | Informe spam
Hola, y como serian los cortes?


Salu2
-
[Microsoft MVP] SQL Server
Orador para Culminis Latam
www.sqlgurus.org

MSN:

"claudio alabarce" <claudio escribió en
el mensaje news:
Hola,

Tengo una consulta,
Tengo la siguiente tabla:

Numeros
940
941
942
943
8000
19075
19076
19077

y quiero devolver un string de la siguiente forma:
940 al 943 - 8000 - 19075 al 19077.

Se puede hacer un SP o Funcion que me devuelva este resultado sin utilizar
cursores?

Muchas gracias.

Script:
if exists (select * from dbo.sysobjects where id > object_id(N'[dbo].[Table1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Table1]
GO
CREATE TABLE [dbo].[Table1] (
[Numeros] [int] NOT NULL
) ON [PRIMARY]
GO

Insert table1 (Numeros) values(940)
Insert table1 (Numeros) values(941)
Insert table1 (Numeros) values(942)
Insert table1 (Numeros) values(943)
Insert table1 (Numeros) values(8000)
Insert table1 (Numeros) values(19075)
Insert table1 (Numeros) values(19076)
Insert table1 (Numeros) values(19077)



Respuesta Responder a este mensaje
#2 Miguel Egea
02/05/2006 - 19:48 | Informe spam
Claudio!!! dios mio cuanto tiempo sin saber de tí, que placer volver a
leerte.

Con esta consulta en 2005 tienes una aproximación interesante, pero no
resuelve lo que quieres.
select numeros, Rank() over (partition by Numeros/1000 order by numeros)
a,Numeros/1000

From table1

Con esta sintaxis, más sencilla y válida para todas las versiones obtienes
casi lo que quieres

select min(numeros), max(numeros)

From table1

group by Numeros/1000

y con esta otra, justo lo que quieres

select case when inicio=fin then cast(inicio as varchar(100))

else cast (inicio as varchar(100)) + ' al ' + cast(fin as varchar(100))

End Rango

from (

select min(numeros) inicio, max(numeros) fin

From table1

group by Numeros/1000

) a






Al principio no te entendí bien, y creí que hablabas de formar parejas, como
no puedo resistirme pego el código aquí abajo.

select a.numeros,b.numeros from

(

select numeros, (row_number() over( order by numeros))+1 fila from Table1

) a

inner join

(

select numeros, (row_number() over( order by numeros)) fila from Table1

) b

on a.fila=b.fila

where a.fila%2=0

go


select numeros,

(Select min(Numeros) From Table1 t where t.Numeros>table1.numeros) Intervalo

from Table1

where

(Select count(Numeros) From Table1 t where t.Numeros<=table1.numeros)%2=1






"claudio alabarce" <claudio escribió en
el mensaje news:
Hola,

Tengo una consulta,
Tengo la siguiente tabla:

Numeros
940
941
942
943
8000
19075
19076
19077

y quiero devolver un string de la siguiente forma:
940 al 943 - 8000 - 19075 al 19077.

Se puede hacer un SP o Funcion que me devuelva este resultado sin utilizar
cursores?

Muchas gracias.

Script:
if exists (select * from dbo.sysobjects where id > object_id(N'[dbo].[Table1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Table1]
GO
CREATE TABLE [dbo].[Table1] (
[Numeros] [int] NOT NULL
) ON [PRIMARY]
GO

Insert table1 (Numeros) values(940)
Insert table1 (Numeros) values(941)
Insert table1 (Numeros) values(942)
Insert table1 (Numeros) values(943)
Insert table1 (Numeros) values(8000)
Insert table1 (Numeros) values(19075)
Insert table1 (Numeros) values(19076)
Insert table1 (Numeros) values(19077)



Respuesta Responder a este mensaje
#3 Miguel Egea
02/05/2006 - 19:53 | Informe spam
Por si no era eso exactamente aquí tienes algo más

declare @v varchar(max)

select @v =''

select @v=@v+(case when inicio=fin then cast(inicio as varchar(100)) +','

else cast (inicio as varchar(100)) + ' al ' + cast(fin as varchar(100)) +','

End )

from (

select min(numeros) inicio, max(numeros) fin

From table1

group by Numeros/1000

) a

select @v



Saludos

"Miguel Egea" escribió en el mensaje
news:%
Claudio!!! dios mio cuanto tiempo sin saber de tí, que placer volver a
leerte.

Con esta consulta en 2005 tienes una aproximación interesante, pero no
resuelve lo que quieres.
select numeros, Rank() over (partition by Numeros/1000 order by numeros)
a,Numeros/1000

From table1

Con esta sintaxis, más sencilla y válida para todas las versiones obtienes
casi lo que quieres

select min(numeros), max(numeros)

From table1

group by Numeros/1000

y con esta otra, justo lo que quieres

select case when inicio=fin then cast(inicio as varchar(100))

else cast (inicio as varchar(100)) + ' al ' + cast(fin as varchar(100))

End Rango

from (

select min(numeros) inicio, max(numeros) fin

From table1

group by Numeros/1000

) a






Al principio no te entendí bien, y creí que hablabas de formar parejas,
como no puedo resistirme pego el código aquí abajo.

select a.numeros,b.numeros from

(

select numeros, (row_number() over( order by numeros))+1 fila from Table1

) a

inner join

(

select numeros, (row_number() over( order by numeros)) fila from Table1

) b

on a.fila=b.fila

where a.fila%2=0

go


select numeros,

(Select min(Numeros) From Table1 t where t.Numeros>table1.numeros)
Intervalo

from Table1

where

(Select count(Numeros) From Table1 t where t.Numeros<=table1.numeros)%2=1






"claudio alabarce" <claudio escribió
en el mensaje news:
Hola,

Tengo una consulta,
Tengo la siguiente tabla:

Numeros
940
941
942
943
8000
19075
19076
19077

y quiero devolver un string de la siguiente forma:
940 al 943 - 8000 - 19075 al 19077.

Se puede hacer un SP o Funcion que me devuelva este resultado sin
utilizar
cursores?

Muchas gracias.

Script:
if exists (select * from dbo.sysobjects where id >> object_id(N'[dbo].[Table1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[Table1]
GO
CREATE TABLE [dbo].[Table1] (
[Numeros] [int] NOT NULL
) ON [PRIMARY]
GO

Insert table1 (Numeros) values(940)
Insert table1 (Numeros) values(941)
Insert table1 (Numeros) values(942)
Insert table1 (Numeros) values(943)
Insert table1 (Numeros) values(8000)
Insert table1 (Numeros) values(19075)
Insert table1 (Numeros) values(19076)
Insert table1 (Numeros) values(19077)







Respuesta Responder a este mensaje
#4 claudio alabarce
02/05/2006 - 19:58 | Informe spam
Que tal Miguel, tanto tiempo.

Muchas gracias por acordarte, espero poder volver a colaborar con el grupo,
ahora tengo que decirte que tu ayuda como siempre esta perfecta, es lo que
necesitabla.

Muchas gracias Miguel.

Un abrazo.

"Miguel Egea" escribió:

Claudio!!! dios mio cuanto tiempo sin saber de tí, que placer volver a
leerte.

Con esta consulta en 2005 tienes una aproximación interesante, pero no
resuelve lo que quieres.
select numeros, Rank() over (partition by Numeros/1000 order by numeros)
a,Numeros/1000

From table1

Con esta sintaxis, más sencilla y válida para todas las versiones obtienes
casi lo que quieres

select min(numeros), max(numeros)

From table1

group by Numeros/1000

y con esta otra, justo lo que quieres

select case when inicio=fin then cast(inicio as varchar(100))

else cast (inicio as varchar(100)) + ' al ' + cast(fin as varchar(100))

End Rango

from (

select min(numeros) inicio, max(numeros) fin

From table1

group by Numeros/1000

) a






Al principio no te entendí bien, y creí que hablabas de formar parejas, como
no puedo resistirme pego el código aquí abajo.

select a.numeros,b.numeros from

(

select numeros, (row_number() over( order by numeros))+1 fila from Table1

) a

inner join

(

select numeros, (row_number() over( order by numeros)) fila from Table1

) b

on a.fila=b.fila

where a.fila%2=0

go


select numeros,

(Select min(Numeros) From Table1 t where t.Numeros>table1.numeros) Intervalo

from Table1

where

(Select count(Numeros) From Table1 t where t.Numeros<=table1.numeros)%2=1






"claudio alabarce" <claudio escribió en
el mensaje news:
> Hola,
>
> Tengo una consulta,
> Tengo la siguiente tabla:
>
> Numeros
> 940
> 941
> 942
> 943
> 8000
> 19075
> 19076
> 19077
>
> y quiero devolver un string de la siguiente forma:
> 940 al 943 - 8000 - 19075 al 19077.
>
> Se puede hacer un SP o Funcion que me devuelva este resultado sin utilizar
> cursores?
>
> Muchas gracias.
>
> Script:
> if exists (select * from dbo.sysobjects where id > > object_id(N'[dbo].[Table1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
> drop table [dbo].[Table1]
> GO
> CREATE TABLE [dbo].[Table1] (
> [Numeros] [int] NOT NULL
> ) ON [PRIMARY]
> GO
>
> Insert table1 (Numeros) values(940)
> Insert table1 (Numeros) values(941)
> Insert table1 (Numeros) values(942)
> Insert table1 (Numeros) values(943)
> Insert table1 (Numeros) values(8000)
> Insert table1 (Numeros) values(19075)
> Insert table1 (Numeros) values(19076)
> Insert table1 (Numeros) values(19077)
>
>
>



Respuesta Responder a este mensaje
#5 claudio alabarce
02/05/2006 - 21:13 | Informe spam
Miguel,

estuve probando lo que me mandaste y si tengo los siguientes numeros no
funciona, puesto que el ejemplo que me mandaste divide en 1000, no se me
ocurre como hacerlo.

1151
1161
1162
1163
1164
1165
1169
1170
1174
1175
1176
1184
1185
1187
1188
1189

El resultado deberia ser

1151
1161 al 1176
1184 al 1189

Por si no quedo claro, si un numero no tiene consecutivo o un numero
anterior va solo, y sino el primero de la serie consecutiva hasta que termina.

Si no me explico me avisas y lo expreso nuevamente.

Gracias.



"Miguel Egea" escribió:

Por si no era eso exactamente aquí tienes algo más

declare @v varchar(max)

select @v =''

select @v=@v+(case when inicio=fin then cast(inicio as varchar(100)) +','

else cast (inicio as varchar(100)) + ' al ' + cast(fin as varchar(100)) +','

End )

from (

select min(numeros) inicio, max(numeros) fin

From table1

group by Numeros/1000

) a

select @v



Saludos

"Miguel Egea" escribió en el mensaje
news:%
> Claudio!!! dios mio cuanto tiempo sin saber de tí, que placer volver a
> leerte.
>
> Con esta consulta en 2005 tienes una aproximación interesante, pero no
> resuelve lo que quieres.
> select numeros, Rank() over (partition by Numeros/1000 order by numeros)
> a,Numeros/1000
>
> From table1
>
> Con esta sintaxis, más sencilla y válida para todas las versiones obtienes
> casi lo que quieres
>
> select min(numeros), max(numeros)
>
> From table1
>
> group by Numeros/1000
>
> y con esta otra, justo lo que quieres
>
> select case when inicio=fin then cast(inicio as varchar(100))
>
> else cast (inicio as varchar(100)) + ' al ' + cast(fin as varchar(100))
>
> End Rango
>
> from (
>
> select min(numeros) inicio, max(numeros) fin
>
> From table1
>
> group by Numeros/1000
>
> ) a
>
>
>
>
>
>
> Al principio no te entendí bien, y creí que hablabas de formar parejas,
> como no puedo resistirme pego el código aquí abajo.
>
> select a.numeros,b.numeros from
>
> (
>
> select numeros, (row_number() over( order by numeros))+1 fila from Table1
>
> ) a
>
> inner join
>
> (
>
> select numeros, (row_number() over( order by numeros)) fila from Table1
>
> ) b
>
> on a.fila=b.fila
>
> where a.fila%2=0
>
> go
>
>
> select numeros,
>
> (Select min(Numeros) From Table1 t where t.Numeros>table1.numeros)
> Intervalo
>
> from Table1
>
> where
>
> (Select count(Numeros) From Table1 t where t.Numeros<=table1.numeros)%2=1
>
>
>
>
>
>
> "claudio alabarce" <claudio escribió
> en el mensaje news:
>> Hola,
>>
>> Tengo una consulta,
>> Tengo la siguiente tabla:
>>
>> Numeros
>> 940
>> 941
>> 942
>> 943
>> 8000
>> 19075
>> 19076
>> 19077
>>
>> y quiero devolver un string de la siguiente forma:
>> 940 al 943 - 8000 - 19075 al 19077.
>>
>> Se puede hacer un SP o Funcion que me devuelva este resultado sin
>> utilizar
>> cursores?
>>
>> Muchas gracias.
>>
>> Script:
>> if exists (select * from dbo.sysobjects where id > >> object_id(N'[dbo].[Table1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
>> drop table [dbo].[Table1]
>> GO
>> CREATE TABLE [dbo].[Table1] (
>> [Numeros] [int] NOT NULL
>> ) ON [PRIMARY]
>> GO
>>
>> Insert table1 (Numeros) values(940)
>> Insert table1 (Numeros) values(941)
>> Insert table1 (Numeros) values(942)
>> Insert table1 (Numeros) values(943)
>> Insert table1 (Numeros) values(8000)
>> Insert table1 (Numeros) values(19075)
>> Insert table1 (Numeros) values(19076)
>> Insert table1 (Numeros) values(19077)
>>
>>
>>
>
>



Respuesta Responder a este mensaje
Ads by Google
Help Hacer una preguntaSiguiente Respuesta Tengo una respuesta
Search Busqueda sugerida