user3671271
user3671271

Reputation: 568

How to count distinct of record in sql server query?

I'm beginner in sql server and query,i want write query to show me this result:

record A count=3
record B count=100

for that purpose i write this query:

select distinct *from EWSD1


but that query show me this result:

Record A
Record B


but i want show for example record A all field detail and how many repeat in all of the table?thanks.

Upvotes: 0

Views: 86

Answers (2)

JamieD77
JamieD77

Reputation: 13949

SELECT  [name], -- select the fields you want to see
        [address],
        [postnumber],
        COUNT(*) -- include aggregate
FROM    TABLE_NAME
GROUP BY [name], -- group by fields that aren't inlcuded in aggregate.. 
        [address],
        [postnumber]

Upvotes: 4

Jorel Amthor
Jorel Amthor

Reputation: 1294

COUNT(*) FROM table_name

This will give you the length of the table, a.k.a. how many entry you got in this table

COUNT(*) FROM table_name WHERE column_name = 3

This will give you the number of entries the table "table_name" got where the value of "column_name" is 3. Use WHERE username LIKE "John" if you're comparing strings. Simply replace the column_name and table_name by appropriates column name and table name

Upvotes: 0

Related Questions