【问题标题】:query retrieve data查询检索数据
【发布时间】:2020-02-14 21:48:45
【问题描述】:

我在这样的表中有数据

insert into mytable (col1,col2,col3,col4) values(a,2,100,501);
insert into mytable (col1,col2,col3,col4) values(a,4,200,555);
insert into mytable (col1,col2,col3,col4) values(a,3,80,500);
insert into mytable (col1,col2,col3,col4) values(a,3,44,443);

现在我想要一个单选来检索这样的数据:

  • 如果 col1=a 和 col2=2,amnt1 将分配给 col3。

  • 如果 col1=a 和 col2=3 和 col4=500,amnt2 将分配给 col3。

结果:

| amnt1 | amnt2 |
|---------------|
|   100 |    80 |

谢谢

【问题讨论】:

  • 请向我们展示您期望的结果。此外,您的示例数据中没有列amnt1amnt2(它有列col1col4)。
  • @GMB amnt1=100 amnt2=80 我想在一行中显示,所以 amnt2 和 amnt2 是 col3 的别名
  • edit your question 以表格文本的形式显示您期望的结果。此外,还不清楚您是想要 update 还是 select 声明。
  • @GMB 一个选择,,我已经编辑了帖子,

标签: sql oracle plsql oracle11g


【解决方案1】:

这是你想要的吗?

select
    (select col3 from mytable where col1 = 'a' and col2 = 2) amnt1,
    (select col3 from mytable where col1 = 'a' and col2 = 3 and col4 = 500) amnt2
from dual

请注意,这要求只有一条记录与两组条件匹配。如果有多个匹配记录,上述查询会引发运行时错误;为避免这种情况,您可以改用条件聚合:

select
    sum(case when col1 = 'a' and col2 = 2 then col3 end) amnt1,
    sum(case when col1 = 'a' and col2 = 3 and col4 = 500 then col3 end) amnt2
from mytable

【讨论】:

    【解决方案2】:

    select * from mytable m where m.col3='amnt1' and m.clo1='a' and m.col2=2

    【讨论】:

    • 抱歉...应该是 >> update mytable set col2='amnt1' where clo1='a' and col2=2
    猜你喜欢
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多