You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
%%sql
SELECT seller_state,
number_of_sellers,
RANK() OVER(ORDER BY number_of_sellers DESC)
FROM (SELECT seller_state,
COUNT(seller_id) AS number_of_sellers
FROM sellers
GROUP BY seller_state) AS nos
LIMIT10
%%sql
WITH ship_interval AS(SELECT seller_id,
order_id,
EXTRACT(EPOCH FROM (order_delivered_carrier - order_purchase)::interval)/3600AS delivery_interval,
EXTRACT(EPOCH FROM (shipping_limit_date - order_purchase)::interval)/3600AS shipping_limit_interval
FROM(SELECT order_id,
seller_id,
order_purchase,
order_delivered_carrier,
shipping_limit_date
FROM order_items
JOIN orders USING(order_id)
JOIN sellers USING(seller_id)
WHERE order_status IN('delivered')) AS ship_time)
SELECT seller_id,
AVG(delivery_interval)::realAS avg_del_time,
AVG(shipping_limit_interval)::realAS ship_limit_time
FROM ship_interval
GROUP BY seller_id
ORDER BY avg_del_time
LIMIT5
%%sql
WITH seller_category AS(SELECT seller_id,
COUNT(product_category) AS num_of_cat
FROM sellers
JOIN order_items USING(seller_id)
JOIN products USING(product_id)
GROUP BY seller_id)
SELECTAVG(num_of_cat) AS avg_categories,
percentile_cont(.5)
WITHIN GROUP (ORDER BY num_of_cat) AS median_categories
FROM seller_category
LIMIT10
%%sql
SELECT seller_id,
avg_order_rev,
RANK() OVER(ORDER BY avg_order_rev DESC) AS rank
FROM(WITH order_unit AS(SELECT seller_id,
price,
COUNT(order_id) AS total_orders,
SUM(order_item_id) AS total_units
FROM sellers
JOIN order_items USING(seller_id)
GROUP BY seller_id, price),
seller_unique AS(SELECT seller_id,
(price*total_units)::realAS revenue,
total_orders
FROM order_unit)
SELECT seller_id,
(revenue/total_orders)::realAS avg_order_rev
FROM seller_unique) AS seller_avg
LIMIT10
%%sql
SELECT*FROM(WITH seller_market AS(SELECT seller_id,
customer_state,
SUM(revenue) AS state_revenue
FROM(SELECT seller_id,
(price * order_item_id)::realAS revenue,
customer_state
FROM sellers
JOIN order_items USING(seller_id)
JOIN orders USING(order_id)
JOIN customers USING(customer_id)) AS revenue_per_state
GROUP BY seller_id, customer_state)
SELECT seller_id,
customer_state,
state_revenue,
RANK() OVER(PARTITION BY seller_id ORDER BY state_revenue DESC)
FROM seller_market
WHERE seller_id IN(SELECT seller_id
FROM(SELECT seller_id,
SUM(revenue) AS total_rev
FROM(SELECT seller_id,
(order_item_id * price)::realAS revenue
FROM sellers
JOIN order_items USING(seller_id)) AS rev
GROUP BY seller_id
ORDER BY total_rev DESC) AS seller_rev
LIMIT5)) AS rank_market_revenue
WHERE rank <4