'Unexpected Behavior with XML Lateral Flatten In Snowflake (Rows with 1 value being filtered)

I am working with some XML data in Snowflake where I am trying to access some data in XML subnodes.

The data I am after looks something like this:

<Employee>
     <EmploymentStatuses>
        <EmployeeEmploymentStatus>
            <OtherInformation>
            </OtherInformation>            
        </EmployeeEmploymentStatus>
</Employee>

For a given employee, they may have multiple 'EmployeeEmploymentStatus'.

To account for this, I created a view like the below:

SELECT
  XMLGET(XML_CONTENT,'EMPLOYEE_CODE'):"$"::STRING AS EMPLOYEE_CODE
  , XMLGET(EMPLOYMENT_STATUS.value, 'EffectiveStart'):"$"::DATE AS EffectiveStart
...........
FROM
  XML_FILE
  LATERAL FLATTEN(XML_CONTENT:"$") STATUSES
  LATERAL FLATTEN(STATUS.VALUE:"$") EMPLOYMENT_STATUS
WHERE 
 GET(STATUSES.VALUE, '@') = 'EmploymentStatuses'
AND GET(EMPSTAT.VALUE, '@') = 'EmployeeEmploymentStatus'

The problem I am running into is that while this looks perfect in situations where an employee has multiple 'EmploymentStatus', if they only have one it is filtering them off. (i.e. anyone with 2 or more employment statuses shows ALL of their statuses as you would expect, but someone with only 1 employment status does not appear at all)

If I remove the second lateral flatten and just use nested XMLGET in the select portion, I am able to return the value, but for employees with multiple 'EmploymentStatus' it only returns the first value.

When looking at the output of STATUSES.VALUE, format-wise they appear identical and have all the same tags.

The only thing I can think to do is basically a union of those two tables, or doing something like modifying the 'where statement to be something like this, which would then require coalesce statements on all the fields:

GET(EMPLOYMENT_STATUS.value, '@') = 'EmployeeEmploymentStatus' OR GET(STATUSES.value:"$",'@')::STRING = 'EmployeeEmploymentStatus')

I tested this method and it seems to work, but it also seems kind of clunky and unintuitive.

Any advice on this would be appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source