作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在表中有以下行:
C1 |C2 |C3 |C4
7 | 3 | 1 | 6
O1 |O2 |O3 |O4
4 | 2 | 1 | 3
case
when C1 = greatest ( C1, C2, C3, C4) then 1
when C1 >= C2 and C1 >= C3 and C1 < C4
or C1 >= C2 and C1 < C3 and C1 >= C4
or C1 < C2 and C1 >= C3 and C1 >= C4 then 2
when C1 >= C2 and C1 < C3 and C1 < C4
or C1 < C2 and C1 >= C3 and C1 < C4
or C1 < C2 and C1 < C3 and C1 >= C4 then 3
when C1 = least (C1, C2, C3, C4 ) then 4
end as O1
最佳答案
复杂的嵌套大小写-不需要时,可以使用“简单”的大小写-当有总和时(虽然有点乏味)
select t.*,
case when c1>c2 then 1 else 0 end
+ case when c1>c3 then 1 else 0 end
+ case when c1>c4 then 1 else 0 end + 1 as q1,
case when c2>c1 then 1 else 0 end
+ case when c2>c3 then 1 else 0 end
+ case when c2>c4 then 1 else 0 end + 1 as q2,
case when c3>c1 then 1 else 0 end
+ case when c3>c2 then 1 else 0 end
+ case when c3>c4 then 1 else 0 end + 1 as q3 ,
case when c4>c1 then 1 else 0 end
+ case when c4>c2 then 1 else 0 end
+ case when c4>c3 then 1 else 0 end + 1 as q4
FROM table1 t;
| c1 | c2 | c3 | c4 | q1 | q2 | q3 | q4 |
|----|----|----|----|----|----|----|----|
| 7 | 3 | 1 | 6 | 4 | 2 | 1 | 3 |
| 6 | 5 | 4 | 1 | 4 | 3 | 2 | 1 |
关于sql - 如何在Oracle SQL中对同一行的列进行排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50932751/
我是一名优秀的程序员,十分优秀!