Buenos días
estoy intentando añadir un campo nuevo en query que ya existia en la empresa, el que yo estoy creando es el de cantidad pero me esta generando el siguiente error, me ayudan porfa
Error: 1). [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. ‘Alertas recibidas’ (OAIB)
Query:
-- Informe para SBO de Indicadores de Inventario
declare @dataInicio datetime
declare @dataFinal datetime
declare @tempo int
SET @tempo=(SELECT TOP 1 T0.TRANSNUM FROM OINM T0 WHERE T0.[DocDate] >=[%0] AND T0.[DocDate] <=[%1] AND T0.[TransType] NOT IN ('67'))
SET @dataInicio=(SELECT '[%0]')
SET @dataFinal=(SELECT '[%1]')
SELECT
[Artículo]=t1.itemcode, [Descripción Artículo] = t1.itemname,
[Referencia]=t1.sww,
[Marca]=(SELECT Y1.[FirmName] FROM OITM Y0 INNER JOIN OMRC Y1 ON Y0.[FirmCode] = Y1.[FirmCode] WHERE Y0.[ItemCode] = t1.itemcode),
--[Grupo]=(SELECT Y1.[ItmsGrpNam] FROM OITM Y0 INNER JOIN OITB Y1 ON Y0.[ItmsGrpCod] = Y1.[ItmsGrpCod] WHERE Y0.[ItemCode] = t1.itemcode),
[Unidad de medida]= t1.[SalUnitMsr],
Consumo= ISNULL(t1.consumo,0),
[Consumo medio diario]=isnull(t1.consumo/datediff(day,@dataInicio,@dataFinal),0),
--[Consumo medio mensual]=isnull(t1.consumo/datediff(month,@dataInicio,@dataFinal),0),
[Stock inicial]=isnull(coalesce(t0.saldoinic,0),0),
[Stock final]=isnull(coalesce(t0.saldoinic,0)+coalesce(t1.saldofin,0)-COALESCE((SELECT Y0.[OnHand] FROM OITW Y0 WHERE Y0.[WhsCode] = '014' AND Y0.[ItemCode] = t1.itemcode),0),0),
--[Stock Actual]=(SELECT SUM(Y0.[OnHand]) FROM OITW Y0 WHERE Y0.[WhsCode] <> '014' AND Y0.[ItemCode] = t1.itemcode),
--[Stock promedio]= isnull((coalesce(t0.saldoinic,0)+(coalesce(t0.saldoinic,0)+t1.saldofin))/2,0),
--[Rotación stock]= case isnull((coalesce(t0.saldoinic,0)+(coalesce(t0.saldoinic,0)+t1.saldofin))/2,0) when 0 then 0 else isnull(t1.consumo / ((coalesce(t0.saldoinic,0)+(coalesce(t0.saldoinic,0)+t1.saldofin))/2) ,0) end,
--[Cobertura de stock]= case isnull(coalesce(t1.consumo/datediff(day,@dataInicio,@dataFinal),0),0) when 0 then 0 else isnull(((coalesce(t0.saldoinic,0)+(coalesce(t0.saldoinic,0)+t1.saldofin))/2)/ ( t1.consumo/datediff(day,@dataInicio,@dataFinal)),0) end,
[Stock comprometido]= (SELECT MAX(Y3.[IsCommited]) FROM OITW Y3 INNER JOIN OITM Y4 ON Y4.[ItemCode] = Y3.[ItemCode] WHERE Y4.[ItemCode] = t1.itemcode),
[Ordenes de Compra]= (SELECT MAX(Y3.[OnOrder]) FROM OITW Y3 INNER JOIN OITM Y4 ON Y4.[ItemCode] = Y3.[ItemCode] WHERE Y4.[ItemCode] = t1.itemcode),
[Último precio de compra]= T1.[LastPurPrc],
**[Cantidad]= (SELECT (T3.[Quantity]) FROM OPDN T0 INNER JOIN PDN1 T1 ON T0.[DocEntry] = T1.[DocEntry] INNER JOIN OITM T2 ON T1.[ItemCode] = T2.[ItemCode] INNER JOIN PRQ1 T3 ON T2.[ItemCode] = T3.[ItemCode] WHERE T2.[ItemCode] = t1.itemcode),**
[Última entrada]= (SELECT MAX(Y0.[DocDate]) FROM OPDN Y0 INNER JOIN PDN1 Y1 ON Y0.[DocEntry] = Y1.[DocEntry] INNER JOIN OITM Y2 ON Y1.[ItemCode] = Y2.[ItemCode] WHERE Y2.[ItemCode] = t1.itemcode)
FROM (
SELECT
a1.itemcode, a1.itemname, a1.sww, a1.SalUnitMsr,
saldoInic=isnull(sum(coalesce(s1.Inqty,0))-sum(coalesce(s1.outqty,0)),0),
a1.LastPurPrc
FROM OITM a1
JOIN OINM s1 on a1.itemcode=s1.itemcode
WHERE s1.docdate<@dataInicio AND s1.[TransType] NOT IN ('67') AND a1.frozenFor = 'N'
GROUP BY a1.itemcode, a1.itemname, a1.sww, a1.LastPurPrc, a1.SalUnitMsr
) as t0
RIGHT JOIN (
SELECT
a.itemcode, a.itemname, a.sww, a.SalUnitMsr,
entradas=isnull(sum(s.Inqty),0),
consumo=ISNULL((SELECT SUM(Y1.[Quantity]) FROM OINV Y0 INNER JOIN INV1 Y1 ON Y0.[DocEntry] = Y1.[DocEntry] WHERE Y0.[DocDate] BETWEEN @dataInicio AND @dataFinal AND Y1.[ItemCode] = a.[ItemCode]),0)-ISNULL((SELECT SUM(Y4.[Quantity]) FROM ORIN Y3 INNER JOIN RIN1 Y4 ON Y3.[DocEntry] = Y4.[DocEntry] WHERE Y3.[DocDate] BETWEEN @dataInicio AND @dataFinal AND Y4.[NoInvtryMv] = 'N' AND Y4.[ItemCode] = a.[ItemCode]),0),
saldoFin=isnull(sum(s.Inqty)-sum(s.outqty),0),
a.LastPurPrc
FROM OINM s
JOIN OITM a on a.itemcode=s.itemcode
WHERE s.docdate BETWEEN @dataInicio and @dataFinal AND s.[TransType] NOT IN ('67') AND a.frozenFor = 'N'
GROUP BY a.itemcode, a.itemname, a.sww, a.LastPurPrc, a.SalUnitMsr) t1 on t1.itemcode=t0.itemcode