I was reading this article that is about Google BigQuery:
Exploring a powerful SQL pattern: ARRAY_AGG, STRUCT and UNNEST
They use a couple of functions that I am trying to figure out what the Oracle equivalents are, if they even exist.
I am digging through the Oracle documentation and I found something similar to ARRAY_AGG
in Oracle called LISTAGG
.
What I have not been able to find are the idiomatic equivalents of STRUCT
and UNNEST
.
Best Answer
Technique Translation
The technique for solving a problem in one RDBMS doesn't always translate well into another.
I've had performance problems with similar code to that which was in the article. Thus, my comment "This technique will kill performance in Oracle".
I don't see
CAST(MULTISET())
(Oracle's version ofARRAY_AGG
) being used too much. As such, the code might be unmaintainable by your replacement after you get a promotion.Translating Terms
Translation goes like this
STRUCT
in BigData isTYPE
in OracleARRAY_AGG
in BigData isCAST(MULTISET())
in OracleUNNEST
in BigData isTABLE()
in OracleUNNEST
of XML Data isXMLTABLE()
in OracleUNNEST
of JSON Data isJSON_TABLE()
in Oracle (12c+)You can't create the structure on the fly. You have to define it ahead of time.
EXAMPLES
Creating a structure of a single row:
Creating a structure of a set of rows based on above: (
nested table
)Using
CAST(MULTISET())
Using
TABLE()
Oracle's Solution
Business Requirement: I want the first time a hurricane reached its maximum category along with its position.
Other, inferred, identifiers to "GROUP BY" would be season, basin, subbasin in addition to hurricane name.
One method to solve this in Oracle is to use Analytics.
Since I don't have access to the huricane data, I'll have to improvise.
First thing we do is use the analytic function
RANK()
to rank all of the rows bycategory
(descending) andtime
(ascending) but partition the rankings byseason
,basin
,subbasin
, andhurricane_name
Finally, we'll pick only the best (rank_score=1)
Putting it all together