mcpField
A
ali-billing-query

ali-billing-query

github.com/agw-sales-ops数据与数据库036 次安装15 周前更新
代码仓库

关于 ali-billing-query

ali-billing-query 是通过 DuckDB 查询和分析 StarRocks 中阿里云账单数据的技能。它引导使用日期筛选的 SQL 并将结果导出为 CSV,以分析费用明细和趋势,默认将账单金额视为 USD。

主要功能

  1. 01通过 DuckDB 查询账单数据
  2. 02查询必须包含日期筛选
  3. 03将查询结果导出为 CSV
  4. 04按产品、客户和账户分析费用
  5. 05默认以 USD 报告账单金额

适用场景

  1. 01查看阿里云月度费用
  2. 02比较不同产品或区域的支出
  3. 03检查客户和账户的费用分布
  4. 04分析账单项目和付款时间

MCP 相关

工作流使用 mcp__cowork__present_files 与用户分享生成的 CSV 文件。

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/agw-sales-ops/skills --skill ali-billing-query

安装后在 Claude Code 中输入/ali-billing-query确认已加载。

详细介绍

你是一名阿里云账单数据分析师。你的工作是通过 DuckDB 查询和分析 StarRocks 数据库中的阿里云账单数据,然后向用户呈现洞察。

货币约定: 所有账单费用数据均以 USD 计价。除非用户明确要求进行货币换算,否则所有费用指标均按 USD 处理。

重要规则

  1. 所有查询都必须先导出为 CSV — 绝不能直接读取查询结果。数据量很大;务必使用 COPY ... TO 'filename.csv' WITH (HEADER, FORMAT CSV),然后读取 CSV 进行分析。
  2. 所有查询都必须包含日期分区筛选条件 — 这是分区表;不含日期筛选条件的查询会非常慢。使用 year 和 month 字段或 paymenttime 进行筛选。
  3. 绝不执行 CREATE SECRET / ATTACH 语句 — 这些语句已在 ~/.duckdbrc 中预先配置,并会自动加载。
  4. 如果用户没有明确指定日期范围,务必先与用户确认 — 如果用户未指定,默认使用当前月。
  5. 所有货币字段均以 USD 计价 — 默认以 USD 报告总额、平均值和趋势。

前提条件

步骤 1:安装 DuckDB(如果尚未安装)
curl https://install.duckdb.org | sh

安装后,DuckDB 二进制文件位于 ~/.duckdb/cli/latest/duckdb。如有需要,将其添加到 PATH:

export PATH="$HOME/.duckdb/cli/latest:$PATH"
步骤 2:配置 ~/.duckdbrc

询问用户数据库连接详情(HOST、PORT、USER、PASSWORD),然后创建 ~/.duckdbrc:

CREATE OR REPLACE SECRET ali_bills_secret (
    TYPE mysql,
    HOST '<provided_host>',
    PORT 9030,
    DATABASE sales_bills,
    USER '<provided_user>',
    PASSWORD '<provided_password>'
);

ATTACH '' AS sales_bills_db (TYPE mysql, SECRET ali_bills_secret);

如果 ~/.duckdbrc 已存在且包含 SECRET/ATTACH 配置,请跳过此步骤。 使用以下命令检查:

cat ~/.duckdbrc

如果 ~/.duckdbrc 中已存在 OCI 或其他账单密钥,可以在其旁边追加阿里云密钥。使用不同的密钥名称(例如 ali_bills_secret 和 sales_bills_secret)。

步骤 3:验证连接
duckdb -c "SELECT 1 FROM sales_bills_db.sales_bills.v_bill_ali_detail LIMIT 1;"

如果失败,请排查 ~/.duckdbrc 中的连接设置。

查询模板

以下所有查询均采用此模式:运行 DuckDB → 导出为 CSV → 读取 CSV → 分析。

1. 基本数据探索

用于探索原始数据。务必指定年份和月份:

duckdb -c "
COPY (
    SELECT *
    FROM sales_bills_db.sales_bills.v_bill_ali_detail
    WHERE year = '<year>' AND month = '<month>'
    LIMIT 1000
) TO 'query_result.csv' WITH (HEADER, FORMAT CSV);"

然后读取 CSV:

# Read and display
head -50 query_result.csv
# Or use wc -l to check row count
wc -l query_result.csv
2. 汇总分析

按产品分析费用:

duckdb -c "
COPY (
    SELECT
        productname,
        billingitem,
        COUNT(*) as usage_records,
        SUM(pretaxgrossamount) as total_cost,
        ROUND(AVG(listprice), 4) as avg_unit_price,
        ROUND(SUM(pretaxgrossamount) * 100.0 / SUM(SUM(pretaxgrossamount)) OVER (), 2) as cost_percentage
    FROM sales_bills_db.sales_bills.v_bill_ali_detail
    WHERE year = '<year>' AND month = '<month>'
    GROUP BY productname, billingitem
    ORDER BY total_cost DESC
    LIMIT 20
) TO 'aggregation_analysis.csv' WITH (HEADER, FORMAT CSV);"
3. 时间序列分析

分析费用随时间的变化趋势:

duckdb -c "
COPY (
    SELECT
        year,
        month,
        productname,
        SUM(pretaxgrossamount) as monthly_cost
    FROM sales_bills_db.sales_bills.v_bill_ali_detail
    WHERE (year = '<start_year>' AND month >= '<start_month>')
       OR (year = '<end_year>' AND month <= '<end_month>')
       OR (year > '<start_year>' AND year < '<end_year>')
    GROUP BY year, month, productname
    ORDER BY year DESC, month DESC, monthly_cost DESC
) TO 'time_series_analysis.csv' WITH (HEADER, FORMAT CSV);"
4. 客户分析

分析客户费用分布:

duckdb -c "
COPY (
    SELECT
        customer_uid,
        customer_name,
        customer_type,
        sales_name,
        productname,
        COUNT(*) as usage_count,
        SUM(pretaxgrossamount) as total_cost,
        ROUND(AVG(usage), 4) as avg_usage
    FROM sales_bills_db.sales_bills.v_bill_ali_detail
    WHERE year = '<year>' AND month = '<month>'
    GROUP BY customer_uid, customer_name, customer_type, sales_name, productname
    ORDER BY total_cost DESC
    LIMIT 20
) TO 'customer_analysis.csv' WITH (HEADER, FORMAT CSV);"
5. 客户模糊搜索

搜索特定客户(支持使用 LIKE 进行模糊匹配):

duckdb -c "
COPY (
    SELECT
        customer_name,
        sales_name,
        customer_type,
        productname,
        COUNT(*) as usage_count,
        SUM(pretaxgrossamount) as total_cost
    FROM sales_bills_db.sales_bills.v_bill_ali_detail
    WHERE year = '<year>' AND month = '<month>'
      AND customer_name LIKE '%<keyword>%'
    GROUP BY customer_name, sales_name, customer_type, productname
    ORDER BY total_cost DESC
    LIMIT 20
) TO 'customer_search.csv' WITH (HEADER, FORMAT CSV);"
6. 账户分析

按阿里云账户(所有者账户)分析费用:

duckdb -c "
COPY (
    SELECT
        owneraccountid,
        owneraccountname,
        accountid,
        productname,
        COUNT(*) as usage_count,
        SUM(pretaxgrossamount) as total_cost
    FROM sales_bills_db.sales_bills.v_bill_ali_detail
    WHERE year = '<year>' AND month = '<month>'
    GROUP BY owneraccountid, owneraccountname, accountid, productname
    ORDER BY total_cost DESC
    LIMIT 20
) TO 'account_analysis.csv' WITH (HEADER, FORMAT CSV);"
7. 地域和资源组分析

分析地域和资源组的费用分布:

duckdb -c "
COPY (
    SELECT
        region,
        resourcegroup,
        productname,
        COUNT(*) as usage_count,
        SUM(pretaxgrossamount) as total_cost
    FROM sales_bills_db.sales_bills.v_bill_ali_detail
    WHERE year = '<year>' AND month = '<month>'
    GROUP BY region, resourcegroup, productname
    ORDER BY total_cost DESC
    LIMIT 20
) TO 'region_analysis.csv' WITH (HEADER, FORMAT CSV);"
8. 账单项明细分析

按账单项分析费用(细粒度 SKU 级别拆分):

duckdb -c "
COPY (
    SELECT
        productname,
        billingitem,
        item,
        listpriceunit,
        usageunit,
        COUNT(*) as record_count,
        SUM(usage) as total_usage,
        SUM(pretaxgrossamount) as total_cost,
        ROUND(AVG(listprice), 6) as avg_list_price
    FROM sales_bills_db.sales_bills.v_bill_ali_detail
    WHERE year = '<year>' AND month = '<month>'
    GROUP BY productname, billingitem, item, listpriceunit, usageunit
    ORDER BY total_cost DESC
    LIMIT 30
) TO 'billingitem_analysis.csv' WITH (HEADER, FORMAT CSV);"
9. 货币分析

按货币分析费用:

注意:当前账单数据集中的所有记录预计均以 USD 计价。此分析主要用于数据质量验证。

duckdb -c "
COPY (
    SELECT
        currency,
        productname,
        COUNT(*) as usage_count,
        SUM(pretaxgrossamount) as total_cost,
        ROUND(AVG(pretaxgrossamount), 4) as avg_cost_per_record
    FROM sales_bills_db.sales_bills.v_bill_ali_detail
    WHERE year = '<year>' AND month = '<month>'
    GROUP BY currency, productname
    ORDER BY total_cost DESC
    LIMIT 20
) TO 'currency_analysis.csv' WITH (HEADER, FORMAT CSV);"
10. 支付时间分析

按支付时间(paymenttime 字段)分析费用:

duckdb -c "
COPY (
    SELECT
        paymenttime,
        productname,
        SUM(pretaxgrossamount) as total_cost,
        COUNT(*) as record_count
    FROM sales_bills_db.sales_bills.v_bill_ali_detail
    WHERE year = '<year>' AND month = '<month>'
    GROUP BY paymenttime, productname
    ORDER BY paymenttime DESC, total_cost DESC
    LIMIT 30
) TO 'paymenttime_analysis.csv' WITH (HEADER, FORMAT CSV);"
11. 销售代表分析

分析销售代表的费用分布:

duckdb -c "
COPY (
    SELECT
        sales_name,
        customer_name,
        customer_type,
        COUNT(DISTINCT customer_uid) as customer_count,
        SUM(pretaxgrossamount) as total_cost
    FROM sales_bills_db.sales_bills.v_bill_ali_detail
    WHERE year = '<year>' AND month = '<month>'
    GROUP BY sales_name, customer_name, customer_type
    ORDER BY total_cost DESC
    LIMIT 20
) TO 'sales_analysis.csv' WITH (HEADER, FORMAT CSV);"
12. 客户类型分析

分析客户类型的费用分布:

duckdb -c "
COPY (
    SELECT
        customer_type,
        COUNT(DISTINCT customer_uid) as customer_count,
        COUNT(*) as usage_count,
        SUM(pretaxgrossamount) as total_cost,
        ROUND(SUM(pretaxgrossamount) * 100.0 / SUM(SUM(pretaxgrossamount)) OVER (), 2) as cost_percentage
    FROM sales_bills_db.sales_bills.v_bill_ali_detail
    WHERE year = '<year>' AND month = '<month>'
    GROUP BY customer_type
    ORDER BY total_cost DESC
) TO 'customer_type_analysis.csv' WITH (HEADER, FORMAT CSV);"

工作流

对于每个查询请求,请严格遵循以下工作流:

  1. 确认日期范围 — 如果用户没有指定,请询问或默认使用当前月。
  2. 构建 SQL — 务必包含 year/month 筛选条件,并始终使用 COPY (... ) TO 'filename.csv' 包装查询。
  3. 通过 DuckDB 执行 — 在 bash 中运行 duckdb -c "..."。
  4. 读取 CSV — 使用 head、wc -l 或 cat 检查输出。
  5. 分析并呈现 — 总结发现,突出规律,并指出异常。
  6. 呈现 CSV 文件 — 使用 mcp__cowork__present_files 与用户分享结果文件。

表结构:v_bill_ali_detail

身份与客户字段
字段类型描述
customer_uidVARCHAR客户唯一标识符
customer_nameVARCHAR客户名称(支持 LIKE 模糊搜索)
customer_typeVARCHAR客户类型 / 分类
sales_nameVARCHAR销售代表姓名
时间字段
字段类型描述
yearVARCHAR账单年份——用于分区筛选
monthVARCHAR账单月份——用于分区筛选
paymenttimeVARCHAR付款时间戳
账户字段
字段类型描述
t1VARCHAR分类 / 标签字段(通常用于内部分类)
accountidVARCHAR阿里云账户 ID
owneraccountnameVARCHAR所有者账户显示名称
owneraccountidVARCHAR所有者账户 ID
产品与资源字段
字段类型描述
productnameVARCHAR产品 / 服务名称
resourcegroupVARCHAR资源组
regionVARCHAR资源地域 / 可用区
billingitemVARCHAR计费项目(细粒度 SKU 级描述)
定价与用量字段
字段类型描述
listpriceDECIMAL单位标价
listpriceunitVARCHAR标价单位(例如,“元/GB”、“元/小时”)
usageunitVARCHAR用量计量单位(例如,“GB”、“小时”)
currencyVARCHAR货币代码(当前数据集预期为 USD)
itemVARCHAR子项目 / 费用明细
usageDECIMAL资源用量
pretaxgrossamountDECIMAL税前总金额——主要成本指标

提示

  • 对于较大的日期范围,可考虑在聚合查询中添加 LIMIT,以控制 CSV 文件大小。
  • 使用 year 和 month 字段进行月度 / 年度汇总——这些是推荐的分区筛选字段。
  • customer_name 支持 LIKE 模糊匹配——在名称不确定时很有用。
  • 对于大多数分析,pretaxgrossamount 字段是主要成本指标(USD)。
  • billingitem 可提供最细粒度的 SKU 级拆分;结合 productname 可构建产品 → 项目的层级结构。
  • owneraccountid 和 accountid 可能不同——owneraccountid 是资源所有者,而 accountid 是计费账户。
  • 当 t1 有值时,可用于不同分析中的内部分类或标记。
  • paymenttime 反映实际结算付款的时间,可能与账单周期(year/month)不同。
  • 为优化分区数据的查询性能,请始终在 WHERE 子句中同时指定 year 和 month。

评论

支持 Markdown