I am trying to Select the two values I have highlighted in the image (attributes.price.list.item.net AND attributes.price.list.item.listPrice.gross)
I am using the following snippet but it just flattens the whole list array and returns every column within. If I try to unnest any other way, I only get errors. How can I unnest multiple nested arrays like this?
SELECT attributes.price.list
FROM my_table LEFT JOIN UNNEST(attributes.price.list)
Consider below approach
SELECT
el.item.net,
el.item.listPrice.gross
FROM my_table
LEFT JOIN UNNEST(attributes.price.list) el
Related
I'm looking for assistance in the below SQL query where column policy_array has NULL values in some rows, but arrays of policy data in others. I would like to be able to include data from rows even when policy_array is NULL in the output.
When I execute the below query it executes a CROSS JOIN UNNEST as expected but also drops all data from columns with NULLs in the column policy_array as expected as well. I can imagine a work around by having an intermediate table where NULLs in policy_array are changed to something else, but I really would prefer not to do that.
SELECT
policy,
account_id,
rejects,
overturns,
appeals,
submits
FROM relevant_table
CROSS JOIN UNNEST(policy_array) AS p (policy)
WHERE
...
There are two options either LEFT JOIN with on true:
FROM relevant_table
LEFT JOIN UNNEST(policy_array) AS p (policy) ON true
Or a little bit more hackish which uses the fact that unnest supports multiple arrays - add array with one element (also note succinct syntax for cross join unnest):
FROM relevant_table,
UNNEST(policy_array, array[1]) AS p (policy, ignored)
My data is in array format like [1,2,3] how to join with another table. Here the query i am trying:
select
RCM.header_details->'auditAssertion'as auditassertion
from masters."RCM" RCM
left join reference."AUDIT_ASSERTION_APPLICATION" as AAA on AAA.id=RCM.header_details->'auditAssertion'
You can use the ? operator to check if a value belongs to json(b) array:
select m.header_details->'auditAssertion'as auditassertion
from masters.rcm m
left join reference.audit_assertion_application a
on m.header_details->'auditAssertion' ? a.id::text
For performance, Postgres would support the following index:
create index on masters.rcm using gin ((header_details->'auditAssertion'));
I'm trying to Replace the schema in existing table using BQ. There are certain fields in BQ which have 3-5 level schema dependency.
For Ex. comsalesorders.comSalesOrdersInfo.storetransactionid this field is nested under two fields.
Since I'm using this to replace existing table, I can not change the field names in query.
The query looks similar to this
SELECT * REPLACE(comsalesorders.comSalesOrdersInfo.storetransactionid AS STRING) FROM CentralizedOrders_streaming.orderStatusUpdated, UNNEST(comsalesorders) AS comsalesorders, UNNEST(comsalesorders.comSalesOrdersInfo) AS comsalesorders.comSalesOrdersInfo
BQ enables unnesting first schema field but presents problem for 2nd nesting.
What changes do I need to make to this query to use UNNEST() for such depedndent schemas ?
Given that you don't have a schema, I will try to provide a generalized answer. Please try to understand the difference between the 2 queries.
-- Provide an alias for each unnest (as if each is a separate table)
select c.stuff
from table
left join unnest(table.first_level_nested) a
left join unnest(a.second_level_nested) b
left join unnest(b.third_level_nested) c
-- b and c won't work here because you are 'double unnesting'
select c.stuff
from table
left join unnest(table.first_level_nested) a
left join unnest(first_level_nested.second_level_nested) b
left join unnest(first_level_nested.second_level_nested.third_level_nested) c
I'm not sure I understand your question, but as I could guess, you want to change one column type to another type, such as STRING.
The UNNEST function is only used with columns that are array types, for example:
"comsalesorders":["comSalesOrdersInfo":{}, comSalesOrdersInfo:{}, comSalesOrdersInfo:{}]
But not with this kind of columns:
"comSalesOrdersInfo":{"storeTransactionID":"X1056-943462","ItemsWarrenty":0,"currencyCountry":"USD"}
Therefore, if a didn't misunderstand your question, I would make a query like this:
SELECT *, CAST(A.comSalesOrdersInfo.storeTransactionID as STRING)
FROM `TABLE`, UNNEST(comsalesorders) as A
I am selecting data from Google Bigquery table which includes JSON column. My table has multiple nested arrays, one of the includes two nested levels.
here is my table schema
https://imgur.com/UBPKUMx
My statement is:
SELECT
items.*,
pay.*,
credits.creditnoteid,
credits.id,
credits.total
FROM client_account.invoices,
UNNEST(lineitems) items,
UNNEST(items.tracking),
UNNEST(payments) pay,
UNNEST(creditnotes) credits
https://imgur.com/c1YT258
Unfortunately I get no results...
Can you help me to unnest all of the arrays.
Ok, I did a test on one of my datasets. I think that creditnotes is always null. Because in my case I get no results when I unnest a column that is always null. You can fix it by using LEFT JOIN I modified your query to use left joins but you might be able to tune it better.
SELECT
items.*,
tracking.*,
pay.*,
credits.creditnoteid,
credits.id,
credits.total
FROM client_account.invoices
LEFT JOIN UNNEST(lineitems) items
LEFT JOIN UNNEST(items.tracking) tracking
LEFT JOIN UNNEST(payments) pay
LEFT JOIN UNNEST(creditnotes) credits
I'm trying to run a join on a repeated field.
Originally I get an error:
Cannot join on repeated field payload.pages.action
I fix this by running flatten on the relevant table (this is only an example query - it will give empty result if it would successfully run):
SELECT
t1.repository.forks
FROM publicdata:samples.github_nested t1
left join each flatten(publicdata:samples.github_nested,payload.pages) t2
on t2.payload.pages.action=t1.repository.url
I get a different error:
Table wildcard function 'FLATTEN' can only appear in FROM clauses
This used to work in the past. Is there some syntax change?
I don't think there has been a syntax change, but you should be able to wrap the flatten statement in a subselect. That is,
SELECT
t1.repository.forks
FROM publicdata:samples.github_nested t1
left join each (SELECT * FROM flatten(publicdata:samples.github_nested,payload.pages)) t2
on t2.payload.pages.action=t1.repository.url