【问题标题】:How to select another column in table if first column doesnt have value that i need如果第一列没有我需要的值,如何在表中选择另一列
【发布时间】:2020-07-15 13:12:09
【问题描述】:

我有 1 个表,其中有 2 列我现在需要使用(长度和 length_to_fault)。如果长度有一个空值(N/A),那么我需要从 length_to_fault 和相反的值中选择值。 我还需要对所有内容进行排序,我可以使用这样的 1 列来完成:

select d.* from (select d.*, lead(length::float) over (partition by port_nbr, pair order by add_date) as next_length from diags d where length != 'N/A') d

此选择对除 length_to_fault 之外的所有内容进行排序。如果 1 条记录在 length_to_fault 中有值,它将被忽略并且不会显示。 有什么建议吗? 也许可以将这 2 列变成 1?这听起来很合乎逻辑。但是怎么做呢?

【问题讨论】:

  • 请提供样本数据和期望的结果。
  • 你可以试试 Coalesce。
  • 我很困惑。我认为当下一个值为'N/A' 时,您的lead() 应该会产生类型转换错误。您还提到“排序”,但您的问题没有order by。并且查询中没有名为 length_to_fault 的列。
  • 我的表中有length_to_fault,但我不知道如何在我的代码中使用它。一切都按我的需要工作,但只有长度。有 1 个问题:有 2 列 - length 和 length_to_fault。如果 length 为 N/A,则 length_to_fault 有一个值。如果 length_to_fault 为 N/A,则 length 具有价值。我可以将这 2 列合并为 1,但我不知道如何

标签: sql select lead


【解决方案1】:

我改成 select d.* from (select d.*, lead(sum(length::float + length_to_fault::float)) over (partition by port_nbr, pair order by d.add_date) as next_length from diags d)d 我得到错误:列“d.ip”必须出现在 group by 子句中或在聚合函数中使用。 我不需要使用 ip 列...我什至现在都不知道该 ip 放在哪里

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 2021-04-23
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多