select
date_format(trx_time, '%Y-%m') as trx_mon,
last_day(max(trx_time)) as last_day,
day(last_day(max(trx_time))) as days_of_mon,
sum(trx_amt) as trx_amt,
count(1) as trx_cnt,
sum(trx_amt) / day(last_day(max(trx_time))) as avg_day_amt,
count(1) / day(last_day(max(trx_time))) as avg_day_cnt
from cmb_usr_trx_rcd a
left join cmb_mch_typ b on a.mch_nm = b.mch_nm
where usr_id = '5201314520'
and year(trx_time) in (2023, 2024)
and b.mch_typ = '休闲娱乐'
group by
date_format(trx_time, '%Y-%m')
order by trx_mon
;
select
distinct date_format(trx_time, '%Y-%m') as trx_mon,
last_day(trx_time) as last_day,
day(last_day(trx_time)) as days_of_mon
from cmb_usr_trx_rcd a
left join cmb_mch_typ b on a.mch_nm = b.mch_nm
where usr_id = '5201314520'
and year(trx_time) in (2023, 2024)
and b.mch_typ = '休闲娱乐'
order by trx_mon;
select
distinct date_format(trx_time, '%Y-%m') as trx_mon,
last_day(trx_time) as last_day
from cmb_usr_trx_rcd
where usr_id = '5201314520'
and date(trx_time) between '2023-1-1' and '2024-12-31'
order by trx_mon;
select
b.mch_typ,
a.mch_nm,
count(1) as trx_cnt,
sum(a.trx_amt) as trx_amt
from cmb_usr_trx_rcd a
left join cmb_mch_typ b on a.mch_nm = b.mch_nm
where a.usr_id = '5201314520'
and year(a.trx_time) = 2024
and b.mch_typ is null
group by b.mch_typ, a.mch_nm
order by trx_cnt desc;
select
b.mch_typ,
count(1) as trx_cnt,
sum(a.trx_amt) as trx_amt
from cmb_usr_trx_rcd a
left join cmb_mch_typ b on a.mch_nm = b.mch_nm
where a.usr_id = '5201314520'
and year(a.trx_time) = 2024
group by b.mch_typ
order by trx_cnt desc;
select
date_format(trx_time, '%Y-%m') as trx_mon,
count(1) as trx_cnt,
sum(trx_amt) as trx_amt
from cmb_usr_trx_rcd
where usr_id = '5201314520'
and date(trx_time) between '2022-11-1' and '2024-12-31'
and (
(mod(truncate(trx_amt, 0), 100) in (88, 98) and trx_amt > 200 and hour(trx_time) in (23, 0, 1, 2))
or (mch_nm rlike '足疗|保健|按摩|养生|SPA')
)
group by trx_mon
order by trx_mon asc;
select
case
when (mod(floor(trx_amt), 100) in (88, 98) and trx_amt > 200)
and (hour(trx_time) in (23, 0 ,1, 2, 3))
then 'illegal'
else 'other'
end as type,
count(1) as trx_cnt,
sum(trx_amt) as trx_amt,
count(distinct mch_nm) as mch_cnt
from cmb_usr_trx_rcd
where usr_id = '5201314520'
group by type
order by mch_cnt desc;
WITH punch_days AS (
SELECT employee_id,
TIMESTAMPDIFF(MINUTE, MIN(punch_time), MAX(punch_time)) AS work_minutes
FROM attendance
WHERE YEAR(punch_time) = 2025
GROUP BY employee_id, TO_DAYS(punch_time)
HAVING COUNT(*) >= 2
),
attendance_means AS (
SELECT employee_id,
CAST(SUM(work_minutes + 0e0) AS DECIMAL(18, 8)) / COUNT(*) AS minutes_per_day
FROM punch_days
GROUP BY employee_id
),
walk_days AS (
SELECT ac.employee_id, COUNT(*) AS swipes
FROM access_control AS ac
WHERE YEAR(ac.timestamp) = 2025
AND HOUR(ac.timestamp) IN (9, 10, 11, 14, 15, 16, 17)
GROUP BY ac.employee_id, TO_DAYS(ac.timestamp)
),
walk_scores AS (
SELECT employee_id,
CASE WHEN COUNT(*) > 70 AND COUNT(CASE WHEN swipes > 3 THEN 1 END) > 25 THEN 3
WHEN COUNT(*) > 60 THEN 1
ELSE 0 END AS points
FROM walk_days
GROUP BY employee_id
),
flybook AS (
SELECT SQL_BIG_RESULT employee_id,
SUM(daily_interaction) AS interactions, COUNT(daily_interaction) AS interaction_days,
SUM(daily_messages)AS messages, COUNT(daily_messages)AS message_days,
SUM(daily_documents) AS documents,COUNT(daily_documents) AS document_days
FROM flybook_stats
WHERE stat_date >= '20250131'
GROUP BY employee_id
),
company_flybook AS (
SELECT SUM(interactions) AS interactions, SUM(interaction_days) AS interaction_days,
SUM(messages) AS messages, SUM(message_days) AS message_days,
SUM(documents)AS documents,SUM(document_days)AS document_days
FROM flybook
),
scored AS (
SELECT e.id AS employee_id,
CASE WHEN COALESCE(a.minutes_per_day, 0) = 0 THEN 1
WHEN a.minutes_per_day < (SELECT AVG(minutes_per_day) FROM attendance_means) THEN 1
ELSE 0 END AS attendance_points,
e.salary * (DATEDIFF('2026-03-06', e.hire_date) + 365) AS pay_numerator,
COALESCE(w.points, 0) AS walk_points,
IF(COALESCE(f.interactions, 0) * c.interaction_days <
COALESCE(NULLIF(f.interaction_days, 0), 1) * c.interactions, 1, 0) AS low_interactions,
IF(COALESCE(f.messages, 0) * c.message_days <
COALESCE(NULLIF(f.message_days, 0), 1) * c.messages, 1, 0) AS low_messages,
IF(COALESCE(f.documents, 0) * c.document_days <
COALESCE(NULLIF(f.document_days, 0), 1) * c.documents, 1, 0) AS low_documents
FROM employees AS e
LEFT JOIN attendance_means AS a ON a.employee_id = e.id
LEFT JOIN walk_scores AS w ON w.employee_id = e.id
LEFT JOIN flybook AS f ON f.employee_id = e.id
CROSS JOIN company_flybook AS c
)
SELECT employee_id,
attendance_points + walk_points +
CASE WHEN pay_numerator < 10 * 12 * 365 THEN 3
WHEN pay_numerator < 20 * 12 * 365 THEN 2
WHEN pay_numerator < 30 * 12 * 365 THEN 1
ELSE 0 END +
CASE (low_interactions + low_messages + low_documents)
WHEN 3 THEN 3 WHEN 2 THEN 1 ELSE 0 END AS score
FROM scored;
select
a.mch_nm as asshole_tried,
a.trx_cnt as trx_cnt,
b.mch_nm as darling_tried
from
(
select mch_nm, count(1) as trx_cnt
from cmb_usr_trx_rcd
where usr_id = '5201314520'
and year(trx_time) in (2023, 2024)
group by mch_nm
having count(trx_cnt) >= 20
) a
left join
(
select distinct mch_nm
from cmb_usr_trx_rcd
where usr_id = '5211314521'
and year(trx_time) in (2023, 2024)
) b
on a.mch_nm = b.mch_nm
order by trx_cnt desc;
select
a.mch_nm as asshole_tried,
a.trx_cnt as trx_cnt,
b.mch_nm as darling_tried
from
(
select mch_nm, count(1) as trx_cnt
from cmb_usr_trx_rcd
where usr_id = '5201314520'
and year(trx_time) between 2023 and 2024
group by mch_nm
) a
left join
(
select distinct mch_nm
from cmb_usr_trx_rcd
where usr_id = '5211314521'
and year(trx_time) between 2023 and 2024
) b
on a.mch_nm = b.mch_nm
order by a.trx_cnt desc;
select
a.mch_nm as asshole_tried,
a.trx_cnt,
b.mch_nm as darling_tried
from
(
select mch_nm, count(1) as trx_cnt
from cmb_usr_trx_rcd
where usr_id = '5201314520'
and year(trx_time) between 2023 and 2024
group by mch_nm
) a
left join
(
select distinct mch_nm
from cmb_usr_trx_rcd
where usr_id = '5211314521'
and year(trx_time) between 2023 and 2024
) b
on a.mch_nm = b.mch_nm
order by a.trx_cnt desc;
with zhanan as (
select mch_nm
from cmb_usr_trx_rcd
where year(trx_time) = 2024
and usr_id = '5201314520'
),
npy as (
select mch_nm
from cmb_usr_trx_rcd
where year(trx_time) = 2024
and usr_id = '5211314521'
)
select distinct a.mch_nm
from zhanan a
inner join npy b on a.mch_nm = b.mch_nm;
select
case when mch_nm like '%按摩保健休闲%' then '按摩保健休闲'
when mch_nm rlike '按摩|保健|休闲|养生|SPA|会所' then '按摩、保健、休闲、养生、SPA、会所'
end as reg_rules,
count(distinct mch_nm) as mch_cnt
from cmb_usr_trx_rcd
where mch_nm like '%按摩保健休闲%'
or mch_nm rlike '按摩|保健|休闲|养生|SPA|会所'
group by reg_rules
order by mch_cnt desc;
SELECT
CASE
WHEN trx_amt = '288' THEN '1.WithHand'
WHEN trx_amt = '388' THEN '2.WithMimi'
WHEN trx_amt = '588' THEN '3.BlowJobbie'
WHEN trx_amt = '888' THEN '4.Doi'
WHEN trx_amt = '1288' THEN '5.DoubleFly'
ELSE '6.other'
END AS ser_typ,
count(*) as trx_cnt,
min(date(trx_time)) as first_date
FROM cmb_usr_trx_rcd
WHERE mch_nm = '红玫瑰按摩保健休闲'
AND usr_id = '5201314520'
GROUP BY ser_typ
ORDER BY ser_typ;
SELECT
CASE
WHEN trx_amt = '288' THEN '1.WithHand'
WHEN trx_amt = '388' THEN '2.WithMimi'
WHEN trx_amt = '588' THEN '3.BlowJobbie'
WHEN trx_amt = '888' THEN '4.Doi'
WHEN trx_amt = '1288' THEN '5.DoubleFly'
ELSE '6.other'
END AS ser_typ,
count(*) as trx_cnt,
min(date(trx_time)) as first_date
FROM cmb_usr_trx_rcd
GROUP BY ser_typ
ORDER BY ser_typ;
SELECT
CASE
WHEN trx_amt = '288' THEN '1.WithHand'
WHEN trx_amt = '388' THEN '2.WithMimi'
WHEN trx_amt = '588' THEN '3.BlowJobbie'
WHEN trx_amt = '888' THEN '4.Doi'
WHEN trx_amt = '1288' THEN '5.DoubleFly'
ELSE '6.other'
END AS ser_typ,
count(*) as trx_cnt,
min(trx_time) as first_date
FROM cmb_usr_trx_rcd
GROUP BY ser_typ
ORDER BY ser_typ;
select
usr_id,
mch_nm,
sum(trx_amt) as trx_amt,
count(*) as trx_cnt,
min(trx_time) as first_time
from cmb_usr_trx_rcd
where usr_id = '5201314520'
and trx_amt >= 288
group by usr_id, mch_nm
order by trx_cnt desc;