Muchas gracias les dejo la consulta
> /* Select * From [dbo].[OWTR] T0 */
> Declare @FecIni DateTime
> Declare @FecFin DateTime
> Declare @HorIni Int
> Declare @HorFin Int
> /* Where */
> select @FecIni /* T0.[DocDate] */= '[%0]'
> /* And */
> Select @FecFin /* T0.[DocDate] */= '[%1]'
> /* And */
> Select @HorIni /* T0.[DocTime] */= '[%2]'
> /* And */
> Select @HorFin /* T0.[DocTime] */= '[%3]'
> SELECT DISTINCT T.ItemCode, T.ItemName,
> A.OnHand + ISNULL((SELECT SUM(Y.Quantity)
> FROM OWTR X INNER JOIN WTR1 Y ON Y.DocEntry=X.DocEntry
> WHERE Y.ItemCode=T.ItemCode AND Y.FromWhsCod = 'MP' AND Y.WhsCode = 'WIP' AND
> (case when X.DocTime<59 then cast(convert(char(8),X.docdate ,112)+'00'+substring(convert(char(2) ,X.doctime ),1,3) as nvarchar(100))
> else
>
> case when X.doctime >959 then cast(convert(char(8),X.docdate ,112)+convert(char(4) ,X.doctime ) as nvarchar(100))
>
> else
> cast(convert(char(8),X.docdate ,112)+'0'+substring(convert(char(3) ,X.doctime ),1,3) as nvarchar(100))end
> end)
> between
> (case when @HorIni <59 then cast(convert(char(8),@fecini ,112)+'00'+substring(convert(char(2) ,@horini ),1,3) as nvarchar(100))
> else
> case when @HorIni >959 then cast(convert(char(8),@fecini ,112)+convert(char(4) ,@HorIni ) as nvarchar(100))
>
> else
> cast(convert(char(8),@fecini ,112)+'0'+substring(convert(char(3) ,@HorIni ),1,3) as nvarchar(100))end
> end)
> and
>
> (case when @HorFin <59 then cast(convert(char(8),@fecfin ,112)+'00'+substring(convert(char(2) ,@horFin ),1,3) as nvarchar(100))
> else
> case when @HorFin >959 then cast(convert(char(8),@fecFin ,112)+convert(char(4) ,@HorFin ) as nvarchar(100))
>
> else
> cast(convert(char(8),@fecFin ,112)+'0'+substring(convert(char(3) ,@HorFin ),1,3) as nvarchar(100))end
> end)),0) as 'Historial Estock',
> IsNull((SELECT Sum(B.Quantity)
> FROM [dbo].PDN1 B INNER JOIN [dbo].OPDN A1 ON B.DocEntry = A1.DocEntry
> WHERE B.ItemCode=T.ItemCode AND A1.Canceled='N' AND
>
> (case when A1.DocTime<59 then cast(convert(char(8),A1.docdate ,112)+'00'+substring(convert(char(2) ,A1.doctime ),1,3) as nvarchar(100))
> else
>
> case when A1.doctime >959 then cast(convert(char(8),A1.docdate ,112)+convert(char(4) ,A1.doctime ) as nvarchar(100))
>
> else
> cast(convert(char(8),A1.docdate ,112)+'0'+substring(convert(char(3) ,A1.doctime ),1,3) as nvarchar(100))end
> end)
> between
> (case when @HorIni <59 then cast(convert(char(8),@fecini ,112)+'00'+substring(convert(char(2) ,@horini ),1,3) as nvarchar(100))
> else
> case when @HorIni >959 then cast(convert(char(8),@fecini ,112)+convert(char(4) ,@HorIni ) as nvarchar(100))
>
> else
> cast(convert(char(8),@fecini ,112)+'0'+substring(convert(char(3) ,@HorIni ),1,3) as nvarchar(100))end
> end)
> and
>
> (case when @HorFin <59 then cast(convert(char(8),@fecfin ,112)+'00'+substring(convert(char(2) ,@horFin ),1,3) as nvarchar(100))
> else
> case when @HorFin >959 then cast(convert(char(8),@fecFin ,112)+convert(char(4) ,@HorFin ) as nvarchar(100))
>
> else
> cast(convert(char(8),@fecFin ,112)+'0'+substring(convert(char(3) ,@HorFin ),1,3) as nvarchar(100))end
> end)),0) as 'Entradas de Mescancia',
> IsNull((SELECT Sum(B.Quantity)
> FROM RPD1 B INNER JOIN ORPD A2 ON B.DocEntry = A2.DocEntry
> WHERE B.ItemCode=T.ItemCode AND A2.Canceled='N' AND
> (case when A2.DocTime<59 then cast(convert(char(8),A2.docdate ,112)+'00'+substring(convert(char(2) ,A2.doctime ),1,3) as nvarchar(100))
> else
>
> case when A2.doctime >959 then cast(convert(char(8),A2.docdate ,112)+convert(char(4) ,A2.doctime ) as nvarchar(100))
>
> else
> cast(convert(char(8),A2.docdate ,112)+'0'+substring(convert(char(3) ,A2.doctime ),1,3) as nvarchar(100))end
> end)
> between
> (case when @HorIni <59 then cast(convert(char(8),@fecini ,112)+'00'+substring(convert(char(2) ,@horini ),1,3) as nvarchar(100))
> else
> case when @HorIni >959 then cast(convert(char(8),@fecini ,112)+convert(char(4) ,@HorIni ) as nvarchar(100))
>
> else
> cast(convert(char(8),@fecini ,112)+'0'+substring(convert(char(3) ,@HorIni ),1,3) as nvarchar(100))end
> end)
> and
>
> (case when @HorFin <59 then cast(convert(char(8),@fecfin ,112)+'00'+substring(convert(char(2) ,@horFin ),1,3) as nvarchar(100))
> else
> case when @HorFin >959 then cast(convert(char(8),@fecFin ,112)+convert(char(4) ,@HorFin ) as nvarchar(100))
>
> else
> cast(convert(char(8),@fecFin ,112)+'0'+substring(convert(char(3) ,@HorFin ),1,3) as nvarchar(100))end
> end)),0) as 'Devoluciones',
> ISNULL((SELECT SUM(Y.Quantity)
> FROM OWTR X INNER JOIN WTR1 Y ON Y.DocEntry=X.DocEntry
> WHERE Y.ItemCode=T.ItemCode AND Y.FromWhsCod = 'MP' AND Y.WhsCode = 'WIP' AND
> (case when X.DocTime<59 then cast(convert(char(8),X.docdate ,112)+'00'+substring(convert(char(2) ,X.doctime ),1,3) as nvarchar(100))
> else
>
> case when X.doctime >959 then cast(convert(char(8),X.docdate ,112)+convert(char(4) ,X.doctime ) as nvarchar(100))
>
> else
> cast(convert(char(8),X.docdate ,112)+'0'+substring(convert(char(3) ,X.doctime ),1,3) as nvarchar(100))end
> end)
> between
> (case when @HorIni <59 then cast(convert(char(8),@fecini ,112)+'00'+substring(convert(char(2) ,@horini ),1,3) as nvarchar(100))
> else
> case when @HorIni >959 then cast(convert(char(8),@fecini ,112)+convert(char(4) ,@HorIni ) as nvarchar(100))
>
> else
> cast(convert(char(8),@fecini ,112)+'0'+substring(convert(char(3) ,@HorIni ),1,3) as nvarchar(100))end
> end)
> and
>
> (case when @HorFin <59 then cast(convert(char(8),@fecfin ,112)+'00'+substring(convert(char(2) ,@horFin ),1,3) as nvarchar(100))
> else
> case when @HorFin >959 then cast(convert(char(8),@fecFin ,112)+convert(char(4) ,@HorFin ) as nvarchar(100))
>
> else
> cast(convert(char(8),@fecFin ,112)+'0'+substring(convert(char(3) ,@HorFin ),1,3) as nvarchar(100))end
> end)),0) as 'Cantidad Entregada',
> A.OnHand as 'Inventario'
> FROM OITM T Inner JOin OITW A on T.ItemCode = A.ItemCode
> where T.SellItem = 'N' and T.PrchSeItem = 'Y' AND A.WhsCode = 'MP'
> GROUP BY T.ItemCode, T.ItemName,A.OnHand,T.U_TIPO_DE_VENTA
> ORDER BY T.ItemCode