GCP

Creating a query based on record type data field in Google BigQuery

CK CK 3788 
Created at  
1,639 0 0

In Google BigQuery, the RECORD type (also known as STRUCT) is a complex data type that allows you to store nested and repeated fields within a table. It helps in organizing hierarchical or structured data efficiently.

 

Key Features of RECORD Type

  1. Nested Fields (STRUCTs)
    • A RECORD can contain multiple subfields, each with its own data type.
    • It is similar to an object or dictionary in programming.
  2. Repeated Fields (Arrays of STRUCTs)
    • A RECORD field can be repeated, meaning it behaves like an array of structures.
    • Useful for handling one-to-many relationships.
  3. Improves Query Performance
    • Helps in reducing joins since related data is stored together.
    • Allows for efficient querying of nested data.

Example 1: Nested RECORD (STRUCT)

Let's define a table where a customer has an address as a nested field.

Schema Definition

Column NameData Type
customer_idSTRING
nameSTRING
addressRECORD (STRUCT)
├── streetSTRING
├── citySTRING
├── zipSTRING

Creating the Table

CREATE TABLE my_dataset.customers (
  customer_id STRING,
  name STRING,
  address STRUCT<
    street STRING,
    city STRING,
    zip STRING
  >
);

Inserting Data

INSERT INTO my_dataset.customers (customer_id, name, address)
VALUES 
('C123', 'Alice', STRUCT('123 Main St', 'New York', '10001'));

Querying Nested Data

SELECT customer_id, name, address.city FROM my_dataset.customers;

Example 2: Repeated RECORD (Array of STRUCTs)

Now, let's say a customer can have multiple orders.

Schema Definition

Column NameData Type
customer_idSTRING
nameSTRING
ordersARRAY of RECORD (STRUCT)
├── order_idSTRING
├── productSTRING
├── amountFLOAT64

Creating the Table

CREATE TABLE my_dataset.customers_orders (
  customer_id STRING,
  name STRING,
  orders ARRAY<STRUCT<
    order_id STRING,
    product STRING,
    amount FLOAT64
  >>
);

Inserting Data

INSERT INTO my_dataset.customers_orders (customer_id, name, orders)
VALUES 
('C123', 'Alice', 
  [STRUCT('O001', 'Laptop', 1200.50), STRUCT('O002', 'Mouse', 25.00)]
);

Querying Repeated Fields (Flattening the Array)

SELECT customer_id, name, order.order_id, order.product, order.amount
FROM my_dataset.customers_orders, UNNEST(orders) AS order;
  • UNNEST(orders) is used to flatten the array and return individual rows for each order.

When to Use RECORD Type?

  • When dealing with hierarchical or structured data.
  • To reduce table joins and improve query efficiency.
  • When working with JSON-like or denormalized datasets.

 

Tags Nested Data RECORD Facebook X
Comments 0
  1. Google OAuth Token Expiration: Causes and Solutions
    28
  2. Step-by-Step Guide to Using Google AI Gemini API with CURL
    3,208
  3. Creating a Service Account via Google Cloud Console (UI)
    1,067
  4. Improving nested_fields data access experience with a persistent user-defined function (aka UDF) in Google Big Query
    1,504
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    55
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    49
  3. How do I create animated background for Google Meet?
    22,830
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    271
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    103
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    168
  7. Array of arguments passed to script in PHP
    97
  8. Invalid Auction Storms in Ad Tech
    106
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    207
  10. The Two Clocks of Ad Server Architecture
    77
  11. Frequency Cap Architecture Overview
    526
  12. Collecting Proof-of-Play from 100,000 Screens
    152
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    302
  14. What is RedisVL?
    114
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    246
  16. TPU & GPU Cluster Management Tools
    257
  17. The Concept of Scaffolding in AI Agents
    244
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    596
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    267
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    353
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    206
  22. Machine Learning: Training vs. Ranking
    229
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    228
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    202
  25. Designing a Data Curation System Around a Vector Database
    230
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    263
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    671
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    200
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    232
  30. Understanding Data Lineage
    271