on 06-24-2015 10:46 PM
Hola Expertos,
Necesito generar un reporte que me diga la rotación de stock de artículos. Yo consulto el reporte auditoría de stocks, más no me da lo que necesito.
Serían tan amables de informarme cómo generar un query que me ayude a saber el movimiento (salidas/ventas) de un SKU, para poder compararlo contra los días de almacenamiento, y así poder informar, el costo de mantenimiento en almacén contra lo que se está vendiendo, por ejemplo.
Agradezco de corazón cualquier tipo de información.
Saludos,
Kenneth Méndez T.
Hola Kenneth
Yo hice este, puede servirte de guía
--SET Arithabort OFF
--SET ANSI_WARNINGS OFF
/* SELECT FROM [dbo].[OINM] T1 */
DECLARE @dateInicio DATETIME
/* WHERE */
SET @dateInicio = '20150101'--/* T1.DocDate */ '[%0]'
/* SELECT FROM [dbo].[OINM] T1 */
DECLARE @dateFinal DATETIME
/* WHERE */
SET @dateFinal = '20150301'--/* T1.DocDate */ '[%1]'
/* SELECT FROM [dbo].[OITB] T2 */
DECLARE @grupodesde AS NVARCHAR(30)
/* WHERE */
SET @grupodesde = ''--/* T1.ItmsGrpNam */ '[%2]'
/* SELECT FROM [dbo].[OITB] T2 */
DECLARE @grupohasta AS NVARCHAR(30)
/* WHERE */
SET @grupohasta = ''--/* T1.ItmsGrpNam */ '[%3]'
/* SELECT FROM [dbo].[OITM] T0 */
DECLARE @productodesde AS NVARCHAR(30)
/* WHERE */
SET @productodesde = ''--/* T0.ItemCode */ '[%4]'
/* SELECT FROM [dbo].[OITM] T0 */
DECLARE @productohasta AS NVARCHAR(30)
/* WHERE */ SET @productohasta = ''--/* T0.ItemCode */ '[%5]'
select t0.articulo [Artículo], t0.ItemName [Descripción], t3.Consumo*t1.Price 'Consumo',
((t3.saldoinic-t3.salidas)* t1.Price) 'Stock Inicial $',
cast(ISNULL(X2.Compras,0)as decimal(16,2))'Compras',
cast(ISNULL(X3.Ventas,0) as decimal(16,2)) 'Ventas',
(t1.SaldoFin * t1.Price) 'Stock Final $',
cast(ISNULL(X3.CostoVenta,0) as decimal (16,2)) 'Costo Venta' ,
cast(ISNULL((X3.Ventas-X3.CostoVenta),0)as decimal (16,2))'Ganancia',
convert(decimal (16,2),case isNull(X3.Ventas,0)
when 0 then 0 else ((IsNull(X3.Ventas,0)-IsNull(X3.CostoVenta,0))/IsNull(X3.Ventas,0) *100) end)'%',
round(convert(decimal (16,2),case ((IsNull(t3.saldoInic,0) +IsNull(t3.saldoInic,0) +IsNull(t1.SaldoFin,0)) /2)
when 0 then 0 else (IsNull(t3.Consumo,0)) / nullif(((IsNull(t3.saldoInic,0) +
IsNull(t3.saldoInic,0) +IsNull(t1.SaldoFin,0)) /2),0) end),-1)'Rotación Veces',
--(365)/
case when
(
round(convert(decimal (16,2),case ((IsNull(t3.saldoInic,0) +IsNull(t3.saldoInic,0) +IsNull(t1.SaldoFin,0)) /2)
when 0 then 0 else (IsNull(t3.Consumo,0)) /
((IsNull(t3.saldoInic,0) +IsNull(t3.saldoInic,0) + IsNull(t1.SaldoFin,0)) /2) end),-1)) = 0 then 0
else
(365)/
(round(convert(decimal (16,2),case ((IsNull(t3.saldoInic,0) +IsNull(t3.saldoInic,0) +IsNull(t1.SaldoFin,0)) /2)
when 0 then 0 else (IsNull(t3.Consumo,0))/
((IsNull(t3.saldoInic,0) +IsNull(t3.saldoInic,0) + IsNull(t1.SaldoFin,0)) /2) end),-1)) end
'Rotación Días',
(CASE WHEN EntMas = 0 THEN (EntMas*t1.Price)
WHEN EntMas > 0 AND EntMas < = SaltoT THEN 0
WHEN SaltoT > 0 AND EntMas > = SaltoT THEN (EntMas - SaltoT)*T1.Price
WHEN SaltoT = 0 AND EntMas > = SaltoT THEN (EntMas * T1.Price)
END) 'Existencia +2 años'
from
(
SELECT a1.itemcode articulo, a1.ItemName
FROM OITM a1
INNER JOIN OITB T1 ON a1.ItmsGrpCod = T1.ItmsGrpCod
WHERE
(
(IsNull(@grupodesde ,'') = '' and isNull(@grupohasta ,'') = '') Or
(
(IsNull(@grupodesde ,'') = '' Or T1.ItmsGrpNam >= @grupodesde ) AND
(isNull(@grupohasta ,'') = '' Or T1.ItmsGrpNam <= @grupohasta )
)
)
AND
(
(IsNull(@productodesde ,'') = '' and isNull(@productohasta ,'') = '') Or
(
(IsNull(@productodesde ,'') = '' Or a1.ItemCode >= @productodesde) AND
(isNull(@productohasta ,'') = '' Or a1.ItemCode <= @productohasta)
))
GROUP BY a1.itemcode,a1.ItemName) AS t0
left outer JOIN
(SELECT a.itemcode, a.itemname,
(select ISNULL(sum(InQty),0) from oinm s
where s.itemcode = a.itemcode and datediff(day,Docdate, @dateInicio) >= 730) EntMas,
(Select ISNULL(sum(OutQty),0) from OINM s where s.itemcode = a.itemcode) SaltoT,
(select avg(s.price) from oinm s
where s.itemcode = a.itemcode and s.price <> 0 and transtype in(-2,14,16,18,20,58,59,67)) PriceInQty,
(select max(price) from oinm s where s.itemcode = a.itemcode and transtype in(13,15,21,60,67)) PriceOutQty,
isnull(sum(coalesce(s.Inqty,0)),0)saldoInic,
isnull(sum(coalesce(s.Outqty,0)),0) Salidas ,
(select ISNULL(sum(coalesce(x.OutQty,0)),0) from OINM x where x.DocDate between @dateInicio and @dateFinal and x.ItemCode = s.ItemCode) [Consumo]
FROM OINM s inner JOIN OITM a ON a.itemcode = s.itemcode
where s.DocDate < @dateInicio
GROUP BY a.itemcode, a.itemname, a.OnHand, s.ItemCode)
t3 ON t3.itemcode=t0.articulo
left outer join
(SELECT a.itemcode, a.itemname, a.OnHand SaldoFin, b.price
FROM OINM s inner JOIN OITM a ON a.itemcode = s.itemcode
inner join itm1 b on a.itemcode = b.itemcode
where b.pricelist = 2
GROUP BY a.itemcode, a.itemname, a.OnHand, b.Price)
t1 ON t1.itemcode=t0.articulo
left outer join
(
select T1.ItemCode,
Sum(T1.LineTotal)[Compras]
from OPCH T0 INNER JOIN PCH1 T1 on T0.DocEntry = T1.DocEntry
AND T1.ItemCode IS NOT NULL
AND T0.DocDate BETWEEN @dateInicio and @dateFinal
group by t1.ItemCode
)X2 ON X2.ItemCode=T0.articulo
LEFT OUTER join
(
select Z0.ItemCode,SUM(Z0.Ventas)[Ventas],SUM(Z0.CostoVenta*Z0.Cantidad)[CostoVenta]
From
(
select T1.ItemCode,
(T1.Quantity - isnull(t2.quantity,0)) Cantidad,
(T1.LineTotal - IsNull(T2.LineTotal,0))[Ventas] ,
(T1.GrossBuyPR - IsNull(T2.GrossBuyPR,0))[CostoVenta]
from
OINV T0 INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry AND (T0.DocDate BETWEEN @dateInicio AND @dateFinal)
LEFT OUTER JOIN (select BaseEntry,BaseLine,LineTotal,GrossBuyPr,Quantity from RIN1 )T2 ON T2.BaseEntry = T1.DocEntry And T2.BaseLine = T1.LineNum
UNION ALL
select T1.ItemCode,(T1.LineTotal*-1),(T1.GrossBuyPr*-1),T1.Quantity
from
ORIN T0 INNER JOIN RIN1 T1 ON T0.DocEntry = T1.DocEntry AND (T0.DocDate Between @dateInicio AND @dateFinal)AND T0.Canceled = 'N'
AND T1.basetype = -1
)Z0
GROUP BY ItemCode
)X3 ON X3.ItemCode = T0.articulo
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Buen día
Voy a partir de la base de que tiene conocimiento sobre consultas en sql.
La vista OINM contiene toda la información de entradas y salidas de todos los productos y de todas las bodegas de la empresa.
Campo ItemCode --> Código del producto
Campo InQty --> Cantidad de entrada por la transacción
Campo OutQty --> Cantidad de salida por la transacción
Campo DocDate --> Fecha de la transacción
Campo Warehouse --> Código de la bodega de la transacción
Saludos
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
91 | |
11 | |
10 | |
6 | |
5 | |
5 | |
5 | |
3 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.