I Beispiel ist Wiederverwendung Sie in bq command line tool - How to insert into Big query tables that has nested fields?
bereitgestellt unter Versuchen, es für Legacy SQL ist und mit in-line-Version von Javascript UDF für Legacy SQL
Hinweis: standardmäßig BigQuery Legacy-SQL kein Ergebnis abflacht, so machen sicher stellen Sie Zieltabelle und legen Allow Large Results
auf true (oder überprüfen Sie es in Web-UI) und Flatten Results
auf false (oder deaktivieren Sie es in Web-UI)
SELECT Employee_id, Name, Age, Department.*, Location.* FROM JS((
SELECT Employee_id, Name, Age, Department_id, Department_Name, Department_Code, e.Location_id AS Location_id, Country, State, City
FROM (SELECT e.Employee_id AS Employee_id, e.Name AS Name, e.Age AS Age,
e.Department_id AS Department_id, d.Department_Name AS Department_Name, d.Department_Code AS Department_Code, e.Location_id AS Location_id
FROM Employee e JOIN Department d ON e.Department_id = d.Department_id) AS e
JOIN Location l ON e.Location_id = l.Location_id
),
// input columns
Employee_id, Name, Age, Department_id, Department_Name, Department_Code, Location_id, Country, State, City,
// output schema
"[
{'name': 'Employee_id', 'type': 'INTEGER', 'mode': 'NULLABLE'},
{'name': 'Name', 'type': 'STRING', 'mode': 'NULLABLE'},
{'name': 'Age', 'type': 'INTEGER', 'mode': 'NULLABLE'},
{'name': 'Department', 'type': 'RECORD', 'mode': 'NULLABLE', 'fields': [
{'name': 'Department_id', 'type': 'STRING', 'mode': 'NULLABLE'},
{'name': 'Department_Name', 'type': 'STRING', 'mode': 'NULLABLE'},
{'name': 'Department_Code', 'type': 'STRING', 'mode': 'NULLABLE'}
]},
{'name': 'Location', 'type': 'RECORD', 'mode': 'NULLABLE', 'fields': [
{'name': 'Location_id', 'type': 'STRING', 'mode': 'NULLABLE'},
{'name': 'Country', 'type': 'STRING', 'mode': 'NULLABLE'},
{'name': 'State', 'type': 'STRING', 'mode': 'NULLABLE'},
{'name': 'City', 'type': 'STRING', 'mode': 'NULLABLE'}
]}
]",
// function
"function(r, emit){
emit({
Employee_id: r.Employee_id, Name: r.Name, Age: r.Age,
Department: {Department_id:r.Department_id, Department_Name:r.Department_Name, Department_Code:r.Department_Code},
Location: {Location_id:r.Location_id, Country:r.Country, State:r.State, City:r.City}
});
}"
)
Bitte beachten Sie: i in-line bin mit Version von UDF hier für den Zweck von einfach Zeigen und Testen. Inline-Version wird nicht empfohlen und nicht offiziell unterstützt. Aber Sie können es leicht zur unterstützten Version umwandeln - sehen Sie mehr für User-Defined Functions in Legacy SQL
P.S. obwohl das oben funktioniert und viel geholfen hat, bevor Standard SQL eine Option war - was ist der Hauptgrund für Sie, Legacy SQL zu verwenden, wo Standard SQL eleganter ist und Ihnen viel mehr Flexibilität gibt, besonders wenn es um Verschachtelung und Wiederholung geht Felder
Ich werde die offensichtliche Frage stellen ... Warum Alt-SQL für diesen Zweck verwenden? Es hat ziemlich strenge Einschränkungen im Zusammenhang mit dem Erstellen von geschachtelten und wiederholten Feldern für Abfrageergebnisse. –
@vetri - Wie Elliot feststellt, sollten Sie sich wirklich darauf konzentrieren, in Ihren Lösungen Standard-SQL in BigQuery zu verwenden und Legacy-SQL zu entfernen. –
Sicher. Ich verstehe die Vorteile von Standard SQL. Also Migration auf Standard SQL. Danke allen. – Vetri