Postgresql:按真实OR子句数计算等级 [英] Postgresql: Calculate rank by number of true OR clauses
本文介绍了Postgresql:按真实OR子句数计算等级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要按真实的OR子句的数量对PostgreSQL查询的结果进行排序/排序.例如,给定类似
I need to order/rank the results of a PostgreSQL query by the number of OR-clauses that are true. For example, given a query like
SELECT * FROM mytable WHERE cond1 OR cond2 OR cond3 ORDER BY rank DESC
应根据满足条件的数量对结果进行排名.还非常欢迎使用视图/存储过程解决此问题的方法!
should rank results based on the number of fulfilled conditions. Also approaches that solve this issue with views/stored procedures are very welcome!
推荐答案
重复条件并添加它们:
SELECT * FROM mytable
WHERE fld = 'A' OR fldB = CURRENT_DATE OR fldC = 7
ORDER BY
(fld = 'A')::int + (fldB = CURRENT_DATE)::int + (fldC = 7)::int
DESC
这篇关于Postgresql:按真实OR子句数计算等级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文