In a query
I am collecting data on representatives, customers and vendors.
Some representatives have salespeople, who in turn have customers. These cases are exceptional because the clients are linked directly to their representatives.
When attempting to use a subquery
with case
:
SELECT
vw.regional,
vw.cod_gerente,
vw.nome_gerente,
vw.cod_representante,
vw.nome_representante,
vw.cod_vendedor,
vw.nome_vendedor,
SUM(t.valor_original) AS total_valor_original
FROM vw_regional_gerente_representante_vendedor vw
INNER JOIN vwTitulosPagos t ON t.cod_representante = vw.cod_representante
WHERE 1 = 1
AND t.cod_cliente IN (
CASE WHEN vw.cod_representante IN (59,77,147) THEN
(SELECT DISTINCT cod_cliente FROM vendedores_x_clientes WHERE cod_vendedor = vw.cod_vendedor)
ELSE
t.cod_cliente
END
)
AND t.saldo = 0
AND t.data_movto BETWEEN CONVERT(DATETIME, '01/08/2016',103) AND CONVERT(DATETIME, '31/08/2016',103)
GROUP BY
vw.regional,
vw.cod_gerente,
vw.nome_gerente,
vw.cod_representante,
vw.nome_representante,
vw.cod_vendedor,
vw.nome_vendedor
ORDER BY vw.regional, vw.nome_gerente, vw.nome_representante, vw.nome_vendedor
The following error occurs:
SQL Error [512] [S0001]: Subquery returned more than 1 value. This is not allowed when the subquery follows =,! =, & lt ;, < =, & gt ;, > = or when the subquery is used as an expression.
com.microsoft.sqlserver.jdbc.SQLServerException: Subquery returned more than 1 value. This is not permitted when the subquery follows =,
In fact the subquery will return more than one result, but is expected to use within the IN
clause, however this only occurs within case
.
I'm trying this way and looking for solutions other than using UNION
.