在sql server中按字符串对列排序?

在sql server中按字符串对列排序?,sql,sql-server,Sql,Sql Server,这是存储错误消息的表 是否可以获取所有错误消息的计数 例如 消息:-“LogsIndex”未被激活。计数=1 消息:-“路径的控制器”无效。计数=5请尝试这个答案 LogID Title Message 1 Error Occured Could not find stored procedure 'RT_SELECTAll1_Users'. : [UserID={2}],[UserSessionID={10068}] 2 Error Occured A public a

这是存储错误消息的表

是否可以获取所有错误消息的计数

例如

消息:-“LogsIndex”未被激活。计数=1


消息:-“路径的控制器”无效。计数=5请尝试这个答案

LogID   Title   Message
1   Error Occured   Could not find stored procedure 'RT_SELECTAll1_Users'. : [UserID={2}],[UserSessionID={10068}]
2   Error Occured   A public action method 'LogsIndex' was not found on controller 'oMail.Web.Controllers.EmailTemplateController'. : [UserID={2}],[UserSessionID={20071}]
3   Error Occured   The controller for path '/ControllerName/ActionName' was not found or does not implement IController. : [UserID={2}],[UserSessionID={20071}]
4   Error Occured   The controller for path '/ControllerName/ActionName' was not found or does not implement IController. : [UserID={2}],[UserSessionID={20071}]
5   Error Occured   The controller for path '/ControllerName/ActionName' was not found or does not implement IController. : [UserID={2}],[UserSessionID={20071}]
6   Error Occured   The controller for path '/ControllerName/ActionName' was not found or does not implement IController. : [UserID={2}],[UserSessionID={20071}]
7   Error Occured   The controller for path '/ControllerName/ActionName' was not found or does not implement IController. : [UserID={2}],[UserSessionID={20071}]
8   Error Occured   The controller for path '/ControllerName/ActionName' was not found or does not implement IController. : [UserID={2}],[UserSessionID={20071}]

选择消息并执行计数,然后根据消息对结果进行分组,如下所示:

SELECT Message, count(1) as MCount 
FROM TableName

由于消息不完全相同,我只能看到您使用LIKE和case:

    Select Message, Count(*) as 'Count'
    From Table
    Group by Message
你可以使用CTE或者一个联合体使它更优雅一些,如果你想简化它的话

您可能希望通过日期或其他方式对此进行限制。

选择消息,从TableName中将(1)计数为MCount
SELECT
    CASE 
        WHEN Message like '%LogsIndex%' THEN 'LogsIndex'
        WHEN Message like 'The controller for path'' was not%' THEN 'Controller Path'
        ELSE 'Other'
    END AS UserMessage
    ,count(*)
FROM
    TableName
Group by
    CASE 
        WHEN Message like '%LogsIndex%' THEN 'LogsIndex'
        WHEN Message like 'The controller for path' was not%' THEN 'Controller Path'
        ELSE 'Other'
    END