Subquery (SQL error (1242): Subquery Returns more than 1 Row)

Asked

Viewed 712 times

-2

I created the following query:

SELECT
    a.`CPF/CNPJ`,
    a.Cliente,
    a.Cep,
    a.Cidade,
    a.Estado,
    COUNT(a.`CPF/CNPJ`) AS Novas_Instancias,
    (
      SELECT COUNT(c.`CPF/CNPJ`)
      FROM relat_instancias c 
      GROUP BY c.`CPF/CNPJ`
    ) AS Total_Instancias
FROM relat_instancias a
LEFT JOIN relat_form_pag_cartao b
    ON a.Pedido = b.Pedido
WHERE a.Data_abertura > '2019-03-01'
    AND b.lista_casamento = 'Nao'
    AND a.Analisado IS NULL
GROUP BY a.`CPF/CNPJ`
ORDER BY Novas_Instancias DESC

That me returns:

SQL Error (1242): Subquery Returns more than 1 Row

The initial idea was to consult how many new CPF/CNPJ logs I have and this information come in a column and in another column the total with the old and new logs.

Example:

CPF XXXXXXXXXXX from January to August had 10 logs, but now it’s September and I have a new 1 that was inserted in the table.

The expected return is the next:

CPF, Date, Zip code, City, State, New log, Total log

Is there any possibility of correcting this?

  • 1

    the sub-consultation (SELECT COUNT(c.'CPF/CNPJ') FROM relat_instancias c GROUP BY c.'CPF/CNPJ') will group by Cpf, so can bring more than one return.. would have to add one where or top 1 or something like..

2 answers

1

Shouldn’t it be something like this? I am without sql to test, but I believe to be something along these lines...

SELECT
    a.`CPF/CNPJ`,
    a.Cliente,
    a.Cep,
    a.Cidade,
    a.Estado,
    SUBQUERY.Total_Instancias 
FROM relat_instancias a
INNER JOIN (
      SELECT COUNT(*) Total_Instancias ,c.`CPF/CNPJ`
      FROM relat_instancias c 
      GROUP BY c.`CPF/CNPJ`
    ) SUBQUERY ON A.CPF/CNPJ = SUBQUERY.CPF/CNPJ
LEFT JOIN relat_form_pag_cartao b
    ON a.Pedido = b.Pedido
WHERE a.Data_abertura > '2019-03-01'
    AND b.lista_casamento = 'Nao'
    AND a.Analisado IS NULL
GROUP BY a.`CPF/CNPJ`
ORDER BY Novas_Instancias DESC

1


An alternative solution would be to put a WHERE in place of GROUP BY subselect.

SELECT
    a.`CPF/CNPJ`,
    a.Cliente,
    a.Cep,
    a.Cidade,
    a.Estado,
    COUNT(a.`CPF/CNPJ`) AS Novas_Instancias,
    (
        SELECT COUNT(*)
        FROM relat_instancias c 
        WHERE c.`CPF/CNPJ` = a.`CPF/CNPJ`
    ) AS Total_Instancias
FROM relat_instancias a
LEFT JOIN relat_form_pag_cartao b
    ON a.Pedido = b.Pedido
WHERE
    a.Data_abertura > '2019-03-01'
    AND b.lista_casamento = 'Nao'
    AND a.Analisado IS NULL
GROUP BY a.`CPF/CNPJ`
ORDER BY Novas_Instancias DESC

As for the performance of this is another story.

  • Hello Diego, thank you so much for your help! The way you sent it worked perfectly. For further understanding, could you explain to me a little more what you did in this subquery? Just a small description even, I did not understand it very well (WHERE c.CPF/CNPJ = to.CPF/CNPJ). You are the guy!

  • 1

    As the friend @rLinhares spoke in the comment there of his question, a GROUP BY will return several results grouped by Cpf/cnpj, already the WHERE so that I suggested restricting the count only to Cpf/cnpj current of the main query and return only one result, gave to draw? rs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.