Count separate records with filtering condition in another field

Asked

Viewed 203 times

0

Staff need to perform a count of how many distinct records exist in a field, but my filtering condition is based on another field.

Example of how the data are organized:

PDV |   DESC
----------------
410 | "Automa"
410 | "Automa"
410 | "Automa"
150 | "Automa"
150 | "Automa"
150 | "Automa"
831 | "Automa"
831 | "Automa"

Query I made:

SELECT COUNT(PDV) FROM Table WHERE DESC ='Automa'

What I need to count is how many different Pdvs exist with the same description.

The result I got was 8 what I hope to receive is 3 since I have 3 distinct Pdvs

  • What is your DBMS?

  • MSSQL. It worked! thank you very much!

  • If the answer has met you do not forget to accept it as correct to help people with similar doubts

1 answer

0


Use a DISTINCT allied with the COUNT:

SELECT COUNT(DISTINCT PDV)
  FROM table
 WHERE desc = 'Automa'

COUNT

This function returns the number of items found in a group.


DISTINCT

The DISTINCT keyword eliminates duplicate rows of the results of a SELECT statement.

Browser other questions tagged

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