SELECT
f.mch_id,
COUNT(DISTINCT f.cust_uid) AS fav_user_count,
COUNT(DISTINCT CASE
WHEN p.cust_uid IS NOT NULL AND p.pchs_tm > f.fav_tm
THEN f.cust_uid
ELSE NULL
END) AS conv_user_count,
ROUND(
CASE
WHEN COUNT(DISTINCT f.cust_uid) = 0 THEN 0
ELSE (COUNT(DISTINCT CASE
WHEN p.cust_uid IS NOT NULL AND p.pchs_tm > f.fav_tm
THEN f.cust_uid
ELSE NULL
END) / COUNT(DISTINCT f.cust_uid)) * 100
END,
2
) AS conversion_rate
FROM xhs_fav_rcd f
LEFT JOIN xhs_pchs_rcd p
ON f.cust_uid = p.cust_uid
AND f.mch_id = p.mch_id
GROUP BY f.mch_id
ORDER BY conversion_rate DESC;
WITH favorite_users AS (
SELECT mch_id, cust_uid
FROM xhs_fav_rcd
GROUP BY mch_id, cust_uid
),
favorite_counts AS (
SELECT mch_id, COUNT(DISTINCT cust_uid) AS fav_user_count
FROM favorite_users
GROUP BY mch_id
),
conversion_counts AS (
SELECT f.mch_id, COUNT(DISTINCT f.cust_uid) AS conv_user_count
FROM favorite_users f
JOIN xhs_fav_rcd fv ON f.mch_id = fv.mch_id AND f.cust_uid = fv.cust_uid
JOIN xhs_pchs_rcd p ON f.mch_id = p.mch_id AND f.cust_uid = p.cust_uid
WHERE p.pchs_tm > fv.fav_tm
GROUP BY f.mch_id
)
SELECT
fc.mch_id,
fc.fav_user_count,
COALESCE(cc.conv_user_count, 0) AS conv_user_count,
COALESCE(ROUND((cast(cc.conv_user_count as float) / fc.fav_user_count) * 100, 2), 0) AS conversion_rate
FROM favorite_counts fc
LEFT JOIN conversion_counts cc ON fc.mch_id = cc.mch_id
ORDER BY conversion_rate DESC;
WITH all_routes AS (
SELECT r.start_loc, r.end_loc, l_start.loc_ctg AS start_ctg, l_end.loc_ctg AS end_ctg, COUNT(*) AS trip_count
FROM didi_sht_rcd r
JOIN loc_nm_ctg l_start ON r.start_loc = l_start.loc_nm
JOIN loc_nm_ctg l_end ON r.end_loc = l_end.loc_nm
GROUP BY r.start_loc, r.end_loc, l_start.loc_ctg, l_end.loc_ctg
),
ranked_routes AS (
SELECT start_loc, end_loc, start_ctg, end_ctg, trip_count,
dense_rank() OVER (PARTITION BY start_ctg, end_ctg ORDER BY trip_count DESC) AS route_rank
FROM all_routes
)
SELECT start_loc, end_loc, start_ctg, end_ctg, trip_count
FROM ranked_routes
WHERE route_rank = 1
ORDER BY trip_count DESC;
WITH hotel_routes AS (
SELECT r.start_loc, r.end_loc, l_start.loc_ctg AS start_ctg, l_end.loc_ctg AS end_ctg, COUNT(*) AS trip_count
FROM didi_sht_rcd r
JOIN loc_nm_ctg l_start ON r.start_loc = l_start.loc_nm
JOIN loc_nm_ctg l_end ON r.end_loc = l_end.loc_nm
WHERE l_start.loc_ctg = '酒店' OR l_end.loc_ctg = '酒店'
GROUP BY r.start_loc, r.end_loc, l_start.loc_ctg, l_end.loc_ctg
),
ranked_routes AS (
SELECT start_loc, end_loc, start_ctg, end_ctg, trip_count,
ROW_NUMBER() OVER (PARTITION BY start_ctg, end_ctg ORDER BY trip_count DESC) AS route_rank
FROM hotel_routes
)
SELECT start_loc, end_loc, start_ctg, end_ctg, trip_count
FROM ranked_routes
WHERE route_rank = 1
ORDER BY trip_count DESC;
WITH hotel_routes AS (
SELECT r.start_loc, r.end_loc, l_end.loc_ctg, COUNT(*) AS trip_count
FROM didi_sht_rcd r
JOIN loc_nm_ctg l_start ON r.start_loc = l_start.loc_nm
JOIN loc_nm_ctg l_end ON r.end_loc = l_end.loc_nm
WHERE l_start.loc_ctg = '酒店'
GROUP BY r.start_loc, r.end_loc, l_end.loc_ctg
),
ranked_routes AS (
SELECT start_loc, end_loc, loc_ctg, trip_count,
ROW_NUMBER() OVER (PARTITION BY loc_ctg ORDER BY trip_count DESC) AS route_rank
FROM hotel_routes
)
SELECT start_loc, end_loc, loc_ctg, trip_count
FROM ranked_routes
WHERE route_rank = 1
ORDER BY trip_count DESC;
WITH hotel_to_cafe AS (
SELECT r.start_loc, r.end_loc, COUNT(*) AS trip_count
FROM didi_sht_rcd r
JOIN loc_nm_ctg l_start ON r.start_loc = l_start.loc_nm
JOIN loc_nm_ctg l_end ON r.end_loc = l_end.loc_nm
WHERE l_start.loc_ctg = '酒店' AND l_end.loc_ctg = '餐饮'
GROUP BY r.start_loc, r.end_loc
)
SELECT start_loc, end_loc, trip_count
FROM hotel_to_cafe
ORDER BY trip_count DESC
LIMIT 1;
SELECT r.*
FROM didi_sht_rcd r
JOIN loc_nm_ctg l_start ON r.start_loc = l_start.loc_nm
JOIN loc_nm_ctg l_end ON r.end_loc = l_end.loc_nm
WHERE l_start.loc_ctg = '住宅'
AND l_end.loc_ctg = '写字楼'
ORDER BY r.start_tm ASC;
SELECT r.*
FROM didi_sht_rcd r
JOIN loc_nm_ctg l_start ON r.start_loc = l_start.loc_nm
JOIN loc_nm_ctg l_end ON r.end_loc = l_end.loc_nm
WHERE l_start.loc_ctg = '餐饮'
AND l_end.loc_ctg = '餐饮'
ORDER BY r.start_tm ASC;
select
cust_uid,
start_loc,
end_loc,
start_tm,
car_cls
from
didi_sht_rcd a
join loc_nm_ctg b on a.end_loc = b.loc_nm
where
b.loc_ctg = '餐饮'
order by
start_tm asc
SELECT
gd.gd_id,
gd.gd_nm,
gd.gd_typ
FROM
xhs_fav_rcd fav
JOIN
gd_inf gd ON fav.mch_id = gd.gd_id
LEFT JOIN
xhs_pchs_rcd pchs ON gd.gd_id = pchs.mch_id
WHERE
pchs.mch_id IS NULL
group by
gd.gd_id,
gd.gd_nm,
gd.gd_typ
SELECT
gd.gd_id,
gd.gd_nm,
gd.gd_typ
FROM
xhs_fav_rcd fav
JOIN
gd_inf gd ON fav.mch_id = gd.gd_id
LEFT JOIN
xhs_pchs_rcd pchs ON gd.gd_id = pchs.mch_id
WHERE
pchs.mch_id IS NULL
SELECT
gd.gd_typ,
COUNT(DISTINCT pchs.cust_uid) AS buyer_count
FROM
xhs_pchs_rcd pchs
JOIN
gd_inf gd ON pchs.mch_id = gd.gd_id
GROUP BY
gd.gd_typ
ORDER BY
buyer_count DESC
LIMIT 1;
select
t1.gd_id,
t1.gd_nm,
count(t2.fav_trq) as fav_count
from
gd_inf t1
join
xhs_fav_rcd t2 on t1.gd_id = t2.mch_id
group by
1,2
order by
fav_count desc
limit
1;
SELECT
gd.gd_id,
gd.gd_nm,
COUNT(*) AS fav_count
FROM
xhs_fav_rcd fav
JOIN
gd_inf gd ON fav.mch_id = gd.gd_id
GROUP BY
gd.gd_id, gd.gd_nm
ORDER BY
fav_count DESC
LIMIT 1;
select
a.live_id,
b.live_nm,
count(1) as enter_cnt
from ks_live_t1 a
join ks_live_t2 b using(live_id)
where date_format(a.enter_time, '%Y-%m-%d %H') = '2021-09-12 23'
group by 1,2
order by enter_cnt desc
limit 5;
select
a.live_id,
b.live_nm,
count(a.enter_time) as enter_cnt
from ks_live_t1 a
join ks_live_t2 b using(live_id)
where date_format(a.enter_time, '%Y-%m-%d %H') = '2021-09-12 23'
group by 1,2
order by enter_cnt desc
limit 5;
select
a.live_id,
b.live_nm,
count(a.enter_time) as enter_cnt
from ks_live_t1 a
join ks_live_t2 b using(live_id)
where date_format(a.enter_time, '%Y-%m-%d %H')
group by 1,2
order by enter_cnt desc
limit 5;
select
date(start_time) as dt,
start_loc,
end_loc,
count(start_time) as cnt,
count(distinct user_id) as usr_cnt,
round(count(start_time) / count(distinct user_id) , 4) as cnt_per_usr
from
hello_bike_riding_rcd
where
left(start_time,7) = '2024-10'
group by date(start_time), start_loc,end_loc
select
hour(start_time) as H,
start_loc,
end_loc,
count(1) as cnt
from
hello_bike_riding_rcd
where
user_id = 'u802844'
and (start_loc = '望京南' and end_loc = '方恒购物中心' or start_loc = '方恒购物中心' and end_loc='望京南')
group by 1,2,3
order by 1,2
select
hour(start_time) as H,
start_loc,
end_loc,
count(1) as cnt
from
hello_bike_riding_rcd
where
user_id = 'u802844'
and (start_loc = '望京南' and end_loc = '方恒购物中心')
or (start_loc = '方恒购物中心' and end_loc='望京南')
group by 1,2,3
order by 1,2