cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

How much USD are you spending on Databricks?

Hubert-Dudek
Esteemed Contributor III

Join two system tables and get exactly how much USD you are spending.

The short version of the query:

 

SELECT 
    u.usage_date,
    u.sku_name,
    SUM(u.usage_quantity * p.pricing.default) AS total_spent,
    p.currency_code
FROM 
    system.billing.usage u
LEFT JOIN 
    system.billing.list_prices p
ON 
    u.sku_name = p.sku_name
    AND u.cloud = p.cloud
    AND u.usage_start_time < COALESCE(p.price_end_time, date'2029-12-31')
    AND u.usage_end_time > p.price_start_time
GROUP BY 
    ALL
ORDER BY 
    ALL;

 

Extended version (which is handling potential price overlap):

 

SELECT 
    u.usage_date,
    u.sku_name,
    SUM(
        (UNIX_TIMESTAMP(
            LEAST(u.usage_end_time, COALESCE(p.price_end_time, date'2029-12-31'))
        ) -
        UNIX_TIMESTAMP(
            GREATEST(u.usage_start_time, p.price_start_time)
        ))
        / (UNIX_TIMESTAMP(u.usage_end_time) - UNIX_TIMESTAMP(u.usage_start_time))
        * u.usage_quantity * p.pricing.default
    ) AS total_spent,
    p.currency_code
FROM 
    system.billing.usage u
LEFT JOIN 
    system.billing.list_prices p
ON 
    u.sku_name = p.sku_name
    AND u.cloud = p.cloud
    AND u.usage_start_time < COALESCE(p.price_end_time, date'2029-12-31')
    AND u.usage_end_time > p.price_start_time
GROUP BY 
    ALL
ORDER BY 
    ALL;

 

system_pig.png

1 REPLY 1

jose_gonzalez
Databricks Employee
Databricks Employee

Thank you for sharing this information @Hubert-Dudek 

Join Us as a Local Community Builder!

Passionate about hosting events and connecting people? Help us grow a vibrant local community—sign up today to get started!

Sign Up Now