9

Sort column by string in sql server?

 2 years ago
source link: https://www.codesd.com/item/sort-column-by-string-in-sql-server.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Sort column by string in sql server?

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

this is table in which error messages are stored.

is it possible to get count of all error message.

for e.g,

Message:- 'LogsIndex' was not. Count = 1

Message:- 'The controller for path' was not. Count = 5


Since the messages are not exactly the same, I can only see you using LIKE and 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

You could make it a bit more elegant using CTE or maybe a UNION if you want to simply it.

You would want to limit this by date or something.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK