on 07-30-2014 3:10 PM
Hola amigos de la comunidad SAP quisiera que por favor me colaboraran con un problema que me tiene un poco cabezón hace varios días, tengo una consulta en sap es un poco larga pero pues es que la verdad no se como acortarla, lo que hace la consulta es cojer los artículos vendidos de X marca y restarle los artículos devueltos para asi tener la cantidad exacta que se ha vendido en tres meses ademas de eso coje el tiempo de reposición de la marca, me saca el promedio de venta mensual de la marca, para cuanto tiempo me alcanza lo que esta en stock y cuanto seria la cantidad ideal que debo comprar según las ventas para tener el stock ideal de ese articulo, bueno en fin la consulta funciona siempre y cuando ponga el nombre de la marca por ejemplo asi: T0.[ItmsGrpNam]= 'GAP' hay funciona pero si en vez de eso hago esto T0.[ItmsGrpNam] =[%0] me arroja un error mejor dicho aquí les pongo el código y el error que me saca cuando intento poner la variable:
Select A.ID_adicional, A.Numero_de_articulo, A.Descripcion, A.Grupo, A.Linea, A.Subgrupo,
CASE WHEN
((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias > 0
THEN ((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias
WHEN ((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias < 0
THEN (((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias) *0 END AS Sugerido,
(A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion AS Proyeccion,
A.Existencias,
A.Existencias /((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))) as Capacidad_Inventario,
(A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0)) as Consumo_Total,
(A.Ventas - ISNULL(B.Devoluciones,0)) AS Ventas_ultimo_3_meses,
A.Tiempo_Reposicion
FROM
(
SELECT DISTINCT T1.[SWW] as ID_adicional, T0.[ItemCode] AS Numero_de_articulo, T0.[Dscription] as Descripcion, T2.[ItmsGrpNam] as Grupo, T3.[FirmName] as Linea, T1.[U_Subgrupo] as Subgrupo,
T1.[OnHand] AS Existencias,
SUM(T0.[Quantity]) AS Ventas,
T1.[LeadTime]/30 as Tiempo_Reposicion
FROM [dbo].[INV1] T0
INNER JOIN OITM T1 ON T0.ItemCode = T1.ItemCode
INNER JOIN OITB T2 ON T1.ItmsGrpCod = T2.ItmsGrpCod
INNER JOIN OMRC T3 ON T1.FirmCode = T3.FirmCode
WHERE T2.[ItmsGrpNam]='GAP' and T0.[DocDate] >= (getdate()-90) and T0.[DocDate] <= getdate()
GROUP BY T0.[ItemCode], T2.[ItmsGrpNam], T1.[OnHand], T1.[LeadTime], T0.[Dscription],T1.[SWW],T3.[FirmName], T1.[U_Subgrupo]
)A
left Join
(
SELECT T0.[ItemCode] AS ITEM_DEVUELTO, SUM(T0.[Quantity]) AS Devoluciones
FROM RIN1 T0
WHERE T0.[DocDate] >=(getdate()-90) AND T0.[DocDate] <= getdate()
GROUP BY T0.[ItemCode]
)B
On A.Numero_de_articulo=B.ITEM_DEVUELTO
GROUP BY A.Ventas, B.Devoluciones, A.ID_adicional, A.Numero_de_articulo, A.Descripcion, A.Grupo, A.Existencias, A.Linea,A.Subgrupo, A.Tiempo_Reposicion
HAVING (A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0)) > 0
Order By A.Linea, A.Subgrupo
Entiendo que el código es un poco largo pero no pude hacer menos si se fijan la parte que esta en rojo, si yo dejo el codigo asi me funciona perfecto en sap sin lios, pero lo que quiero es que la persona escoja la marca que desea concultar, al cambiar el WHERE T2.[ItmsGrpNam]='GAP' por WHERE T2.[ItmsGrpNam]=[%0] me arroja el siguiente error:
1). [Microsoft][SQL Server Native Client 10.0][SQL Server]Must specify table to select from. 2). [Microsoft][SQL Server Native Client 10.0][SQL Server]Statement 'Alertas recibidas' (OAIB) (s) could not be prepared.
espero no haberme enredado tanto y que me puedan colaborar es lo único que me falta para poner andar el código, y pues si tienen una forma de que el código sea mas corto también les agradecería.
Mil gracias y espero sus comentarios
Lendder intenta poniéndole ponle comillas sencillas a la variable para que te acepte alfanumérico así: ('[%0]'), si aun así el problema persiste trabajalo en SQL, inicialmente guarda la información declarando variables tipo nvarchar y en comillas sencillas prueba si funciona.
Slds
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Que pena molestar tanto pero ya probe lo que me dijiste y nada mire me quedo asi:
Select A.ID_adicional, A.Numero_de_articulo, A.Descripcion, A.Grupo, A.Linea, A.Subgrupo,
CASE WHEN
((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias > 0
THEN ((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias
WHEN ((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias < 0
THEN (((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias) *0 END AS Sugerido,
(A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion AS Proyeccion,
A.Existencias,
A.Existencias /((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))) as Capacidad_Inventario,
(A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0)) as Consumo_Total,
(A.Ventas - ISNULL(B.Devoluciones,0)) AS Ventas_ultimo_3_meses,
A.Tiempo_Reposicion
FROM
(
SELECT DISTINCT T1.[SWW] as ID_adicional, T0.[ItemCode] AS Numero_de_articulo, T0.[Dscription] as Descripcion, T2.[ItmsGrpNam] as Grupo, T3.[FirmName] as Linea, T1.[U_Subgrupo] as Subgrupo,
T1.[OnHand] AS Existencias,
SUM(T0.[Quantity]) AS Ventas,
T1.[LeadTime]/30 as Tiempo_Reposicion
FROM [dbo].[INV1] T0
INNER JOIN [dbo].[OITM] T1 ON T0.ItemCode = T1.ItemCode
INNER JOIN [dbo].[OITB] T2 ON T1.ItmsGrpCod = T2.ItmsGrpCod
INNER JOIN [dbo].[OMRC] T3 ON T1.FirmCode = T3.FirmCode
WHERE T2.[ItmsGrpNam]=[%0] and T0.[DocDate] >= (getdate()-90) and T0.[DocDate] <= getdate()
GROUP BY T0.[ItemCode], T2.[ItmsGrpNam], T1.[OnHand], T1.[LeadTime], T0.[Dscription],T1.[SWW],T3.[FirmName], T1.[U_Subgrupo]
)A
left Join
(
SELECT T0.[ItemCode] AS ITEM_DEVUELTO, SUM(T0.[Quantity]) AS Devoluciones
FROM [dbo].[RIN1] T0
WHERE T0.[DocDate] >=(getdate()-90) AND T0.[DocDate] <= getdate()
GROUP BY T0.[ItemCode]
)B
On A.Numero_de_articulo=B.ITEM_DEVUELTO
GROUP BY A.Ventas, B.Devoluciones, A.ID_adicional, A.Numero_de_articulo, A.Descripcion, A.Grupo, A.Existencias, A.Linea,A.Subgrupo, A.Tiempo_Reposicion
HAVING (A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0)) > 0
Order By A.Numero_de_articulo
pero igual me sigue sacando el mismo error, ya no se que hacer
Muy buenas
Yo he probado tu consulta con la variable '[%0]' y poniendo en la cabecera de la query
declare @Fecha date
set @Fecha = select max X0.docdate from oinv X0 where X0.docdate = '[%0]'
y me ha funcionado en SAP 9.0 PL12. Eso si, he tenido que quitar un campo que tenías creado personalizado.
Si puedo, te envío la que he modificado, pero no sé si hoy me va a dar tiempo.
Saludos
Dios, fallo mío....
declare @Grupo nvarchar(25) ' Por ejemplo, si alguno de tus nombres de marcas es mayor, aumenta este número
set @Grupo = select distinct X0.[itmsgrpnam] from OITB X0 where X0.[itmsgrpnam] = '[%0]'
Y en WHERE T2.[ItmsGrpNam] = '[%0]' ' Comiilas simples antes y después de los corchetes.
Espero que esta vez si funcione.
Saludos
Pablo, no funciona me vota este error:
1). [Microsoft][SQL Server Native Client 10.0][SQL Server]Must specify table to select from. 2). [Microsoft][SQL Server Native Client 10.0][SQL Server]Statement 'Alertas recibidas' (OAIB) (s) could not be prepared.
Mira como lo puse:
declare @Grupo nvarchar(35)
set @Grupo = select distinct X0.[ItmsGrpNam] from OITB X0 where X0.[itmsgrpnam] = '[%0]'
Select A.ID_adicional, A.Numero_de_articulo, A.Descripcion, A.Grupo, A.Linea, A.Subgrupo,
CASE WHEN
((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias > 0
THEN ((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias
WHEN ((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias < 0
THEN (((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias) *0 END AS Sugerido,
(A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion AS Proyeccion,
A.Existencias,
A.Existencias /((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))) as Capacidad_Inventario,
(A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0)) as Consumo_Total,
(A.Ventas - ISNULL(B.Devoluciones,0)) AS Ventas_ultimo_3_meses,
A.Tiempo_Reposicion
FROM
(
SELECT DISTINCT T1.[SWW] as ID_adicional, T0.[ItemCode] AS Numero_de_articulo, T0.[Dscription] as Descripcion, T2.[ItmsGrpNam] as Grupo, T3.[FirmName] as Linea, T1.[U_Subgrupo] as Subgrupo,
T1.[OnHand] AS Existencias,
SUM(T0.[Quantity]) AS Ventas,
T1.[LeadTime]/30 as Tiempo_Reposicion
FROM [dbo].[INV1] T0
INNER JOIN [dbo].[OITM] T1 ON T0.ItemCode = T1.ItemCode
INNER JOIN [dbo].[OITB] T2 ON T1.ItmsGrpCod = T2.ItmsGrpCod
INNER JOIN [dbo].[OMRC] T3 ON T1.FirmCode = T3.FirmCode
WHERE T2.[ItmsGrpNam]='[%0]' and T0.[DocDate] >= (getdate()-90) and T0.[DocDate] <= getdate()
GROUP BY T0.[ItemCode], T2.[ItmsGrpNam], T1.[OnHand], T1.[LeadTime], T0.[Dscription],T1.[SWW],T3.[FirmName], T1.[U_Subgrupo]
)A
left Join
(
SELECT T0.[ItemCode] AS ITEM_DEVUELTO, SUM(T0.[Quantity]) AS Devoluciones
FROM [dbo].[RIN1 T0]
WHERE T0.[DocDate] >=(getdate()-90) AND T0.[DocDate] <= getdate()
GROUP BY T0.[ItemCode]
)B
On A.Numero_de_articulo=B.ITEM_DEVUELTO
GROUP BY A.Ventas, B.Devoluciones, A.ID_adicional, A.Numero_de_articulo, A.Descripcion, A.Grupo, A.Existencias, A.Linea,A.Subgrupo, A.Tiempo_Reposicion
HAVING (A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0)) > 0
Order By A.Numero_de_articulo
Que pena molestar tanto con este tema, pero es que realmente lo necesito
No ni asi funciona me sigue saliendo el mismo error de 1). [Microsoft][SQL Server Native Client 10.0][SQL Server]Must specify table to select from. 2). [Microsoft][SQL Server Native Client 10.0][SQL Server]Statement 'Alertas recibidas' (OAIB) (s) could not be prepared.
es algo de especificar la tabla del from pero no ya lo he modificado por todos lados y nada.
lo hice como tu dijiste asi :
declare @Grupo nvarchar(35)
set @Grupo = select distinct X0.[ItmsGrpNam] from OITB X0 where X0.[itmsgrpnam] = '[%0]'
Select A.ID_adicional, A.Numero_de_articulo, A.Descripcion, A.Grupo, A.Linea, A.Subgrupo,
CASE WHEN
((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias > 0
THEN ((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias
WHEN ((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias < 0
THEN (((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias) *0 END AS Sugerido,
(A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion AS Proyeccion,
A.Existencias,
A.Existencias /((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))) as Capacidad_Inventario,
(A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0)) as Consumo_Total,
(A.Ventas - ISNULL(B.Devoluciones,0)) AS Ventas_ultimo_3_meses,
A.Tiempo_Reposicion
FROM
(
SELECT DISTINCT T1.[SWW] as ID_adicional, T0.[ItemCode] AS Numero_de_articulo, T0.[Dscription] as Descripcion, T2.[ItmsGrpNam] as Grupo, T3.[FirmName] as Linea, T1.[U_Subgrupo] as Subgrupo,
T1.[OnHand] AS Existencias,
SUM(T0.[Quantity]) AS Ventas,
T1.[LeadTime]/30 as Tiempo_Reposicion
FROM [dbo].[INV1] T0
INNER JOIN [dbo].[OITM] T1 ON T0.ItemCode = T1.ItemCode
INNER JOIN [dbo].[OITB] T2 ON T1.ItmsGrpCod = T2.ItmsGrpCod
INNER JOIN [dbo].[OMRC] T3 ON T1.FirmCode = T3.FirmCode
WHERE T2.[ItmsGrpNam]=@Grupo and T0.[DocDate] >= (getdate()-90) and T0.[DocDate] <= getdate()
GROUP BY T0.[ItemCode], T2.[ItmsGrpNam], T1.[OnHand], T1.[LeadTime], T0.[Dscription],T1.[SWW],T3.[FirmName], T1.[U_Subgrupo]
)A
left Join
(
SELECT T0.[ItemCode] AS ITEM_DEVUELTO, SUM(T0.[Quantity]) AS Devoluciones
FROM [dbo].[RIN1 T0]
WHERE T0.[DocDate] >=(getdate()-90) AND T0.[DocDate] <= getdate()
GROUP BY T0.[ItemCode]
)B
On A.Numero_de_articulo=B.ITEM_DEVUELTO
GROUP BY A.Ventas, B.Devoluciones, A.ID_adicional, A.Numero_de_articulo, A.Descripcion, A.Grupo, A.Existencias, A.Linea,A.Subgrupo, A.Tiempo_Reposicion
HAVING (A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0)) > 0
Order By A.Numero_de_articulo
pero nada
Si Ivan es que el problema solo es la variable si lo quito y dejo el where solo para la fecha el codigo funciona o si lo dejo sin variable tambien funciona por ejemplo si lo dejo T2.Itmsgrpnam = 'GAP' hay funciona pero si lo pongo T2.Itmsgrpnam = [%0] me saca el error de from ya le quite el itmsgrpnam en el where y lo deje asi:
WHERE T0.[DocDate] >= (getdate()-90) and T0.[DocDate] <= getdate() y funciono o si lo pongo asi:
WHERE T2.[ItmsGrpNam]='GAP' and T0.[DocDate] >= (getdate()-90) and T0.[DocDate] tambien funciona pero si solo agrego [%0] hay es donde saca el error, por eso es que se me hace tan raro
Dada la urgencia y la importancia que manifiestas, mientras se busca la solución para el informe por medio del generador de consultas, te aconsejaría ya que en SQL funciona, pasa tu informe a Crystal Reports, lo diseñas y configuras a tu gusto, le creas parametros, luego lo pruebas y lo montas a SAP.
Slds,
IFV
Buenos días
Te comento lo que hago cuando una variable se me pone tonta.
En la query de SAP ponemos
/*SELECT * FROM OITB T0 WHERE T0.[ItmsGrpNam] ='[%0]'*/
DECLARE @Grupo nvarchar(20)
SET @Grupo = '[%0]'
EXEC Prueba @grupo
Y después creamos el procedimiento almacenado Prueba en SQL
USE [XXXXXXXXX] /*Nombre de tu base de datos*/
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Prueba] (@grupo nvarchar(20))
AS
BEGIN
Select A.ID_adicional, A.Numero_de_articulo, A.Descripcion, A.Grupo, A.Linea,
CASE WHEN
((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias > 0
THEN ((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias
WHEN ((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias < 0
THEN (((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias) *0 END AS Sugerido,
(A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion AS Proyeccion,
A.Existencias,
A.Existencias /((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))) as Capacidad_Inventario,
(A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0)) as Consumo_Total,
(A.Ventas - ISNULL(B.Devoluciones,0)) AS Ventas_ultimo_3_meses,
A.Tiempo_Reposicion
FROM
(
SELECT DISTINCT T1.[SWW] as ID_adicional, T0.[ItemCode] AS Numero_de_articulo, T0.[Dscription] as Descripcion, T2.[ItmsGrpNam] as Grupo, T3.[FirmName] as Linea,
T1.[OnHand] AS Existencias,
SUM(T0.[Quantity]) AS Ventas,
T1.[LeadTime]/30 as Tiempo_Reposicion
FROM [dbo].[INV1] T0
INNER JOIN OITM T1 ON T0.ItemCode = T1.ItemCode
INNER JOIN OITB T2 ON T1.ItmsGrpCod = T2.ItmsGrpCod
INNER JOIN OMRC T3 ON T1.FirmCode = T3.FirmCode
WHERE T2.[ItmsGrpNam]= @grupo and T0.[DocDate] >= (getdate()-90) and T0.[DocDate] <= getdate()
GROUP BY T0.[ItemCode], T2.[ItmsGrpNam], T1.[OnHand], T1.[LeadTime], T0.[Dscription],T1.[SWW],T3.[FirmName]
)A
left Join
(
SELECT T0.[ItemCode] AS ITEM_DEVUELTO, SUM(T0.[Quantity]) AS Devoluciones
FROM RIN1 T0
WHERE T0.[DocDate] >=(getdate()-90) AND T0.[DocDate] <= getdate()
GROUP BY T0.[ItemCode]
)B
On A.Numero_de_articulo=B.ITEM_DEVUELTO
GROUP BY A.Ventas, B.Devoluciones, A.ID_adicional, A.Numero_de_articulo, A.Descripcion, A.Grupo, A.Existencias, A.Linea, A.Tiempo_Reposicion
HAVING (A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0)) > 0
Order By A.Linea
END
Recuerda que he quitado un campo tuyo en la consulta ya que yo no lo tengo en la mía.
A ver si ahora hay suerte. Yo lo he probado tal cual lo pongo aquí y me funciona.
Saludos
Buenas tardes
Prueba tu query así
declare @it nvarchar(20)
set @it = (/*select T0.ItmsGrpNam from [dbo].[OITB] T0 where T0.ItmsGrpNam = */'[%0]')
Select A.ID_adicional, A.Numero_de_articulo, A.Descripcion, A.Grupo, A.Linea, A.Subgrupo,
CASE WHEN
((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias > 0
THEN ((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias
WHEN ((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias < 0
THEN (((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion)-A.Existencias) *0 END AS Sugerido,
(A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))* A.Tiempo_Reposicion AS Proyeccion,
A.Existencias,
A.Existencias /((A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0))) as Capacidad_Inventario,
(A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0)) as Consumo_Total,
(A.Ventas - ISNULL(B.Devoluciones,0)) AS Ventas_ultimo_3_meses,
A.Tiempo_Reposicion
FROM
(
SELECT DISTINCT T1.[SWW] as ID_adicional, T0.[ItemCode] AS Numero_de_articulo, T0.[Dscription] as Descripcion, T2.[ItmsGrpNam] as Grupo, T3.[FirmName] as Linea, T1.[U_Subgrupo] as Subgrupo,
T1.[OnHand] AS Existencias,
SUM(T0.[Quantity]) AS Ventas,
T1.[LeadTime]/30 as Tiempo_Reposicion
FROM [dbo].[INV1] T0
INNER JOIN OITM T1 ON T0.ItemCode = T1.ItemCode
INNER JOIN OITB T2 ON T1.ItmsGrpCod = T2.ItmsGrpCod
INNER JOIN OMRC T3 ON T1.FirmCode = T3.FirmCode
WHERE T2.[ItmsGrpNam]=@it and T0.[DocDate] >= (getdate()-90) and T0.[DocDate] <= getdate()
GROUP BY T0.[ItemCode], T2.[ItmsGrpNam], T1.[OnHand], T1.[LeadTime], T0.[Dscription],T1.[SWW],T3.[FirmName], T1.[U_Subgrupo]
)A
left Join
(
SELECT T0.[ItemCode] AS ITEM_DEVUELTO, SUM(T0.[Quantity]) AS Devoluciones
FROM RIN1 T0
WHERE T0.[DocDate] >=(getdate()-90) AND T0.[DocDate] <= getdate()
GROUP BY T0.[ItemCode]
)B
On A.Numero_de_articulo=B.ITEM_DEVUELTO
GROUP BY A.Ventas, B.Devoluciones, A.ID_adicional, A.Numero_de_articulo, A.Descripcion, A.Grupo, A.Existencias, A.Linea,A.Subgrupo, A.Tiempo_Reposicion
HAVING (A.Ventas - ISNULL(B.Devoluciones,0))/(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0)) > 0
Order By A.Linea, A.Subgrupo
Saludos
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Muy buenas Lender
¿Has solucionado algo de la consulta?.
Saludos
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hola pablo, si logre resolverlos como me dijiste la primera vez asi:
declare @Grupo nvarchar(25)
set @Grupo = select distinct X0.[itmsgrpnam] from OITB X0 where X0.[itmsgrpnam] = '[%0]'
Y en WHERE T2.[ItmsGrpNam] = '@Grupo'
lo que pasa es que asi me generaba error lo unico que hize para que funcionara fue ponerle en el set [dbo].[OITB] y listo con eso quedo, aunque hay una cosa que me quedo dando vueltas en la cabeza cuando no uso variables en una consulta simpre que me muestra el resultado me lo muestra asi:
Si te fijas por ejemplo en esa consulta el numero del articulo viene acompañado de una flecha amarilla lo que me deja hacer esta flecha es seleccionar dicho articulo y me lleva a los datos maestros, en cambio cuando uso las variable me lo muestra de la siguiente manera:
vez que no me sale la flecha amarilla ?
De casualidad sabes como soluciono ste inconveniente?.
Mil gracias por la atencion
Buenas!
Si no lo vas a hacer por un procedimiento almacenado, puedes poner al comienzo de la query:
declare @Fecha date
set @Fecha = select max X0.docdate from oinv X0 where X0.docdate = '[%0]'
Saludos
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Podrías intentar hacerlo de una manera sencilla, para ver si el error es por eso.
Declare @Marca varchar(30)
Set @Marca = [%0]
y luego reemplazas WHERE T2.[ItmsGrpNam]=@Marca.
No entiendo este fragmento del codigo.
(ROUND((DATEDIFF(dd, (getdate()-90), getdate())/30.0),0)
getdate() - 90 = Fecha actual - 90 dias
datediff(dd,getdate()-90,getdate()) = 90 siempre porque son los dias que le restaste a la fecha de hoy.
Resultado anterior / 30 = 3 Siempre.
Si estoy entendiendo mal, este codigo podrias reemplazarlo siempre por un 3.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.