SQL 查询中的复杂排序:可能使用 CASE WHEN
我有一个复杂的优先级算法,我想在返回优先级数据页时用 SQL 编写该算法。
其中一些根据固定值确定优先级,另一些则根据变量值排序。即
// Initial sort on ATTR1 (value1/value2 is higher than value3)
if ATTR1 = value1 or value2
then orderBy creationDate, then modifiedDate, then author
else if ATTR1 = value3
then
// if ATTR1 = value3, do secondary sort on ATTR2 (value4 is higher than value5)
if ATTR2 = value4
then orderBy oldPriority, then modifiedDate, then creationDate, then author
if ATTR2 = value5
then orderBy creationDate, then modifiedDate
我已经查看了 SQL CASE WHEN,但不确定如何使其与第二级属性比较一起工作。
CASE WHEN 是实现这一目标的合适工具吗?有人对处理额外的复杂性有什么建议吗?
先感谢您!
I have a complex prioritisation algorithm that I want to write in SQL to when return prioritised pages of data.
Some of these are prioritised according to fixed values, other are ordered by variable values. i.e.
// Initial sort on ATTR1 (value1/value2 is higher than value3)
if ATTR1 = value1 or value2
then orderBy creationDate, then modifiedDate, then author
else if ATTR1 = value3
then
// if ATTR1 = value3, do secondary sort on ATTR2 (value4 is higher than value5)
if ATTR2 = value4
then orderBy oldPriority, then modifiedDate, then creationDate, then author
if ATTR2 = value5
then orderBy creationDate, then modifiedDate
I have had a look at SQL CASE WHEN, but not sure how to make that work with the second level of attribute comparison.
Is CASE WHEN a suitable tool for achieving this? Does anyone have any tips on dealing with the additional levels of complexity?
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样的事情应该做到这一点:
我假设 oldPriority 是一个 NUMBER 列,但情况可能并非如此。关键是使用适当的格式掩码将所有内容转换为顺序正确的字符串。
Something like this should do it:
I assumed that oldPriority was a NUMBER column, which may not be the case. The point is to convert everything to strings that order correctly, using appropriate format masks.
做出一些小假设,看起来这应该可行:
Making a couple small assumptions, it seems this should work: