Tellius
  • 🚩Getting Started
    • 👋Say Hello to Tellius
      • Glossary
      • Tellius 101
      • Navigating around Tellius
      • Guided tours for quick onboarding
    • ⚡Quick Start Guides
      • Search
      • Vizpads (Explore)
      • Insights (Discover)
    • ✅Best Practices
      • Search
      • Vizpads (Explore)
      • Insights (Discover)
      • Predict
      • Data
    • ⬇️Initial Setup
      • Tellius architecture
      • System requirements
      • Installation steps for Tellius
      • Customizing Tellius
    • Universal Search
    • 🏠Tellius Home Page
  • Kaiya
    • ♟️Understanding AI Agents & Agentic Flows
      • Glossary
      • Composer
      • 🗝️Triggering an agentic workflow
      • The art of possible
      • Setting up LLM for Kaiya
    • 🤹Kaiya conversational AI
      • ❓FAQs on Kaiya Conversations
      • Triggering Insights with "Why" questions
      • Mastering Kaiya conversational AI
  • 🔍Search
    • 👋Get familiar with our Search interface
    • 🤔Understanding Tellius Search
    • 📍Search Guide
    • 🚀Executing a search query
      • Selecting a Business View
      • Typing a search query
      • Constructing effective search queries
      • Marketshare queries
    • 🔑Analyzing search results
      • Understanding search results
      • Search Inspector
      • Time taken to execute a query
      • Interacting with the resulting chart
    • 📊Know your charts in Tellius
      • Understanding Tellius charts
      • Variations of a chart type
      • Building charts from Configuration pane
      • List of chart-specific fields
      • Adding columns to fields in Configuration pane
      • Absolute and percentage change aggregations
      • Requirements of charts
      • Switching to another chart
      • Formatting charts
      • Advanced Analytics
      • Cumulative line chart
    • 🧑‍🏫Help Tellius learn
    • 🕵️‍♂️Search history
    • 🎙️Voice-driven search
    • 🔴Live Query mode
  • 📈Vizpads (Explore)
    • 🙋Meet Vizpads!
    • 👋Get familiar with our Vizpads
    • #️⃣Measures, dimensions, date columns
    • ✨Creating Vizpads
    • 🌐Applying global filters
      • Filters in multi-BV Vizpads
      • Filters using common columns
    • 📌Applying local filters
    • 📅Date picker in filters
      • Customizing the calendar view
    • ✅Control filters
      • Multi-select list
      • Single-select list
      • Range slider
      • Dropdown list
    • 👁️Actions in View mode
      • Interacting with the charts
    • 📝Actions in Edit mode
      • 🗨️Viz-level actions
    • 🔧Anomaly management for line charts
      • Instance level
      • Vizpad level
      • Chart level
    • ⏳Time taken to load a chart
      • Instance level
      • Vizpad level
      • Chart level
    • ♟️Working with sample datasets
    • 🔁Swapping Business View of charts
      • Swapping only the current Vizpad
      • Swapping multiple objects
      • Configuring the time of swap
    • 🤖Explainable AI charts
  • 💡Insights (Discover)
    • 👋Get familiar with our Insights
    • ❓Understanding the types of Insights
    • 🕵️‍♂️Discovery Insights
    • ➕How to create new Insights
      • 🔛Creating Discovery Insight
      • 🔑Creating Key Driver Insights
      • 〰️Creating Trend Insights
      • 👯Creating Comparison Insights
    • 🧮The art of selecting columns for Insights
      • ➡️How to include/exclude columns?
  • 🔢Data
    • 👋Get familiar with our Data module
    • 🥂Connect
    • 🪹Create new datasource
      • Connecting to Oracle database
      • Connecting to MySQL database
      • Connecting to MS SQL database
      • Connecting to Postgres SQL database
      • Connecting to Teradata
      • Connecting to Redshift
      • Connecting to Hive
      • Connecting to Azure Blob Storage
      • Connecting to Spark SQL
      • Connecting to generic JDBC
      • Connecting to Salesforce
      • Connecting to Google cloud SQL
        • Connecting to a PostgreSQL cloud SQL instance
        • Connecting to an MSSQL cloud SQL instance
        • Connecting to a MySQL Cloud SQL Instance
      • Connecting to Amazon S3
      • Connecting to Google BigQuery
        • Steps to connect to a Google BigQuery database
      • Connecting to Snowflake
        • OAuth support for Snowflake
        • Integrating Snowflake with Azure AD via OAuth
        • Integrating Snowflake with Okta via OAuth
        • Azure PrivateLink
        • AWS PrivateLink
        • Best practices
      • Connecting to Databricks
      • Connecting to Databricks Delta Lake
      • Connecting to an AlloyDB Cluster
      • Connecting to HDFS
      • Connecting to Looker SQL Interface
      • Loading Excel sheets
      • 🚧Understanding partitioning your data
    • ⏳Time-to-Live (TTL) and Caching
    • 🌷Refreshing a datasource
    • 🪺Managing your datasets
      • Swapping datasources
    • 🐣Preparing your datasets
      • 🤾Actions that can be done on a dataset
      • Data Pipeline
      • SQL code snippets
      • ✍️Writeback window
      • 🧩Editing Prepare → Data
      • Handling null or mismatched values
      • Metadata view
      • List of icons and their actions
        • Functions
        • SQL Transform
        • Python Transform
        • Standard Aggregation
        • Creating Hierarchies
      • Dataset Scripting
      • Fusioning your datasets
      • Scheduling refresh for datasets
    • 🐥Preparing your Business Views
      • 🌟Create a new Business View
      • Creating calculated columns
      • Creating dynamic parameters
      • Scheduling refresh for Business Views
      • Setting up custom calendars
    • Tellius Engine: Comparison of In-Memory vs. Live Mode
  • Feed
    • 📩What is a Feed in Tellius?
    • ❗Alerts on the detection of anomalies
    • 📥Viewing and deleting metrics
    • 🖲️Track a new metric
  • Assistant
    • 💁Introducing Tellius Assistant
    • 🎤Voice-based Assistant
    • 💬Interacting with Assistant
    • ↖️Selecting Business View
  • Embedding Tellius
    • What you should know before embedding
    • Embedding URL
      • 📊Embedding Vizpads
        • Apply and delete filters
        • Vizpad-related actionTypes
        • Edit, save, and share a Vizpad
        • Keep, remove, drill sections
        • Adding a Viz to a Vizpad
        • Row-level policy filters
      • 💡Embedding Insights
        • Creating and Viewing Insights
      • 🔎Embedding Search
        • Search query execution
      • Embedding Assistant
      • 🪄Embedding Kaiya
      • Embedding Feed
  • API
    • Insights APIs
    • Search APIs
    • Authentication API (Login API)
  • ✨What's New
    • Release 5.4
      • Patch 5.4.0.x
    • Release 5.3
      • Patch 5.3.1
      • Patch 5.3.2
      • Patch 5.3.3
    • Release 5.2
      • Patch 5.2.1
      • Patch 5.2.2
    • Release 5.1
      • Patch 5.1.1
      • Patch 5.1.2
      • Patch 5.1.3
    • Release 5.0
      • Patch 5.0.1
      • Patch 5.0.2
      • Patch 5.0.3
      • Patch 5.0.4
      • Patch 5.0.5
    • Release 4.3 (Fall 2023)
      • Patch 4.3.1
      • Patch 4.3.2
      • Patch 4.3.3
      • Patch 4.3.4
    • Release 4.2
      • Patch 4.2.1
      • Patch 4.2.2
      • Patch 4.2.3
      • Patch 4.2.4
      • Patch 4.2.5
      • Patch 4.2.6
      • Patch 4.2.7
    • Release 4.1
      • Patch 4.1.1
      • Patch 4.1.2
      • Patch 4.1.3
      • Patch 4.1.4
      • Patch 4.1.5
    • Release 4.0
Powered by GitBook

© 2025 Tellius

On this page
  • 1. Converting a time column to UTC
  • 2. Calculating Lag/Lead
  • 3. Creating rolling averages
  • 4. Creating bins/buckets with NTILE
  • 5. Creating an average in the same table
  • 6. Using CASE statements
  • 7. Handling NULLs with COALESCE
  • 8. Date/Time truncation
  • 9. Calculating time differences

Was this helpful?

Export as PDF
  1. Data
  2. Preparing your datasets

SQL code snippets

Quickly transform columns, create rolling averages, build bins, or run more complex window functions

PreviousData PipelineNextWriteback window

Last updated 4 months ago

Was this helpful?

When working with Tellius, you may need to perform data transformations beyond simple aggregations or filters. SparkSQL (the SQL engine used by Tellius) offers a host of built-in functions and advanced window operations—like converting time zones, lag/lead comparisons, rolling averages, and more. Below, we highlight several commonly needed transformations, their use cases, and sample SQL queries.

You can adapt them to your own datasets in the Tellius SQL editor (check out page for more details on how to access and apply these transformations in your workflow).

1. Converting a time column to UTC

  • Scenario: You have a timestamp column in a specific time zone and want to standardize it to UTC for consistent analysis.

SELECT 
    *,
    to_utc_timestamp(origin_timestamp, origin_timezone) AS time_utc
FROM table_name
  • Function: to_utc_timestamp()

  • Explanation: Converts origin_timestamp, which is in origin_timezone, to UTC.

  • Example: If origin_timezone is 'America/Los_Angeles', then time_utc becomes the corresponding UTC timestamp.

2. Calculating Lag/Lead

Scenario: Compare current row values to a previous (lag) or next (lead) row. Useful for identifying changes or trends (e.g., comparing week-over-week or month-over-month sales).

Using LAG

SELECT 
    *,
    LAG(Sales) OVER (PARTITION BY Order_Id ORDER BY Date) AS Prev_Sales
FROM table_name
  • Function: LAG(column) OVER(...)

  • Explanation: Retrieves the prior Sales value for the same Order_Id, ordered by Date.

  • Example: Compare this week’s Sales to the previous week’s (Prev_Sales) to see the difference.

Using LEAD

SELECT 
    *,
    LEAD(Sales) OVER (PARTITION BY Order_Id ORDER BY Date) AS Next_Sales
FROM table_name
  • Function: LEAD(column) OVER(...)

  • Explanation: Retrieves the next Sales value for the same Order_Id, ordered by Date.

  • Example: Forecast or compare today’s Sales to a post-period’s Sales.

3. Creating rolling averages

Scenario: Track moving averages (e.g., a 2-week rolling average of Sales), excluding the current row to gauge past performance.

SELECT 
    *,
    AVG(Sales) OVER (
        PARTITION BY Order_Id
        ORDER BY Date
        ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING
    ) AS Two_Weeks_Rolling_Average
FROM table_name
  • Function: AVG(column) OVER(...) with a window frame definition.

  • Explanation: Looks at the two preceding rows (excluding current row) to compute an average.

  • Note: Adjust the frame (e.g., ROWS BETWEEN 3 PRECEDING AND 1 PRECEDING) for a 3-day/week/month rolling window.

4. Creating bins/buckets with NTILE

Scenario: Segment numeric values (e.g., net_sales) into a fixed number of buckets (like quartiles or deciles) for each product category.

SELECT 
    product_category_name,
    month,
    net_sales,
    NTILE(4) OVER (
        PARTITION BY product_category_name
        ORDER BY net_sales DESC
    ) AS net_sales_group
FROM table_name
  • Function: NTILE(n) OVER(...)

  • Explanation: Divides the ordered partition into n buckets. Here, 4 bins give you quartiles.

  • Example: Quickly categorize product categories into 1 (highest net sales) through 4 (lowest net sales).

5. Creating an average in the same table

Scenario: You want to compare each row’s Sales to the state-wide average.

SELECT
    a.*,
    b.State_Sales_Average
FROM table_name a
LEFT JOIN (
    SELECT 
        State, 
        AVG(Sales) AS State_Sales_Average
    FROM table_name 
    GROUP BY State
) b
ON a.State = b.State
  • Explanation: Sub-query or common table expression (CTE) for computing aggregated values, then join back to the main table. Each row in a now has a new column State_Sales_Average for contextual comparison.

6. Using CASE statements

Scenario: Conditionally update column values based on custom logic.

SELECT 
    Employee_Name,
    CASE 
        WHEN StateCode = 'AR' THEN 'FL'
        WHEN StateCode = 'GE' THEN 'AL'
        ELSE StateCode 
    END AS StateCode
FROM table_name
  • Function: CASE WHEN ... THEN ... ELSE ... END

  • Explanation: If StateCode is 'AR', set it to 'FL'. If 'GE', set it to 'AL'. Otherwise, keep existing value.

7. Handling NULLs with COALESCE

SELECT 
    COALESCE(Sales, 0) AS Sales_No_Nulls,
    COALESCE(Comments, 'N/A') AS Comments_Filled
FROM table_name
  • Function: COALESCE(column, default_value)

  • Explanation: Replaces NULL with a default (numeric or string) value.

8. Date/Time truncation

SELECT 
    date_trunc('month', timestamp_col) AS month_start,
    COUNT(*) AS num_events
FROM table_name
GROUP BY date_trunc('month', timestamp_col)
  • Function: date_trunc('unit', column)

  • Explanation: Rounds down the timestamp to a specified boundary (hour, day, week, month, quarter, etc.).

9. Calculating time differences

SELECT
    start_time,
    end_time,
    datediff(end_time, start_time) AS diff_in_days
FROM table_name
  • Function: datediff(end, start)

  • Explanation: Returns the difference in days between two date/timestamp columns.

These SQL snippets illustrate some of the powerful transformation capabilities available in SparkSQL—particularly beneficial for preparing and enriching your data in Tellius before deeper analysis. By combining basic functions (e.g., CASE, COALESCE) with advanced window operations (LAG, LEAD, NTILE, OVER clauses), you can build sophisticated data transformations directly in the Tellius SQL editor.

🔢
🐣
this