0

See the original question and code further down. Here is the code that works:

SELECT v1.s_owner_guid,
       n.node_name as s_owner_name
FROM vm_list v 
    CROSS JOIN LATERAL (
     SELECT (VALUES (TRIM(split_part(v2.set_of_guids, ':', 3), '|')))
     ) v1(s_owner_guid)
     LEFT JOIN nodes n
     ON n.id = v1.s_owner_guid::uuid

Original code that does not work:

SELECT 
    (TRIM (split_part(v2.set_of_guids, ':', 3)), '|') AS s_owner_guid
        , n1.node_name as s_owner_name
    FROM vm_list as v2
    INNER JOIN nodes 
        AS n1
        ON n1.id=s_owner_guid
   

I want to get s_owner_guid and s_owner_name out of this SELECT

This is a part of the query that I am having issues with and is a nested SELECT

The error I get is:
ERROR:  column "s_owner_guid" does not exist
LINE 7:   ON v2.owner=s_owner_guid

I have a set_of_guids containing 2 node uids seperated by certain characters

I have 2 tables with the information I need

  • vm_list and nodes are the 2 tables
  • vm_owner_guid is a column in vm_list table that has the uid matching id column in nodes table
  • id is a column in the nodes table
  • vm_name is a column in vm_list with the VM name
  • node_name is a column in nodes with that is associated with the id column in nodes table
  • the first uid in the set_of_guids matches the vm_owner and also the id column in the nodes table and has a node_name in another column in nodes

I need to parse the set_of_nodes to get the second uid for the VM so I am using the SPLIT_PART and TRIM options to isolate the uid and am calling it s_owner_guid

I need to take s_owner_guid and match it against id in the nodes table to get the second node_name

Ultimately the output will look something like this:

vm_name      |   p_owner_guid                        |   p_owner_name    |   s_owner_guid                        |   s_owner_name
-------------+---------------------------------------+-------------------+---------------------------------------+-----------------------
 NAMEOFVM1   |  b2a0bb4e-0a6a-4208-8ff1-6df549cf9c3f | primary_node_name | 1c732242-56d4-c9c8-d275-ba271600c314  |  secondary_node_name

I can get the first 4 fields now, but not the last s_owner_name. Is there any way to accomplish this? I am trying not to create a temporary table due to having a live system depending on the DB.

4

2 回答 2

1

Postgres supports lateral joins, which are a nice way of accomplishing this:

SELECT v.s_owner_guid,
       n.node_name as s_owner_name
FROM vm_list v2 CROSS JOIN LATERAL
     (VALUES (TRIM(split_part(v2.set_of_guids, ':', 3), '|')))
     ) v(s_owner_guid)
     nodes n
     ON n.id = v.s_owner_guid;

A lateral join allows you to define the column in the FROM clause -- it can then be used in expressions in the SELECT, FROM, or WHERE, just like any other column.

于 2020-08-22T12:07:35.120 回答
0

You can't use a column alias on the same level where you introduced it. You need to wrap the query in a derived table:

SELECT v2.s_owner_guid,
       n1.node_name as s_owner_name
FROM (
  select trim(split_part(v2.set_of_guids, ':', 3)), '|') AS s_owner_guid
  FROM vm_list
) as v2
  JOIN nodes AS n1 ON n1.id = s_owner_guid
于 2020-08-22T08:37:43.187 回答