【问题标题】:SQL: conditioning in aggregation functionSQL:聚合函数中的条件
【发布时间】:2017-11-25 22:50:08
【问题描述】:

我有以下 sql:

  SELECT LISTAGG((TO_CHAR(ch.count), '|') WITHIN GROUP (ORDER BY ch.Count)
    FROM ChG cg
    JOIN Ch ch on ch.GroupID = cg.GroupID
    WHERE cg.PartyID = cp.PartyID

我想添加条件,伪代码: if(ch.TYPECODE = 1) then ch.count = 'A' + ch.count。在存储过程中如何更好地实现?

【问题讨论】:

  • 'A' + ch.count 是什么意思?你的意思是串联吗? Oracle 使用 ||,而不是加号。

标签: sql oracle stored-procedures conditional-statements


【解决方案1】:
listagg(case when ch.typecode = 1 then 'A' end || to_char(ch.count), '|') .....

在聚合之前,会检查每一行的条件ch.typecode = 1。如果为真,则'A' 会在to_char(ch.count) 前面附加(连接在前面)。我只是猜测这就是你需要的。

如果您还需要将此 'A' 预先添加到 ch.count 以用于 ORDER BY 条件,那么您可以在那里做同样的事情。您还需要包含在 to_char 中。 (如果你不这样做,甲骨文老大哥无论如何都会为你做,但尽量避免隐式转换。)

【讨论】:

    猜你喜欢
    • 2014-09-28
    • 2018-04-21
    • 2017-02-19
    • 1970-01-01
    • 2017-05-10
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多