SELECT
Название функции (столбец для вычислений)
OVER (
PARTITION BY столбец для группировки
ORDER BY столбец для сортировки
ROWS или RANGE выражение для ограничения строк в пределах группы
)
select date, source,conversion,
sum(conversion) over() as Sum
from `united-strategy-279207.Example.conversion`
select date, source,conversion,
sum(conversion) over(PARTITION BY date) as Sum
from `united-strategy-279207.Example.conversion`
select date, source,conversion,
sum(conversion) over(PARTITION BY date ORDER BY source) as Sum
from `united-strategy-279207.Example.conversion`
select date, source,conversion,
SUM (conversion) OVER(PARTITION BY date ORDER BY source) as Sum,
COUNT(conversion) OVER(PARTITION BY date) AS Count,
AVG(conversion) OVER(PARTITION BY date) AS Avg,
MAX(conversion) OVER(PARTITION BY date) AS Max,
MIN(conversion) OVER(PARTITION BY date) AS Min
from `united-strategy-279207.Example.conversion`
select date, source,conversion,
CUME_DIST() OVER(PARTITION BY date ORDER BY conversion) AS cume_dist,
PERCENT_RANK() OVER(PARTITION BY date ORDER BY conversion) AS percent_rank,
PERCENTILE_CONT(conversion, 0.5) OVER (PARTITION BY date) as percentile_cont,
PERCENTILE_DISC(conversion, 0.5) OVER(PARTITION BY date) AS percentile_disc
from `united-strategy-279207.Example.conversion`
select date, source,conversion,
ROW_NUMBER() OVER(PARTITION BY date ORDER BY conversion) AS row_number,
RANK() OVER(PARTITION BY date ORDER BY conversion) AS rank,
DENSE_RANK() OVER(PARTITION BY date ORDER BY conversion) AS dense_rank,
NTILE(4) OVER(PARTITION BY date ORDER BY conversion) AS ntile
from `united-strategy-279207.Example.conversion`
select date, source,conversion,
LAG(conversion) OVER(PARTITION BY date ORDER BY date) AS lag,
LEAD(conversion) OVER(PARTITION BY date ORDER BY date) AS lead,
FIRST_VALUE(conversion) OVER(PARTITION BY date ORDER BY date) AS first_value,
LAST_VALUE(conversion) OVER(PARTITION BY date ORDER BY date) AS last_value
from `united-strategy-279207.Example.conversion`
select date,cid,source_medium,
FIRST_VALUE(source_medium) OVER(PARTITION BY cid ORDER BY date ASC) AS first_click,
sessions,conversions
from `united-strategy-279207.Example.conversions`
with first as
(
select date,cid,source_medium,
FIRST_VALUE(source_medium) OVER(PARTITION BY cid ORDER BY date ASC) AS first_click,
sessions,conversions
from `united-strategy-279207.Example.conversions`
)
select first_click, sum (conversions) as conversions
from first
group by first_click
select date,cid,source_medium,
-- Присваиваем ранг каждой строчке в партиции
DENSE_RANK() OVER(PARTITION BY cid ORDER BY date ASC) AS rank,
sessions,conversions
from `united-strategy-279207.Example.conversions`
select date,cid,source_medium,
-- Делим ранг определенной строки на сумму рангов по пользователю
ROUND(CAST(DENSE_RANK() OVER(PARTITION BY cid ORDER BY date) AS float64) / CAST(SUM(rank) OVER(PARTITION BY cid) AS float64), 2) AS distribution,
sessions,conversions
from (
select date,cid,source_medium,
-- Присваиваем ранг каждой строчке в партиции
DENSE_RANK() OVER(PARTITION BY cid ORDER BY date) AS rank,
sessions,conversions
from `united-strategy-279207.Example.conversions`)
with rank as (
select date,cid,source_medium,
-- Делим ранг определенной строки на сумму рангов по пользователю
ROUND(CAST(DENSE_RANK() OVER(PARTITION BY cid ORDER BY date) AS float64) / CAST(SUM(rank)
OVER(PARTITION BY cid) AS float64), 2) AS distribution,
sessions,conversions
from (
select date,cid,source_medium,
-- Присваиваем ранг каждой строчке в партиции
DENSE_RANK() OVER(PARTITION BY cid ORDER BY date) AS rank,
sessions,conversions
from `united-strategy-279207.Example.conversions`
) table_rank
)
select source_medium, sum (distribution) as value, sum(conversions) as conversions
from rank
group by source_medium
order by value desc