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 |
|
chedderslam
Posting Yak Master
223 Posts |
Posted - 2009-04-01 : 15:08:52
|
| I am trying to get the distinct error messages and how many times each occurs. What I have below is not working, I only get 1 as the count.entry_data is a text field, thus the cast.Any help?SELECT DISTINCT CAST(entry_data AS varchar(MAX)) AS error_msg, COUNT(CAST(entry_data AS varchar(MAX))) AS count_of_errorFROM pos_logWHERE (entry_data LIKE '%<MsgStatusCd>Error</MsgStatusCd>%')GROUP BY CAST(entry_data AS varchar(MAX)) |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-04-01 : 15:23:43
|
What happens when you run the query again, without the DISTINCT keyword? E 12°55'05.63"N 56°04'39.26" |
 |
|
|
chedderslam
Posting Yak Master
223 Posts |
Posted - 2009-04-01 : 15:34:37
|
| I have modified the query to limit the portion of the log being looked at to the relevant data.Here is the new query:SELECT DISTINCT SUBSTRING(CAST(entry_data AS varchar(MAX)), PATINDEX('%<MsgStatusDesc>%', CAST(entry_data AS varchar(MAX))) + 15, PATINDEX('%</MsgStatusDesc>%', CAST(entry_data AS varchar(MAX))) - (PATINDEX('%<MsgStatusDesc>%', CAST(entry_data AS varchar(MAX))) + 15)) AS Expr1, COUNT(SUBSTRING(CAST(entry_data AS varchar(MAX)), PATINDEX('%<MsgStatusDesc>%', CAST(entry_data AS varchar(MAX))) + 15, PATINDEX('%</MsgStatusDesc>%', CAST(entry_data AS varchar(MAX))) - (PATINDEX('%<MsgStatusDesc>%', CAST(entry_data AS varchar(MAX))) + 15))) AS countoferrorFROM pos_logWHERE (entry_data LIKE '%<MsgStatusCd>Error</MsgStatusCd>%')GROUP BY CAST(entry_data AS varchar(MAX))(I tried to indent properly, hope that comes across)I get 41 rows returned, each with a different error message, but the countoferror stays at 0.To answer your question, if I remove the distinct keyword and run the query below, I get 209 records, still with 1 for countoferror:SELECT SUBSTRING(CAST(entry_data AS varchar(MAX)), PATINDEX('%<MsgStatusDesc>%', CAST(entry_data AS varchar(MAX))) + 15, PATINDEX('%</MsgStatusDesc>%', CAST(entry_data AS varchar(MAX))) - (PATINDEX('%<MsgStatusDesc>%', CAST(entry_data AS varchar(MAX))) + 15)) AS Expr1, COUNT(SUBSTRING(CAST(entry_data AS varchar(MAX)), PATINDEX('%<MsgStatusDesc>%', CAST(entry_data AS varchar(MAX))) + 15, PATINDEX('%</MsgStatusDesc>%', CAST(entry_data AS varchar(MAX))) - (PATINDEX('%<MsgStatusDesc>%', CAST(entry_data AS varchar(MAX))) + 15))) AS countoferrorFROM pos_logWHERE (entry_data LIKE '%<MsgStatusCd>Error</MsgStatusCd>%')GROUP BY CAST(entry_data AS varchar(MAX)) |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-04-01 : 15:43:16
|
Now when you are on SQL Server 2005, you can 1. Edit column to VARCHAR(MAX)2. Edit column to XMLThis way you don't have to clutter your code with a massive amount of CAST's. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
|
|
|
|
|