How can I count the number of Records in Mysql?

Asked

Viewed 18 times

-1

I have this Mysql code:

SELECT
    COUNT( PEDIDO.COD_PEDIDO ) 
FROM
    PEDIDO
    JOIN PEDIDO P2 ON P2.COD_CLIENTE = PEDIDO.COD_CLIENTE 
    AND DATE( PEDIDO.DT_PEDIDO ) = DATE( P2.DT_PEDIDO ) 
    AND PEDIDO.COD_PEDIDO <> P2.COD_PEDIDO
    LEFT OUTER JOIN CLIENTE ON PEDIDO.COD_CLIENTE = CLIENTE.COD_CLIENTE
    LEFT OUTER JOIN ESTADO ON CLIENTE.COD_ESTADO = ESTADO.COD_ESTADO
    LEFT OUTER JOIN CIDADE ON CLIENTE.COD_CIDADE = CIDADE.COD_CIDADE
    LEFT OUTER JOIN NOTA_FISCAL ON PEDIDO.COD_PEDIDO = NOTA_FISCAL.COD_PEDIDO 
    AND NOTA_FISCAL.FG_EXCLUIDO = 'N'
    LEFT OUTER JOIN OP_PEDIDO ON PEDIDO.COD_PEDIDO = OP_PEDIDO.COD_PEDIDO 
    AND OP_PEDIDO.NR_ORDEM > 0
    LEFT OUTER JOIN TRANSPORTADOR ON TRANSPORTADOR.COD_TRANSPORTADOR = OP_PEDIDO.COD_TRANSPORTADORA
    LEFT OUTER JOIN PEDIDO_DEGUSTACAO ON PEDIDO_DEGUSTACAO.COD_PEDIDO = PEDIDO.COD_PEDIDO 
WHERE
    PEDIDO.COD_PEDIDO > 0 
    AND PEDIDO.DT_PEDIDO >= '2021-03-08' 
    AND PEDIDO.DT_PEDIDO <= '2021-03-09' 
GROUP BY
    PEDIDO.COD_PEDIDO

The result of this select is :

COUNT(PEDIDO.COD_PEDIDO)
1
1
1
1
1
3
3
3
3
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

I wanted to know how I can count the number of Records using this code, for example, is displayed 24 lines in this select, wanted to be able to display this number, 24.

1 answer

1


Just remove the grouping of the query, ie, take that chunk out of the code.

GROUP BY
    PEDIDO.COD_PEDIDO

I suggest you take a look at how the GROUP BY.


According to comment here in the reply, you need to then use the distinct in the query, so each occurrence of the code will only be accounted for once:

SELECT COUNT(DISTINCT(PEDIDO.COD_PEDIDO))
  • If you notice, in the records, there are some that flag "3" I need to signal the number of lines, not the sum, which is what happens when removing the GROUP BY

  • @Miguelkrug, see if he answers

Browser other questions tagged

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