October 29, 2010 at 9:24 am
What I'm after is to be able to show distinct of entries in the first colomn and in the second colomn a count of how many times the first column appears in the table?
as an example:
field1
jim
john
roger
jim
jim
to show as
field1 field2
jim 3
john 1
roger 1
October 29, 2010 at 9:48 am
SELECT Field1, Field2 = count(*)
FROM YourTable
GROUP BY Field1
ORDER BY Field1
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
October 29, 2010 at 9:53 am
You're looking for the COUNT function (http://msdn.microsoft.com/en-us/library/aa258232(v=SQL.80).aspx).
An example:
declare @Counts table (field1 varchar(10), field2 char(1))
insert into @Counts
select 'jim', 'a' union all
select 'jim', 'b' union all
select 'jim', 'c' union all
select 'john', 'a' union all
select 'roger', 'a'
select
field1,
count(*) as field2
from @Counts
group by field1
order by count(*) desc
- Jeff
October 29, 2010 at 9:56 am
That's great, works a treat,thanks Wayne and Jeff
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply