【问题标题】:SQL query for PostgresPostgres 的 SQL 查询
【发布时间】:2019-07-13 12:20:20
【问题描述】:

我正在尝试从一个表名“t”编写查询。我的查询中主要涉及 4 列。从这个示例数据中,我需要一个特定的输出。

Unit_name      Unit_id_from   unit_transferred_to     Action
------------------------------------------------------------
UNITABC        011102X         0215478Y               CONVERTED  
UNITQWE        0222487Y        NULL                   NEW RAISED   
UNITASDF       0215478Y        NULL                   INVALID   
UNITPOU        0487985Z        08975469K              CONVERTED  
UNITHUT        08975469K       NULL                   INACTIVE

unit_transferred_to 的名称实际上在第一列 unit_name 中。所以我需要来自 unit_name 的 unit_transferred_to 的名称和 action = 'converted'

输出需要如下所示:

Unit_name   Unit_id_from   unit_transferred_to     unit_transfer_name     Action
----------------------------------------------------------------------------------
UNITABC       011102X        0215478Y              UNITASDF               CONVERTED  
UNITPOU       0487985Z       08975469K             UNITHUT                CONVERTED

【问题讨论】:

    标签: sql postgresql postgresql-9.4


    【解决方案1】:

    这好像是join

    select ti.Unit_name, ti.Unit_id_from, ti.unit_transferred_to,
           tito.unit_name as unit_transfer_name, 
           ti.Action
    from ti join
         ti tito
         on ti.unit_transferred_to = tito.unit_transferred_from
    where ti.action = 'CONVERTED'
    

    【讨论】:

    • 表 t 中没有名为 unit_transfer_name 的列。我想将 unit_name 别名为输出中的两列,其中一列是 unit_name 作为 unit_name_from,unit_name 作为 unit_name transfer_to.. 它必须通过 join 来完成,但是我没有正确理解
    猜你喜欢
    • 2015-07-18
    • 2012-03-21
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2012-09-28
    • 1970-01-01
    • 2013-08-19
    • 1970-01-01
    相关资源
    最近更新 更多