Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Distinct Count Query

Status
Not open for further replies.

kloner

Programmer
May 15, 2000
79
AU
Hi guys,

I would appreciate any helpon this one as I am pulling out my hair trying to get it to work.

What I have got so far is a recordset with all the correct distict data. The query is below.

What I am after is to also return the count of each distinct record.

SQL Query:

Code:
SELECT 
	DISTINCT [FunctionName], 
	[ModuleName], 
	[ErrorCode], 
	[ErrorDescription]
FROM 
	[dbo].[Tbl_Error]


Returned Data:
Code:
ClArticle      CheckAdd     -214   [DBNETLIB]
ClArticle      LoadInfo     -214   Object req
ClArticle      VersionLoad  -214   [DBNETLIB04]
ClArticleMain  Load         -29    [DBNETLIB999]
CLMatt	       jkj          123    ffff
ClMatthew      GetOutOfHere 123    AAA You m
ClMatthew      GetOutOfHere 135    You must
ClUser         IsActive     430    Report:=
ClUser	       LogIn        440    Username


As you can see each row is UNIQUE. I am trying to add a counter column onto the end of each row to show how many times that error occured.

So expected output would be:

Code:
ClArticle      CheckAdd     -214   [DBNETLIB]     12
ClArticle      LoadInfo     -214   Object req     58
ClArticle      VersionLoad  -214   [DBNETLIB04]   95
ClArticleMain  Load         -29    [DBNETLIB999]  10
CLMatt	       jkj          123    ffff           6
ClMatthew      GetOutOfHere 123    AAA You m      45
ClMatthew      GetOutOfHere 135    You must       46
ClUser         IsActive     430    Report:=       55
ClUser	       LogIn        440    Username       19

I have tried doing all sorts of things (distinct counts and groupby?) without any luck.

Any help or tips would be great!

Thanks guys and girls.

Matt
 
Ah I finally worked it out! Thanks anyway guys! Happy I answered my own question ...


Code:
SELECT
	[FunctionName], 
	[ModuleName], 
	[ErrorCode], 
	[ErrorDescription], 
	COUNT(*) AS ErrorCount
FROM 
	[dbo].[Tbl_Error]
GROUP BY 
	[FunctionName], 
	[ModuleName], 
	[ErrorCode], 
	[ErrorDescription]
ORDER BY 
	[FunctionName], 
	[ModuleName], 
	[ErrorCode], 
	[ErrorDescription]

Matt

kloner
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top