简体   繁体   English

Postgres的SQL查询

[英]SQL query for Postgres

I am trying to write a query from one table name 't'. 我正在尝试从一个表名't'编写查询。 It has mainly 4 columns involved in my query. 它主要有4列涉及我的查询。 From this sample data, I need a specific output. 从这个样本数据中,我需要一个特定的输出。

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

The name of unit_transferred_to is actually in first column unit_name. unit_transferred_to的名称实际上在第一列unit_name中。 So I need name of the unit_transferred_to from unit_name and the action = 'converted' 所以我需要unit_name中的unit_transferred_to名称,并且action ='converted'

The output needs to look like this: 输出需要如下所示:

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

This seems like a join : 这似乎是一个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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM