Tsql 按特定字符串值对数据进行排序

2021-09-10 00:00:00 sql tsql sql-server

我有一个审计表,其中包含用于插入、更新和删除操作的数据.我正在编写一个报告,它将按插入、更新和删除的顺序显示数据.我认为 order by 条款不会有帮助.有什么帮助吗?

I have an audit table that have data for insert, update and delete operations. I am writing a report that will display data in the order of Insert, Update and Delete. I don't think the order by clause will help. any help?

推荐答案

您可以将 ORDER BY 与 CASE 子句:

You can use an ORDER BY combined with a CASE clause:

SELECT *
FROM auditlog
ORDER BY CASE operation_type
    WHEN 'Insert' THEN 1
    WHEN 'Update' THEN 2
    WHEN 'Delete' THEN 3
END

相关文章