Advertisement
Viktor_Kucherov

test

Dec 9th, 2022 (edited)
645
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SELECT *
  2. -- исправил на LEAD, т.к. необходимо посчитать процент следующего месяца по сравнению с предыдущим
  3. ,ROUND((LEAD(tips_sum)  OVER (PARTITION BY taxi_id ORDER BY  year_month ) - tips_sum)/tips_sum*100., 2)  AS tips_change
  4. -- изменил расчет процента
  5. from(
  6. SELECT
  7.  
  8.   taxi_id,
  9.   CAST(TIMESTAMP_TRUNC(trip_start_timestamp, MONTH) AS DATE) AS year_month,
  10.   sum(tips) AS tips_sum,
  11.  
  12. FROM
  13.   bigquery-public-data.chicago_taxi_trips.taxi_trips
  14. WHERE CAST(TIMESTAMP_TRUNC(trip_start_timestamp, MONTH) AS DATE) > '2022-04-01'
  15.     AND taxi_id IN (
  16.                 SELECT taxi_id FROM (
  17.                     SELECT
  18.                       taxi_id,
  19.                       CAST(TIMESTAMP_TRUNC(trip_start_timestamp, MONTH) AS DATE) AS month,
  20.                       sum(tips) AS count_tips
  21.                     FROM
  22.                       bigquery-public-data.chicago_taxi_trips.taxi_trips
  23.                     WHERE CAST(TIMESTAMP_TRUNC(trip_start_timestamp, MONTH) AS DATE) = '2022-04-01'
  24.                     group by 1, 2
  25.                     ORDER BY 3 DESC -- вставил сортировку, совсем про нее забыл
  26.  
  27.                     LIMIT 3))
  28. GROUP BY 1, 2
  29. ORDER BY 1, 2
  30. )
  31.  
  32.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement