sql问题

当four为公司名称时three就是体系,two就是部门,取出体系和部门

当three为公司名称时two就是体系,one就是部门,取出体系和部门

这种的sql该怎么写?

各位大神写个伪代码吧(orcale)


无标题.png

愤怒的老头 发布于 2020-10-28 16:29
1min目标场景问卷 立即参与
回答问题
悬赏:3 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共5回答
最佳回答
0
luckybaiLv4初级互助
发布于2020-10-28 16:37

如果只有你上面列的两种情况的话,可以用这个sql取到:

select decode(four,'公司名称',four, three) as 公司名称, decode(three, '公司名称', two, three) as 体系, decode(four,'公司名称',two, one) as 部门 from table

  • 愤怒的老头 愤怒的老头(提问者) 大哥我再加一句当two为公司名称的时候,one既是体系也是部门这该怎么写?
    2020-10-28 16:52 
  • luckybai luckybai 回复 愤怒的老头(提问者) select decode(four,\'公司名称\',four, decode(three,\'公司名称\',three,two)) as 公司名称, decode(three,\'体系\',three, decode(two,\'体系\',two,one)) as 体系, decode(four,\'公司名称\',two, decode(three,\'公司名称\',one,one)) as 部门 from table
    2020-10-28 17:06 
  • 愤怒的老头 愤怒的老头(提问者) 回复 luckybai decode(three,\\\'体系\\\',three, decode(two,\\\'体系\\\',two,one)) as 体系,大哥这一句的引号里的体系是个死值?还是随意取得?
    2020-10-28 17:20 
  • luckybai luckybai 回复 愤怒的老头(提问者) 随意啊,看你要用哪个字段判断,取你要判断的那个字段就行
    2020-10-28 17:21 
  • luckybai luckybai 回复 愤怒的老头(提问者) select \'公司名称\' as 公司名称, decode(DEPT_four,\'公司名称\',DEPT_three, decode(DEPT_three,\'公司名称\',DEPT_two,DEPT_one)) as 体系, decode(DEPT_four,\'公司名称\',DEPT_two, decode(DEPT_three,\'公司名称\',DEPT_one,DEPT_one)) as 部门 from table
    2020-10-29 13:46 
最佳回答
1
shirokoLv6资深互助
发布于2020-10-28 16:34

首先要怎么判断是公司名称。。

最佳回答
0
free_zzLv6中级互助
发布于2020-10-28 16:31(编辑于 2020-10-28 16:33)

case when four=‘公司名称’then three else‘’end AS 体系

最佳回答
0
沉默的反补Lv6中级互助
发布于2020-10-28 16:32(编辑于 2020-10-28 16:32)
select
        case
        when four= '公司名称' then three = '体系',two = '部门' end as four,
       case
        when three = '公司名称' then two= '体系',one= '部门' end as three 
from dual


最佳回答
0
touyuan001Lv6中级互助
发布于2020-10-28 17:18

SELECT DECODE(three,'公司名称',TWO,four,'公司名称',three) AS 体系,

            DECODE(three,'公司名称',TWO,ONE,'公司名称',TWO) AS 部门 

FROM 表名

  • 6关注人数
  • 416浏览人数
  • 最后回答于:2020-10-28 17:18
    请选择关闭问题的原因
    确定 取消
    返回顶部