Estimados,
El tema es más simple de lo que intentan hacer.
Les adjunto la solución
Att,
Manuel Lazcano
Exxis
Chile
Query informe de stock por fecha
SELECT T0.DocDate AS 'Fecha de contabilización',
T0.ItemCode AS 'Número de artículo',
t1.itemname AS 'Descripción',T2.ItmsGrpnam as 'Grupo',
'Bod' AS 'Código de almacén',
case when T0.InQty=0 or T0.InQty is null then T0.OutQty*-1 else T0.InQty end AS 'Cantidad TRX',
T0.CalcPrice AS 'Costo',
case when ((case when T0.InQty=0 or T0.InQty is null then T0.OutQty*-1 else T0.InQty end) * T0.CalcPrice) <> 0 then ((case when T0.InQty=0 or T0.InQty is null then T0.OutQty*-1 else T0.InQty end) * T0.CalcPrice) else T0.transvalue END AS 'Monto', T3.segment_0 AS 'Cuenta Stock', T3.AcctName AS 'Nombre Cuenta' INTO #TEMP
FROM dbo.OINM T0 INNER JOIN dbo.OITM T1 ON T0.ItemCode = T1.ItemCode INNER JOIN dbo.OITB T2 ON T1.ItmsGrpCod = T2.ItmsGrpCod INNER JOIN dbo.OACT T3 ON T2.BalInvntAc = T3.AcctCode
WHERE T0.DocDate<='[%0]'
SELECT
, Descripción, Grupo, [Código de almacén], SUM( ) AS Cantidad, SUM(Monto) AS Valor,CAST ( CASE WHEN SUM(Monto)=0 OR SUM( )=0 THEN 0 else SUM(Monto)/SUM( ) END AS NVARCHAR) AS 'Costo Prom', , , ,
FROM #TEMP
GROUP BY , Descripción, , , ,grupo