Ver Mensaje Individual
  #16 (permalink)  
Antiguo 03/04/2013, 21:50
Avatar de jpogsistem
jpogsistem
 
Fecha de Ingreso: diciembre-2001
Mensajes: 2.405
Antigüedad: 22 años, 5 meses
Puntos: 4
Respuesta: Dos Order By Con Union o alguna solución

Bien muchachos agradeciéndoles como siempre su valiosa ayuda, dejo la solución que desarrollé y con la que me siento cómodo :

- Crear 1 SP para el query 1
- Crear 1 SP para el query 2
- Crear 1 SP para unir los query 1 y 2

Query1
Código PHP:
CREATE procedure [dbo].[validarEstadoCuenta_1]
@
idEstadoCuenta int 
as
select reg.regulacion_id,reg.regulacion_resolucion_real,reg.regulacion_cuota_real
reg.resolucion_fecha_ejecutoria_real as 'fecha ejecutoria real',reg.resolucion_fecha_ejecutoria_real
from sgva_Regulacion reg
sgva_estado_de_cuenta est
where reg
.regulacion_nit est.estado_nit_empresa and reg.regulacion_estado_id 
and reg.resolucion_fecha_ejecutoria_real >=  DATEADD(yy,-3,est.estado_fecha_creacion 
and 
reg.resolucion_fecha_ejecutoria_real <= est.estado_fecha_creacion 
and est.id_estado = @idEstadoCuenta
order by reg
.resolucion_fecha_ejecutoria_real asc 

Query2
Código PHP:
CREATE procedure [dbo].[validarEstadoCuenta_2]
@
idEstadoCuenta int 
as
select top(1reg.regulacion_id,reg.regulacion_resolucion_real,reg.regulacion_cuota_realreg.resolucion_fecha_ejecutoria_real as 'fecha ejecutoria real',reg.resolucion_fecha_ejecutoria_real
from sgva_Regulacion reg
sgva_estado_de_cuenta est
where reg
.regulacion_nit est.estado_nit_empresa and reg.regulacion_estado_id 
and reg.resolucion_fecha_ejecutoria_real <=  DATEADD(yy,-3,est.estado_fecha_creacion 
and 
est.id_estado = @idEstadoCuenta
order by reg
.resolucion_fecha_ejecutoria_real desc 

Query que hace el Union
Código PHP:
ALTER procedure [dbo].[validarEstadoCuenta]
@
idEstadoCuenta int 
as

declare @
validarEstadoCuenta_1 table (regulacion_id intregulacion_resolucion_real varchar(50), regulacion_cuota_real intfecha_ejecutoria_real smalldatetimeresolucion_fecha_ejecutoria_real smalldatetime)
declare @
validarEstadoCuenta_2 table (regulacion_id intregulacion_resolucion_real varchar(50), regulacion_cuota_real intfecha_ejecutoria_real smalldatetimeresolucion_fecha_ejecutoria_real smalldatetime)

insert into  @validarEstadoCuenta_2
exec validarEstadoCuenta_2 
@idEstadoCuenta

insert into  
@validarEstadoCuenta_1
exec validarEstadoCuenta_1 
@idEstadoCuenta




select regulacion_id
regulacion_resolucion_realregulacion_cuota_real,CONVERT(nvarchar(10),resolucion_fecha_ejecutoria_real,103) as 'fecha ejecutoria real'resolucion_fecha_ejecutoria_real
from 
@validarEstadoCuenta_2
union all
select regulacion_id
regulacion_resolucion_realregulacion_cuota_real,CONVERT(nvarchar(10),resolucion_fecha_ejecutoria_real,103) as 'fecha ejecutoria real'resolucion_fecha_ejecutoria_real
from 
@validarEstadoCuenta_1 

Gracias de nuevo. Saludos.
__________________
EL LIMITE ES EL UNIVERSO