上传源代码版本
This commit is contained in:
69
customer_order_analysis.sql
Normal file
69
customer_order_analysis.sql
Normal file
@@ -0,0 +1,69 @@
|
||||
-- 以客户维度展示客户的每日下单量以及每周的下单量(每周日到周六)
|
||||
SELECT
|
||||
c.Id AS CustomerId,
|
||||
c.CustomerCode,
|
||||
c.CustomerName,
|
||||
-- 每日下单量
|
||||
DATE(lrr.CreatedAt) AS OrderDate,
|
||||
COUNT(*) AS DailyOrderCount,
|
||||
-- 每周下单量(按周日到周六计算)
|
||||
CONCAT(YEARWEEK(lrr.CreatedAt, 0), '周') AS WeekNumber,
|
||||
COUNT(*) OVER (PARTITION BY c.Id, YEARWEEK(lrr.CreatedAt, 0)) AS WeeklyOrderCount
|
||||
FROM
|
||||
customers c
|
||||
JOIN
|
||||
label_replace_requests lrr ON c.Id = lrr.CustomerId
|
||||
WHERE
|
||||
lrr.ReplaceStatus = 'Y' -- 只统计有效订单
|
||||
GROUP BY
|
||||
c.Id,
|
||||
c.CustomerCode,
|
||||
c.CustomerName,
|
||||
DATE(lrr.CreatedAt),
|
||||
YEARWEEK(lrr.CreatedAt, 0)
|
||||
ORDER BY
|
||||
c.CustomerName,
|
||||
OrderDate;
|
||||
|
||||
-- 另一种形式:分别展示每日和每周汇总(每周日到周六)
|
||||
SELECT
|
||||
c.Id AS CustomerId,
|
||||
c.CustomerCode,
|
||||
c.CustomerName,
|
||||
'Daily' AS PeriodType,
|
||||
DATE(lrr.CreatedAt) AS Period,
|
||||
COUNT(*) AS OrderCount
|
||||
FROM
|
||||
customers c
|
||||
JOIN
|
||||
label_replace_requests lrr ON c.Id = lrr.CustomerId
|
||||
WHERE
|
||||
lrr.ReplaceStatus = 'Y'
|
||||
GROUP BY
|
||||
c.Id,
|
||||
c.CustomerCode,
|
||||
c.CustomerName,
|
||||
DATE(lrr.CreatedAt)
|
||||
UNION ALL
|
||||
SELECT
|
||||
c.Id AS CustomerId,
|
||||
c.CustomerCode,
|
||||
c.CustomerName,
|
||||
'Weekly' AS PeriodType,
|
||||
CONCAT(YEARWEEK(lrr.CreatedAt, 0), '周') AS Period,
|
||||
COUNT(*) AS OrderCount
|
||||
FROM
|
||||
customers c
|
||||
JOIN
|
||||
label_replace_requests lrr ON c.Id = lrr.CustomerId
|
||||
WHERE
|
||||
lrr.ReplaceStatus = 'Y'
|
||||
GROUP BY
|
||||
c.Id,
|
||||
c.CustomerCode,
|
||||
c.CustomerName,
|
||||
YEARWEEK(lrr.CreatedAt, 0)
|
||||
ORDER BY
|
||||
CustomerName,
|
||||
PeriodType,
|
||||
Period;
|
||||
Reference in New Issue
Block a user