AND o.`deleted_at` IS NULL
AND o.`store_id` = #{storeId}
AND o.`customer_id` = #{customerId}
AND o.`order_type` = #{orderType}
AND o.`status` = #{status}
AND o.`payment_status` = #{paymentStatus}
AND o.`purchased_at` >= #{beginAt}
AND o.`purchased_at` <= #{endAt}
SELECT
CAST(DATE_FORMAT(FROM_UNIXTIME(o.`purchased_at` / 1000), '%Y%m%d') AS UNSIGNED) AS `dateValue`,
COUNT(1) AS `orderCount`,
COALESCE(SUM(o.`sale_amount`), 0) AS `saleAmount`,
COALESCE(SUM(o.`paid_amount`), 0) AS `paidAmount`,
COALESCE(SUM(o.`pending_paid_amount`), 0) AS `pendingPaidAmount`,
COALESCE(SUM(o.`refunded_amount`), 0) AS `refundedAmount`,
COALESCE(SUM(o.`paid_amount` - o.`refunded_amount`), 0) AS `netPaidAmount`
FROM `gao_sales_order` o
WHERE 1 = 1
GROUP BY DATE_FORMAT(FROM_UNIXTIME(o.`purchased_at` / 1000), '%Y%m%d')
ORDER BY `dateValue` ASC
SELECT
o.`order_type` AS `orderType`,
COUNT(1) AS `orderCount`,
COALESCE(SUM(o.`sale_amount`), 0) AS `saleAmount`,
COALESCE(SUM(o.`paid_amount`), 0) AS `paidAmount`,
COALESCE(SUM(o.`refunded_amount`), 0) AS `refundedAmount`,
COALESCE(SUM(o.`paid_amount` - o.`refunded_amount`), 0) AS `netPaidAmount`
FROM `gao_sales_order` o
WHERE 1 = 1
GROUP BY o.`order_type`
ORDER BY o.`order_type` ASC
SELECT
o.`payment_status` AS `paymentStatus`,
COUNT(1) AS `orderCount`,
COALESCE(SUM(o.`sale_amount`), 0) AS `saleAmount`,
COALESCE(SUM(o.`paid_amount`), 0) AS `paidAmount`,
COALESCE(SUM(o.`pending_paid_amount`), 0) AS `pendingPaidAmount`,
COALESCE(SUM(o.`sale_amount` - o.`paid_amount`), 0) AS `unpaidAmount`,
COALESCE(SUM(o.`refunded_amount`), 0) AS `refundedAmount`
FROM `gao_sales_order` o
WHERE 1 = 1
GROUP BY o.`payment_status`
ORDER BY o.`payment_status` ASC
SELECT
o.`customer_id` AS `customerId`,
c.`code` AS `customerCode`,
c.`name` AS `customerName`,
COUNT(1) AS `orderCount`,
COALESCE(SUM(o.`sale_amount`), 0) AS `saleAmount`,
COALESCE(SUM(o.`paid_amount`), 0) AS `paidAmount`,
COALESCE(SUM(o.`refunded_amount`), 0) AS `refundedAmount`,
COALESCE(SUM(o.`paid_amount` - o.`refunded_amount`), 0) AS `netPaidAmount`
FROM `gao_sales_order` o
LEFT JOIN `gao_customer` c ON c.`id` = o.`customer_id` AND c.`deleted_at` IS NULL
WHERE 1 = 1
AND o.`status` <> 'CANCELLED'
GROUP BY o.`customer_id`, c.`code`, c.`name`
ORDER BY `netPaidAmount` DESC, `orderCount` DESC, o.`customer_id` ASC
LIMIT #{topN}
SELECT
i.`product_id_snapshot` AS `productId`,
MAX(i.`product_name_snapshot`) AS `productName`,
COUNT(DISTINCT o.`id`) AS `orderCount`,
COALESCE(SUM(CASE WHEN i.`item_type` = 'DIRECT_PRODUCT' THEN i.`quantity` ELSE 0 END), 0) AS `directQuantity`,
COALESCE(SUM(CASE WHEN i.`item_type` = 'PACKAGE_ENTITLEMENT' THEN i.`quantity` ELSE 0 END), 0) AS `packageQuantity`,
COALESCE(SUM(i.`reserved_base_quantity`), 0) AS `reservedQuantity`,
COALESCE(SUM(i.`fulfilled_base_quantity`), 0) AS `fulfilledQuantity`,
COALESCE(SUM(i.`base_quantity` - i.`reserved_base_quantity` - i.`fulfilled_base_quantity`), 0) AS `availableQuantity`,
COALESCE(SUM(CASE WHEN i.`item_type` = 'DIRECT_PRODUCT' THEN i.`base_quantity` ELSE 0 END), 0) AS `directBaseQuantity`,
COALESCE(SUM(CASE WHEN i.`item_type` = 'PACKAGE_ENTITLEMENT' THEN i.`base_quantity` ELSE 0 END), 0) AS `packageBaseQuantity`,
COALESCE(SUM(i.`reserved_base_quantity`), 0) AS `reservedBaseQuantity`,
COALESCE(SUM(i.`fulfilled_base_quantity`), 0) AS `fulfilledBaseQuantity`,
COALESCE(SUM(i.`base_quantity` - i.`reserved_base_quantity` - i.`fulfilled_base_quantity`), 0) AS `availableBaseQuantity`,
COALESCE(SUM(CASE WHEN i.`item_type` = 'DIRECT_PRODUCT' THEN COALESCE(i.`line_amount`, 0) ELSE 0 END), 0) AS `directSaleAmount`
FROM `gao_sales_order_item` i
INNER JOIN `gao_sales_order` o ON o.`id` = i.`sales_order_id`
INNER JOIN `gao_product` p ON p.`id` = i.`product_id_snapshot` AND p.`store_id` = o.`store_id`
WHERE o.`store_id` = #{storeId}
AND o.`customer_id` = #{customerId}
AND o.`deleted_at` IS NULL
AND i.`deleted_at` IS NULL
AND o.`status` <> 'CANCELLED'
AND o.`purchased_at` >= #{beginAt}
AND o.`purchased_at` <= #{endAt}
AND i.`product_id_snapshot` = #{productId}
AND i.`sku_id` = #{skuId}
GROUP BY i.`product_id_snapshot`
ORDER BY (SUM(CASE WHEN i.`item_type` = 'DIRECT_PRODUCT' THEN i.`quantity` * COALESCE(i.`base_quantity_per_unit_snapshot`, 1) ELSE 0 END)
+ SUM(CASE WHEN i.`item_type` = 'PACKAGE_ENTITLEMENT' THEN i.`quantity` * COALESCE(i.`base_quantity_per_unit_snapshot`, 1) ELSE 0 END)) DESC,
directSaleAmount DESC,
i.`product_id_snapshot` ASC
LIMIT #{topN}
SELECT
i.`sku_id` AS `skuId`,
MAX(i.`sku_code_snapshot`) AS `skuCode`,
MAX(i.`sku_name_snapshot`) AS `skuName`,
MAX(i.`spec_summary_snapshot`) AS `specSummary`,
i.`product_id_snapshot` AS `productId`,
MAX(i.`product_name_snapshot`) AS `productName`,
COUNT(DISTINCT o.`id`) AS `orderCount`,
COALESCE(SUM(CASE WHEN i.`item_type` = 'DIRECT_PRODUCT' THEN i.`base_quantity` ELSE 0 END), 0) AS `directBaseQuantity`,
COALESCE(SUM(CASE WHEN i.`item_type` = 'PACKAGE_ENTITLEMENT' THEN i.`base_quantity` ELSE 0 END), 0) AS `packageBaseQuantity`,
COALESCE(SUM(i.`reserved_base_quantity`), 0) AS `reservedBaseQuantity`,
COALESCE(SUM(i.`fulfilled_base_quantity`), 0) AS `fulfilledBaseQuantity`,
COALESCE(SUM(i.`base_quantity` - i.`reserved_base_quantity` - i.`fulfilled_base_quantity`), 0) AS `availableBaseQuantity`,
COALESCE(SUM(CASE WHEN i.`item_type` = 'DIRECT_PRODUCT' THEN COALESCE(i.`line_amount`, 0) ELSE 0 END), 0) AS `directSaleAmount`
FROM `gao_sales_order_item` i
INNER JOIN `gao_sales_order` o ON o.`id` = i.`sales_order_id`
INNER JOIN `gao_product` p ON p.`id` = i.`product_id_snapshot` AND p.`store_id` = o.`store_id`
WHERE o.`store_id` = #{storeId}
AND o.`customer_id` = #{customerId}
AND o.`deleted_at` IS NULL
AND i.`deleted_at` IS NULL
AND o.`status` <> 'CANCELLED'
AND o.`purchased_at` >= #{beginAt}
AND o.`purchased_at` <= #{endAt}
AND i.`product_id_snapshot` = #{productId}
AND i.`sku_id` = #{skuId}
GROUP BY i.`sku_id`, i.`product_id_snapshot`
ORDER BY `availableBaseQuantity` DESC, `fulfilledBaseQuantity` DESC, `directSaleAmount` DESC, i.`sku_id` ASC
LIMIT #{topN}
SELECT
o.`status` AS `status`,
COUNT(1) AS `orderCount`,
COALESCE(SUM(o.`sale_amount`), 0) AS `saleAmount`,
COALESCE(SUM(o.`paid_amount`), 0) AS `paidAmount`,
COALESCE(SUM(o.`refunded_amount`), 0) AS `refundedAmount`
FROM `gao_sales_order` o
WHERE 1 = 1
GROUP BY o.`status`
ORDER BY o.`status` ASC
SELECT
COUNT(1) AS `orderCount`,
COALESCE(SUM(o.`sale_amount`), 0) AS `saleAmount`,
COALESCE(SUM(o.`paid_amount`), 0) AS `paidAmount`,
COALESCE(SUM(o.`pending_paid_amount`), 0) AS `pendingPaidAmount`,
COALESCE(SUM(o.`sale_amount` - o.`paid_amount`), 0) AS `unpaidAmount`,
COALESCE(SUM(o.`refunded_amount`), 0) AS `refundedAmount`,
COALESCE(SUM(o.`paid_amount` - o.`refunded_amount`), 0) AS `netPaidAmount`
FROM `gao_sales_order` o
WHERE 1 = 1
WITH `daily_stat` AS (
SELECT
CAST(DATE_FORMAT(FROM_UNIXTIME(r.`effective_at` / 1000), '%Y%m%d') AS UNSIGNED) AS `dateValue`,
COUNT(1) AS `recordCount`,
COALESCE(SUM(r.`amount`), 0) AS `amount`
FROM `gao_customer_consumption_record` r
WHERE r.`store_id` = #{storeId}
AND r.`status` = 'ACTIVE'
AND r.`effective_at` >= #{beginAt}
AND r.`effective_at` <= #{endAt}
AND r.`deleted_at` IS NULL
AND r.`customer_id` = #{customerId}
AND r.`order_type` = #{orderType}
GROUP BY DATE_FORMAT(FROM_UNIXTIME(r.`effective_at` / 1000), '%Y%m%d')
)
SELECT
`dateValue`,
`recordCount`,
`amount`,
SUM(`amount`) OVER (ORDER BY `dateValue` ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS `cumulativeAmount`
FROM `daily_stat`
ORDER BY `dateValue` ASC
SELECT
CAST(DATE_FORMAT(FROM_UNIXTIME(o.`purchased_at` / 1000), '%Y%m%d') AS UNSIGNED) AS `dateValue`,
COUNT(1) AS `orderCount`,
COALESCE(SUM(o.`sale_amount`), 0) AS `saleAmount`,
COALESCE(SUM(o.`paid_amount` - o.`refunded_amount`), 0) AS `netPaidAmount`
FROM `gao_sales_order` o
WHERE 1 = 1
GROUP BY DATE_FORMAT(FROM_UNIXTIME(o.`purchased_at` / 1000), '%Y%m%d')
ORDER BY `dateValue` ASC
AND o.`deleted_at` IS NULL
AND o.`store_id` = #{storeId}
AND o.`customer_id` = #{customerId}
AND o.`order_type` = #{orderType}
AND o.`status` = #{status}
AND o.`status` <> 'CANCELLED'
AND o.`purchased_at` <= #{endAt}
WITH `ranked_order` AS (
SELECT
o.`customer_id`,
o.`purchased_at`,
LAG(o.`purchased_at`) OVER (PARTITION BY o.`customer_id` ORDER BY o.`purchased_at`, o.`id`) AS `previous_purchased_at`
FROM `gao_sales_order` o
WHERE 1 = 1
)
SELECT
CAST(DATE_FORMAT(FROM_UNIXTIME(r.`purchased_at` / 1000), '%Y%m%d') AS UNSIGNED) AS `dateValue`,
COUNT(DISTINCT r.`customer_id`) AS `customerCount`,
COUNT(DISTINCT CASE WHEN r.`previous_purchased_at` IS NOT NULL THEN r.`customer_id` END) AS `repeatCustomerCount`,
SUM(CASE WHEN r.`previous_purchased_at` IS NOT NULL THEN 1 ELSE 0 END) AS `repeatOrderCount`
FROM `ranked_order` r
WHERE r.`purchased_at` >= #{beginAt}
GROUP BY DATE_FORMAT(FROM_UNIXTIME(r.`purchased_at` / 1000), '%Y%m%d')
ORDER BY `dateValue` ASC
WITH `ranked_order` AS (
SELECT
o.`customer_id`,
o.`purchased_at`,
LAG(o.`purchased_at`) OVER (PARTITION BY o.`customer_id` ORDER BY o.`purchased_at`, o.`id`) AS `previous_purchased_at`
FROM `gao_sales_order` o
WHERE 1 = 1
)
SELECT
TIMESTAMPDIFF(DAY, FROM_UNIXTIME(r.`previous_purchased_at` / 1000), FROM_UNIXTIME(r.`purchased_at` / 1000)) AS `intervalDays`,
COUNT(DISTINCT r.`customer_id`) AS `customerCount`,
COUNT(1) AS `orderCount`
FROM `ranked_order` r
WHERE r.`previous_purchased_at` IS NOT NULL
AND r.`purchased_at` >= #{beginAt}
GROUP BY TIMESTAMPDIFF(DAY, FROM_UNIXTIME(r.`previous_purchased_at` / 1000), FROM_UNIXTIME(r.`purchased_at` / 1000))
ORDER BY `intervalDays` ASC