-- =====================================================================
-- @Name:             KUDU-D-SQL-{表名}-QUERY
-- @Version:          1.0
-- @Desc:             Kudu (via Impala) 查询模板
-- @TargetDatabase:   Apache Kudu (via Impala)
-- =====================================================================

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

SELECT 
    id,
    name,
    amount,
    created_at
FROM db_name.kudu_table
WHERE stat_date = '${day_id}'
  AND status = 'active'
ORDER BY created_at DESC
LIMIT 1000;

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

-- 两表 JOIN（Kudu 表 JOIN 非 Kudu 表也支持）
SELECT 
    k.id,
    k.name,
    k.amount,
    d.dept_name
FROM db_name.kudu_table k
JOIN db_name.dim_department d ON k.dept_id = d.dept_id
WHERE k.stat_date = '${day_id}';

-- 多表 JOIN
SELECT 
    k.id,
    k.user_name,
    p.product_name,
    k.quantity,
    k.total_amount
FROM db_name.kudu_orders k
JOIN db_name.dim_users u ON k.user_id = u.user_id
JOIN db_name.dim_products p ON k.product_id = p.product_id
WHERE k.stat_date BETWEEN '${start_date}' AND '${end_date}';

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

SELECT 
    department,
    COUNT(*) AS record_count,
    SUM(amount) AS total_amount,
    AVG(amount) AS avg_amount,
    MAX(amount) AS max_amount,
    MIN(amount) AS min_amount
FROM db_name.kudu_table
WHERE stat_date = '${day_id}'
GROUP BY department
HAVING COUNT(*) >= 5
ORDER BY total_amount DESC;

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

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

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

-- 累计聚合
SELECT 
    stat_date,
    daily_amount,
    SUM(daily_amount) OVER (ORDER BY stat_date) AS cumulative_amount
FROM (
    SELECT stat_date, SUM(amount) AS daily_amount
    FROM db_name.kudu_table
    GROUP BY stat_date
) t;

-- LAG/LEAD（环比）
SELECT 
    stat_date,
    daily_amount,
    LAG(daily_amount, 1) OVER (ORDER BY stat_date) AS prev_amount,
    daily_amount - LAG(daily_amount, 1) OVER (ORDER BY stat_date) AS daily_change
FROM (
    SELECT stat_date, SUM(amount) AS daily_amount
    FROM db_name.kudu_table
    GROUP BY stat_date
) t;

-- ============================================================================
-- 5. 子查询
-- ============================================================================

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

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

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

SELECT 
    stat_date,
    COUNT(*) AS total_count,
    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 amount > 1000 THEN amount ELSE 0 END) AS high_value_amount
FROM db_name.kudu_table
WHERE stat_date = '${day_id}'
GROUP BY stat_date;

-- ============================================================================
-- 7. LIMIT / OFFSET（分页）
-- ============================================================================

SELECT id, name, amount
FROM db_name.kudu_table
WHERE stat_date = '${day_id}'
ORDER BY id
LIMIT 20 OFFSET 0;

-- ============================================================================
-- 8. Kudu 特有：通过主键高效点查
-- ============================================================================
-- Kudu 主键查询可跳过扫描，直接定位 tablet

-- 单主键点查
SELECT * FROM db_name.kudu_table
WHERE id = 12345;

-- 复合主键点查
SELECT * FROM db_name.kudu_composite_pk
WHERE user_id = 1001
  AND order_date = '2026-05-01'
  AND order_seq = 1;

-- 主键 IN 查询
SELECT * FROM db_name.kudu_table
WHERE id IN (1001, 1002, 1003, 1004, 1005);
