-- =====================================================================
-- @Name:             DORIS-D-SQL-{表名}-QUERY
-- @Version:          1.0
-- @Desc:             Apache Doris 查询模板
-- @TargetDatabase:   Apache Doris
-- =====================================================================

-- ============================================================================
-- 1. 单表查询
-- ============================================================================

SELECT 
    order_id,
    user_name,
    total_amount,
    create_time
FROM db_name.orders
WHERE order_date = '${day_id}'
  AND status = 'completed'
ORDER BY total_amount DESC
LIMIT 100;

-- ============================================================================
-- 2. JOIN 查询
-- ============================================================================

-- 两表 JOIN
SELECT 
    o.order_id,
    o.total_amount,
    u.user_name,
    u.vip_level
FROM db_name.orders o
JOIN db_name.users u ON o.user_id = u.user_id
WHERE o.order_date = '${day_id}'
  AND o.status = 'completed';

-- 多表 JOIN
SELECT 
    o.order_id,
    u.user_name,
    p.product_name,
    oi.quantity,
    oi.unit_price
FROM db_name.orders o
JOIN db_name.users u ON o.user_id = u.user_id
JOIN db_name.order_items oi ON o.order_id = oi.order_id
JOIN db_name.products p ON oi.product_id = p.product_id
WHERE o.order_date BETWEEN '${start_date}' AND '${end_date}';

-- ============================================================================
-- 3. 聚合查询
-- ============================================================================

-- GROUP BY + HAVING
SELECT 
    department,
    COUNT(*) AS order_count,
    SUM(total_amount) AS total_amount,
    AVG(total_amount) AS avg_amount
FROM db_name.orders
WHERE order_date = '${day_id}'
GROUP BY department
HAVING COUNT(*) >= 5
ORDER BY total_amount DESC;

-- 多字段分组 + 去重计数
SELECT 
    order_date,
    region,
    COUNT(*) AS order_count,
    COUNT(DISTINCT user_id) AS unique_users,
    SUM(total_amount) AS total_amount
FROM db_name.orders
WHERE order_date BETWEEN '${start_date}' AND '${end_date}'
GROUP BY order_date, region;

-- ============================================================================
-- 4. 窗口函数
-- ============================================================================

-- ROW_NUMBER（分组取Top N）
SELECT *
FROM (
    SELECT 
        department,
        user_name,
        total_amount,
        ROW_NUMBER() OVER (PARTITION BY department ORDER BY total_amount DESC) AS rn
    FROM db_name.orders
    WHERE order_date = '${day_id}'
) t
WHERE rn <= 3;

-- 累计聚合
SELECT 
    order_date,
    daily_amount,
    SUM(daily_amount) OVER (ORDER BY order_date) AS cumulative_amount,
    AVG(daily_amount) OVER (
        ORDER BY order_date
        ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
    ) AS moving_avg_7d
FROM (
    SELECT order_date, SUM(total_amount) AS daily_amount
    FROM db_name.orders
    GROUP BY order_date
) t;

-- LAG/LEAD（环比计算）
SELECT 
    order_date,
    daily_amount,
    LAG(daily_amount, 1) OVER (ORDER BY order_date) AS prev_amount,
    daily_amount - LAG(daily_amount, 1) OVER (ORDER BY order_date) AS daily_change,
    ROUND(
        (daily_amount - LAG(daily_amount, 1) OVER (ORDER BY order_date)) 
        / LAG(daily_amount, 1) OVER (ORDER BY order_date) * 100, 2
    ) AS growth_rate_pct
FROM (
    SELECT order_date, SUM(total_amount) AS daily_amount
    FROM db_name.orders
    GROUP BY order_date
) t;

-- ============================================================================
-- 5. 分页查询
-- ============================================================================

-- LIMIT OFFSET 分页（Doris 原生支持）
SELECT 
    order_id, user_name, total_amount
FROM db_name.orders
WHERE order_date = '${day_id}'
ORDER BY order_id
LIMIT 20 OFFSET 0;          -- 第1页，每页20条

-- ============================================================================
-- 6. 子查询
-- ============================================================================

-- IN 子查询
SELECT *
FROM db_name.orders
WHERE user_id IN (
    SELECT user_id FROM db_name.users WHERE vip_level >= 3
)
AND order_date = '${day_id}';

-- EXISTS 子查询
SELECT *
FROM db_name.products p
WHERE EXISTS (
    SELECT 1 FROM db_name.inventory i 
    WHERE i.product_id = p.product_id 
      AND i.quantity > 0
);

-- ============================================================================
-- 7. 条件聚合（CASE WHEN + 聚合）
-- ============================================================================

SELECT 
    order_date,
    COUNT(*) AS total_orders,
    SUM(CASE WHEN status = 'completed' THEN 1 ELSE 0 END) AS completed_count,
    SUM(CASE WHEN status = 'cancelled' THEN 1 ELSE 0 END) AS cancelled_count,
    SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) AS pending_count,
    SUM(CASE WHEN total_amount > 1000 THEN total_amount ELSE 0 END) AS high_value_amount
FROM db_name.orders
WHERE order_date = '${day_id}'
GROUP BY order_date;

-- ============================================================================
-- 8. Bitmap 精确去重（Doris 特有）
-- ============================================================================

-- 使用 bitmap 做精确去重（需要在 Aggregate Key 模型中定义 BITMAP 类型列）
-- 注意：bitmap 函数只能用于包含 BITMAP 类型列的表

-- 精确去重计数（预计算场景，在 Aggregate Key 表中使用 BITMAP_UNION）
-- 建表时 Value 列定义：user_id_bitmap BITMAP BITMAP_UNION
-- 查询时：
-- SELECT date, bitmap_union_count(user_id_bitmap) AS uv FROM table GROUP BY date;

-- HLL 近似去重
-- 建表时 Value 列定义：user_id_hll HLL HLL_UNION
-- 查询时：
-- SELECT date, hll_union_agg(user_id_hll) AS approx_uv FROM table GROUP BY date;
