Introduction to Databricks SQL
Kevin Barlow
Data Manager
SELECT store, totalRev, product
FROM (
SELECT count(*) as count,
sum(revenue) as totalRev,
min(price) as minPrice,
max(units) as maxUnits,
region,
store,
product
FROM sales
GROUP BY region, store, product
) q
SELECT name,
dept,
RANK() OVER
(PARTITION BY dept
ORDER BY salary) AS rank
FROM employees;
name | dept | salary | rank |
---|---|---|---|
Lisa | Sales | 10000 | 1 |
Alex | Sales | 30000 | 2 |
Fred | Engineering | 21000 | 1 |
Tom | Engineering | 23000 | 2 |
SELECT a,
b,
RANK() OVER(PARTITION BY a ORDER BY b DESC),
FROM table_name;
a | b | rank |
---|---|---|
A1 | 3 | 1 |
A1 | 1 | 3 |
A1 | 2 | 2 |
A2 | 1 | 1 |
LAG()
) or subsequent (LEAD()
) value from rowSELECT a,
b,
LAG(b) OVER (PARTITION BY a ORDER BY b)
FROM table_name;
a | b | lag |
---|---|---|
A1 | 3 | NULL |
A1 | 1 | 3 |
A1 | 2 | 1 |
A2 | 1 | NULL |
Introduction to Databricks SQL