在SQL Server中按字符串排序?

时间:2017-06-19 07:02:37

标签: sql sql-server

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}]

这是存储错误消息的表。

是否可以计算所有错误消息。

例如,

消息: - ' LogsIndex'不是。数= 1

消息: - '路径控制器'不是。数= 5

3 个答案:

答案 0 :(得分:1)

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

    Select Message, Count(*) as 'Count'
    From Table
    Group by Message

答案 1 :(得分:1)

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

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 

如果您想简单地使用CTE或者UNION,可以使它更优雅。

您可能希望按日期限制此项。

答案 2 :(得分:0)

请尝试这个答案。

SELECT Message, count(1) as MCount 
FROM TableName