我需要创建一份清单,列出英国的2所评级酒店,它们从一开始就至少提高了3分。
Month | Hotel | Rating | Region |
---------------------------------------
01-Jan-19 | A | 1 | US |
01-Feb-19 | B | 2 | UK |
01-Mar-19 | C | 3 | EU |
01-Apr-19 | A | 1 | US |
01-May-19 | B | 4 | UK |
01-Jun-19 | C | 3 | EU |
01-Jul-19 | A | 1 | US |
01-Aug-19 | B | 5 | UK |
01-Sep-19 | C | 4 | EU |
像这样,查询必须仅生成酒店 B 。
答案 0 :(得分:1)
听起来您想要第一个和最后一个条目。一种方法使用条件聚合。我将假设month
实际上是日期或数字,而不是字符串:
select t.hotel
from (select t.*,
row_number() over (partition by hotel order by month asc) as seqnum_asc,
row_number() over (partition by hotel order by month desc) as seqnum_desc
from t
) t
group by t.hotel
having max(rating) filter (where seqnum_asc = 1) >= max(rating) filter (where seqnum_desc = 1) + 3;
答案 1 :(得分:0)
这也有效
我尝试过
Select "Hotel"
From T
Where "Region" = 'UK'
Group by "Hotel"
Having
Min ("Rating") = 2
And
Max ("Rating") >= 5
测试链接: