Files
LabelChange-server/unclaimed-count-debug.sql
2026-06-01 16:30:29 +08:00

46 lines
1.3 KiB
SQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- 总无人认领数统计(单独查询,用于排查)
-- 逻辑扫描历史记录中CustomerId为0的有扫描记录的订单数去重
WITH
-- 步骤1获取所有CustomerId为0的扫描记录
UnclaimedScans AS (
SELECT
s.NeutralWaybillNumber,
s.CustomerId,
DATE(CONVERT_TZ(s.CreatedAt, '+00:00', '-05:00')) AS ,
s.CreatedAt AS ,
s.Result,
s.Description
FROM label_scan_history s
WHERE s.CustomerId = 0
),
-- 步骤2每个中性面单号每天取最新一条记录
DailyUnclaimedDistinct AS (
SELECT
NeutralWaybillNumber,
,
,
Result,
Description,
ROW_NUMBER() OVER (PARTITION BY NeutralWaybillNumber, ORDER BY DESC) AS rn
FROM UnclaimedScans
),
-- 步骤3统计每日总无人认领数去重
DailyUnclaimedCount AS (
SELECT
,
COUNT(DISTINCT NeutralWaybillNumber) AS
FROM DailyUnclaimedDistinct
WHERE rn = 1
GROUP BY
)
-- 最终查询结果
SELECT
AS ,
,
(UTC_TIMESTAMP() - INTERVAL 5 HOUR) AS UTC_5
FROM DailyUnclaimedCount
ORDER BY DESC;