Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Apples
Posting Yak Master
146 Posts |
Posted - 2009-11-02 : 11:00:05
|
| Here is my table schema with some data:------------------------------------------------------------tblErrors------------------------------------------------------------ErrorID | ErrorType | ErrorOccurred------------------------------------------------------------1 | System.NullReferenceException | default.aspx2 | System.NullReferenceException | default.aspx3 | System.NullReferenceException | test.aspx4 | System.ArgumentNullException | default.aspx5 | System.IndexOutOfRangeException | default.aspx6 | System.IndexOutOfRangeException | default.aspx7 | System.IndexOutOfRangeException | test.aspx8 | System.IndexOutOfRangeException | test.aspxI have a grid that displays the errors, but I want them to be grouped by ErrorType and ErrorOccurred, as well as showing the number of times an error like that occurred. It should look like this:--------------------------------------------------------Error Type | Error Occurred | Count--------------------------------------------------------System.NullReferenceException | default.aspx | 2System.NullReferenceException | test.aspx | 1System.ArgumentNullException | default.aspx | 1System.IndexOutOfRangeException | default.aspx | 2System.IndexOutOfRangeException | test.aspx | 2I am having trouble getting the count for each one though. Here is my current query:SELECT DISTINCT ErrorType, ErrorOccurredFROM tblErrorsHow can I get the count? |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-11-02 : 11:07:23
|
| Don't use DISTINCT, use GROUP BY and include the aggregate.select errorType, ErrorOccured, count(*) as [count] from tblErrors group by errorType, ErrorOccuredBe One with the OptimizerTG |
 |
|
|
Apples
Posting Yak Master
146 Posts |
Posted - 2009-11-02 : 11:29:52
|
| That worked great, thank you! |
 |
|
|
Apples
Posting Yak Master
146 Posts |
Posted - 2009-11-02 : 12:01:32
|
| One more question...Say that I add a Timestamp column to tblErrors.How could I modify my query to get the most recent timestamp for each item in my grid? |
 |
|
|
|
|
|