I'm creating a query to calculate median visits from two different websites by day.
The output should look like the following:
+------------+---------+---------------+
| date | website | median_visits |
+------------+---------+---------------+
| 2019-04-01 | A | median_value |
| 2019-04-01 | B | median_value |
| 2019-04-02 | A | median_value |
| 2019-04-02 | B | median_value |
| 2019-04-03 | A | median_value |
| 2019-04-03 | B | median_value |
+------------+---------+---------------+
Here is what my table (there are 20,000 rows) looks like:
+------------+---------+--------+
| date | website | visits |
+------------+---------+--------+
| 2019-04-01 | A | 10.0 |
| 2019-04-01 | B | 14.0 |
| 2019-04-02 | A | 85.0 |
| 2019-04-03 | A | 75.0 |
| 2019-04-02 | B | 3.0 |
| 2019-04-02 | B | 45.0 |
| 2019-04-01 | A | 12.0 |
| 2019-04-03 | A | 44.0 |
| 2019-04-01 | A | 99.0 |
+------------+---------+--------+
What would be the most efficient way to query for the desired output? I am currently using:
SELECT DISTINCT date, website, median_visits
FROM
(SELECT date, website, PERCENTILE_CONT(visits, 0.5)
OVER(PARTITION BY date, website) AS median_visits
FROM table)
Below is for BigQuery Standard SQL - I cannot claim it is the best. I cannot even guarantee that it is better - but based on my testing I see better execution plan and slots usage. So, you can try and see with your data
#standardSQL
SELECT date, website,
(SELECT PERCENTILE_CONT(visit, 0.5) OVER()
FROM UNNEST(visits) visit LIMIT 1
) AS median_visits
FROM (
SELECT date, website, ARRAY_AGG(visits) visits
FROM `project.dataset.table`
GROUP BY date, website
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.