请求 #1
SEQ 138{
"status": "running"
}案例记录 case-run-00126
对账订单头金额与订单行净额。输出存在差异的 order_id、stored_total、calculated_total、difference,其中 calculated_total 为订单行 quantity * unit_price - discount_amount 之和,difference 为 stored_total - calculated_total,均四舍五入 2 位;按绝对差异降序、order_id 升序。
以下字段来自本次运行的冻结记录。
#16#1261completed2,762不可估算未记录237msopenai_compatiblejson_schemagpt-session-bridgegpt-session-bridge未提供2026年8月29日 22:092026年8月29日 22:095b5d98876ea35114f18ce6dfa48cc9800d88b6baba80d311b2f52552a38b31af显示本次实际保存的完整 Prompt。
你是 Text-to-SQL 数据分析与数据开发生成器。先形成简洁、可审计的结构化查询方案,再生成完成问题所需的 SQL;不要输出隐藏推理。
方言与安全规则:
Use DuckDB SQL. Return exactly one read-only query. Do not access files, URLs, extensions, or schemas outside the supplied tables.
数据库结构:
{"semantic_relationships":[{"cardinality":"many_to_one","from_entity":"order","sql_on":"fact_orders.customer_id = dim_customers.customer_id","to_entity":"customer"},{"cardinality":"many_to_one","from_entity":"order","sql_on":"fact_orders.channel_id = dim_channels.channel_id","to_entity":"channel"},{"cardinality":"many_to_one","from_entity":"order_item","sql_on":"fact_order_items.order_id = fact_orders.order_id","to_entity":"order"},{"cardinality":"many_to_one","from_entity":"order_item","sql_on":"fact_order_items.product_id = dim_products.product_id","to_entity":"product"},{"cardinality":"many_to_one","from_entity":"payment","sql_on":"fact_payments.order_id = fact_orders.order_id","to_entity":"order"},{"cardinality":"many_to_one","from_entity":"return","sql_on":"fact_returns.order_id = fact_order_items.order_id AND fact_returns.line_no = fact_order_items.line_no","to_entity":"order_item"}],"tables":[{"columns":[{"data_type":"BIGINT","name":"channel_id","nullable":false},{"data_type":"VARCHAR","name":"channel_name","nullable":false},{"data_type":"VARCHAR","name":"channel_type","nullable":false}],"foreign_keys":[],"name":"dim_channels","primary_key":["channel_id"]},{"columns":[{"data_type":"BIGINT","name":"customer_id","nullable":false},{"data_type":"VARCHAR","name":"customer_name","nullable":false},{"data_type":"VARCHAR","name":"city","nullable":true},{"data_type":"DATE","name":"signup_date","nullable":false},{"data_type":"VARCHAR","name":"segment","nullable":false}],"foreign_keys":[],"name":"dim_customers","primary_key":["customer_id"]},{"columns":[{"data_type":"BIGINT","name":"product_id","nullable":false},{"data_type":"VARCHAR","name":"product_name","nullable":false},{"data_type":"VARCHAR","name":"category","nullable":false},{"data_type":"VARCHAR","name":"brand","nullable":false},{"data_type":"DECIMAL(14,2)","name":"list_price","nullable":false}],"foreign_keys":[],"name":"dim_products","primary_key":["product_id"]},{"columns":[{"data_type":"BIGINT","name":"order_id","nullable":false},{"data_type":"BIGINT","name":"line_no","nullable":false},{"data_type":"BIGINT","name":"product_id","nullable":false},{"data_type":"BIGINT","name":"quantity","nullable":false},{"data_type":"DECIMAL(14,2)","name":"unit_price","nullable":false},{"data_type":"DECIMAL(14,2)","name":"discount_amount","nullable":false}],"foreign_keys":[{"columns":["order_id"],"referenced_columns":["order_id"],"referenced_table":"fact_orders"},{"columns":["product_id"],"referenced_columns":["product_id"],"referenced_table":"dim_products"}],"name":"fact_order_items","primary_key":["order_id","line_no"]},{"columns":[{"data_type":"BIGINT","name":"order_id","nullable":false},{"data_type":"BIGINT","name":"customer_id","nullable":false},{"data_type":"BIGINT","name":"channel_id","nullable":false},{"data_type":"DATE","name":"order_date","nullable":false},{"data_type":"VARCHAR","name":"status","nullable":false},{"data_type":"DECIMAL(14,2)","name":"total_amount","nullable":false}],"foreign_keys":[{"columns":["customer_id"],"referenced_columns":["customer_id"],"referenced_table":"dim_customers"},{"columns":["channel_id"],"referenced_columns":["channel_id"],"referenced_table":"dim_channels"}],"name":"fact_orders","primary_key":["order_id"]},{"columns":[{"data_type":"BIGINT","name":"payment_id","nullable":false},{"data_type":"BIGINT","name":"order_id","nullable":false},{"data_type":"TIMESTAMP","name":"paid_at","nullable":false},{"data_type":"VARCHAR","name":"payment_method","nullable":false},{"data_type":"DECIMAL(14,2)","name":"amount","nullable":false},{"data_type":"VARCHAR","name":"status","nullable":false}],"foreign_keys":[{"columns":["order_id"],"referenced_columns":["order_id"],"referenced_table":"fact_orders"}],"name":"fact_payments","primary_key":["payment_id"]},{"columns":[{"data_type":"BIGINT","name":"return_id","nullable":false},{"data_type":"BIGINT","name":"order_id","nullable":false},{"data_type":"BIGINT","name":"line_no","nullable":false},{"data_type":"TIMESTAMP","name":"returned_at","nullable":false},{"data_type":"BIGINT","name":"return_qty","nullable":false},{"data_type":"DECIMAL(14,2)","name":"refund_amount","nullable":false},{"data_type":"VARCHAR","name":"reason","nullable":true}],"foreign_keys":[{"columns":["order_id","line_no"],"referenced_columns":["order_id","line_no"],"referenced_table":"fact_order_items"}],"name":"fact_returns","primary_key":["return_id"]}]}
语义层与业务口径:
{"business_rules":["完成订单仅指 fact_orders.status = 'completed'。","净销售额为 quantity * unit_price - discount_amount。","paid、refunded、failed 金额只按 fact_payments.status 分类。","退货率为完成订单的 returned_qty / sold_qty,售出数量仅含完成订单。","月份按 UTC Gregorian calendar 计算。","订单头 total_amount 应等于订单行净额汇总,差异视为数据质量异常。"],"dimensions":[{"data_type":"VARCHAR","description":"客户分群","expression":"dim_customers.segment","name":"customer_segment"},{"data_type":"VARCHAR","description":"商品品类","expression":"dim_products.category","name":"product_category"},{"data_type":"VARCHAR","description":"渠道类型","expression":"dim_channels.channel_type","name":"channel_type"},{"data_type":"VARCHAR","description":"UTC Gregorian 月份","expression":"strftime(fact_orders.order_date, '%Y-%m')","name":"order_month"}],"entities":[{"description":"客户主数据","grain":"每行一个客户","name":"customer","primary_key":["customer_id"],"table":"dim_customers"},{"description":"商品主数据","grain":"每行一个商品","name":"product","primary_key":["product_id"],"table":"dim_products"},{"description":"渠道主数据","grain":"每行一个渠道","name":"channel","primary_key":["channel_id"],"table":"dim_channels"},{"description":"订单头","grain":"每行一个订单","name":"order","primary_key":["order_id"],"table":"fact_orders"},{"description":"订单行","grain":"每行一个订单商品行","name":"order_item","primary_key":["order_id","line_no"],"table":"fact_order_items"},{"description":"支付尝试","grain":"每行一笔支付","name":"payment","primary_key":["payment_id"],"table":"fact_payments"},{"description":"退货记录","grain":"每行一条订单行退货","name":"return","primary_key":["return_id"],"table":"fact_returns"}],"metrics":[{"description":"已完成订单数","expression":"COUNT(DISTINCT CASE WHEN fact_orders.status = 'completed' THEN fact_orders.order_id END)","filters":["fact_orders.status = 'completed'"],"grain":"聚合","name":"completed_order_count"},{"description":"完成订单商品行净销售额","expression":"SUM(fact_order_items.quantity * fact_order_items.unit_price - fact_order_items.discount_amount)","filters":["fact_orders.status = 'completed'"],"grain":"聚合","name":"net_revenue"},{"description":"成功支付金额","expression":"SUM(CASE WHEN fact_payments.status = 'paid' THEN fact_payments.amount ELSE 0 END)","filters":[],"grain":"聚合","name":"paid_amount"},{"description":"退款状态支付金额","expression":"SUM(CASE WHEN fact_payments.status = 'refunded' THEN fact_payments.amount ELSE 0 END)","filters":[],"grain":"聚合","name":"refunded_amount"},{"description":"失败支付金额","expression":"SUM(CASE WHEN fact_payments.status = 'failed' THEN fact_payments.amount ELSE 0 END)","filters":[],"grain":"聚合","name":"failed_amount"},{"description":"完成订单退货数量除以售出数量","expression":"SUM(fact_returns.return_qty) / SUM(fact_order_items.quantity)","filters":["fact_orders.status = 'completed'"],"grain":"聚合","name":"return_rate"}],"relationships":[{"cardinality":"many_to_one","from_entity":"order","sql_on":"fact_orders.customer_id = dim_customers.customer_id","to_entity":"customer"},{"cardinality":"many_to_one","from_entity":"order","sql_on":"fact_orders.channel_id = dim_channels.channel_id","to_entity":"channel"},{"cardinality":"many_to_one","from_entity":"order_item","sql_on":"fact_order_items.order_id = fact_orders.order_id","to_entity":"order"},{"cardinality":"many_to_one","from_entity":"order_item","sql_on":"fact_order_items.product_id = dim_products.product_id","to_entity":"product"},{"cardinality":"many_to_one","from_entity":"payment","sql_on":"fact_payments.order_id = fact_orders.order_id","to_entity":"order"},{"cardinality":"many_to_one","from_entity":"return","sql_on":"fact_returns.order_id = fact_order_items.order_id AND fact_returns.line_no = fact_order_items.line_no","to_entity":"order_item"}]}
问题:
对账订单头金额与订单行净额。输出存在差异的 order_id、stored_total、calculated_total、difference,其中 calculated_total 为订单行 quantity * unit_price - discount_amount 之和,difference 为 stored_total - calculated_total,均四舍五入 2 位;按绝对差异降序、order_id 升序。
规划要求:
plan 只记录输出粒度、数据源、连接、过滤、指标、执行步骤和风险检查;每项保持简短。
SQL 必须严格实现该 plan,并遵循语义层业务口径。
summary 只概括最终做法,assumptions 只列无法由上下文确定的必要假设。
输出必须严格匹配此 JSON Schema:
{"$defs":{"QueryPlan":{"additionalProperties":false,"properties":{"filters":{"items":{"type":"string"},"title":"Filters","type":"array"},"grain":{"title":"Grain","type":"string"},"joins":{"items":{"type":"string"},"title":"Joins","type":"array"},"metrics":{"items":{"type":"string"},"title":"Metrics","type":"array"},"risks":{"items":{"type":"string"},"title":"Risks","type":"array"},"sources":{"items":{"type":"string"},"title":"Sources","type":"array"},"steps":{"items":{"type":"string"},"title":"Steps","type":"array"}},"required":["grain","sources","joins","filters","metrics","steps","risks"],"title":"QueryPlan","type":"object"}},"additionalProperties":false,"properties":{"assumptions":{"items":{"type":"string"},"title":"Assumptions","type":"array"},"plan":{"$ref":"#/$defs/QueryPlan"},"sql":{"title":"Sql","type":"string"},"summary":{"title":"Summary","type":"string"}},"required":["plan","sql","summary","assumptions"],"title":"GenerationOutput","type":"object"}按事件序号排列;密钥和 Authorization 已移除。
历史证据边界:完整 Prompt 已保存,但这次 openai_compatible 记录没有保存底层 HTTP 报文或 CLI 参数。本站不会把推测内容伪装成原始请求;未来运行会在 provider.requested 中记录脱敏后的完整调用信封。
SEQ 138{
"status": "running"
}分别显示原始响应、解析结果和 SQL。
{"plan":{"grain":"每行一个存在金额差异的订单。","sources":["fact_orders:订单头存储金额。","fact_order_items:订单行数量、单价和折扣。"],"joins":["按 fact_orders.order_id = fact_order_items.order_id 左连接,保留无订单行的订单。"],"filters":["仅保留 stored_total 与 calculated_total 不相等的订单。"],"metrics":["stored_total = ROUND(fact_orders.total_amount, 2)。","calculated_total = ROUND(SUM(quantity * unit_price - discount_amount), 2),无订单行按 0 处理。","difference = ROUND(stored_total - calculated_total, 2)。"],"steps":["按 order_id 汇总订单行净额。","计算并四舍五入存储金额、行汇总金额及差额。","筛选差异订单,并按绝对差异降序、order_id 升序排序。"],"risks":["无订单行的订单将以 calculated_total = 0 参与对账。","差异判断基于四舍五入到 2 位后的金额。"]},"sql":"WITH reconciliation AS (\n SELECT\n o.order_id,\n ROUND(o.total_amount, 2) AS stored_total,\n ROUND(COALESCE(SUM(oi.quantity * oi.unit_price - oi.discount_amount), 0), 2) AS calculated_total\n FROM fact_orders AS o\n LEFT JOIN fact_order_items AS oi\n ON o.order_id = oi.order_id\n GROUP BY o.order_id, o.total_amount\n), differences AS (\n SELECT\n order_id,\n stored_total,\n calculated_total,\n ROUND(stored_total - calculated_total, 2) AS difference\n FROM reconciliation\n WHERE stored_total <> calculated_total\n)\nSELECT\n order_id,\n stored_total,\n calculated_total,\n difference\nFROM differences\nORDER BY ABS(difference) DESC, order_id ASC","summary":"汇总每个订单的订单行净额,与订单头 total_amount 在 2 位小数精度下对账,仅输出有差异的订单并按指定顺序排列。","assumptions":[]}{
"filters": [
"仅保留 stored_total 与 calculated_total 不相等的订单。"
],
"grain": "每行一个存在金额差异的订单。",
"joins": [
"按 fact_orders.order_id = fact_order_items.order_id 左连接,保留无订单行的订单。"
],
"metrics": [
"stored_total = ROUND(fact_orders.total_amount, 2)。",
"calculated_total = ROUND(SUM(quantity * unit_price - discount_amount), 2),无订单行按 0 处理。",
"difference = ROUND(stored_total - calculated_total, 2)。"
],
"risks": [
"无订单行的订单将以 calculated_total = 0 参与对账。",
"差异判断基于四舍五入到 2 位后的金额。"
],
"sources": [
"fact_orders:订单头存储金额。",
"fact_order_items:订单行数量、单价和折扣。"
],
"steps": [
"按 order_id 汇总订单行净额。",
"计算并四舍五入存储金额、行汇总金额及差额。",
"筛选差异订单,并按绝对差异降序、order_id 升序排序。"
]
}WITH reconciliation AS (
SELECT
o.order_id,
ROUND(o.total_amount, 2) AS stored_total,
ROUND(COALESCE(SUM(oi.quantity * oi.unit_price - oi.discount_amount), 0), 2) AS calculated_total
FROM fact_orders AS o
LEFT JOIN fact_order_items AS oi
ON o.order_id = oi.order_id
GROUP BY o.order_id, o.total_amount
), differences AS (
SELECT
order_id,
stored_total,
calculated_total,
ROUND(stored_total - calculated_total, 2) AS difference
FROM reconciliation
WHERE stored_total <> calculated_total
)
SELECT
order_id,
stored_total,
calculated_total,
difference
FROM differences
ORDER BY ABS(difference) DESC, order_id ASCWITH reconciliation AS (
SELECT
o.order_id,
ROUND(o.total_amount, 2) AS stored_total,
ROUND(COALESCE(SUM(oi.quantity * oi.unit_price - oi.discount_amount), 0), 2) AS calculated_total
FROM fact_orders AS o
LEFT JOIN fact_order_items AS oi
ON o.order_id = oi.order_id
GROUP BY
o.order_id,
o.total_amount
), differences AS (
SELECT
order_id,
stored_total,
calculated_total,
ROUND(stored_total - calculated_total, 2) AS difference
FROM reconciliation
WHERE
stored_total <> calculated_total
)
SELECT
order_id,
stored_total,
calculated_total,
difference
FROM differences
ORDER BY
ABS(difference) DESC,
order_id ASC{
"token_usage": {
"completion_tokens": 363,
"prompt_tokens": 2399,
"total_tokens": 2762
},
"generation_ms": null,
"execution_ms": 236.52795900125057
}包含评分明细、参考 SQL、结构要求、比较规则和结果差异。
{
"ast_rules": [
{
"details": {
"actual": 2,
"required": 1
},
"id": "reconciliation-cte",
"kind": "cte_count",
"passed": true
}
],
"column_count": 5,
"column_names": 5,
"execution": 10,
"ordering": 10,
"protocol": 5,
"read_only_ast": 5,
"row_f1": 45,
"sql_capability": 15,
"total": 100
}WITH item_totals AS (SELECT order_id, SUM(quantity * unit_price - discount_amount) AS calculated_total FROM fact_order_items GROUP BY order_id) SELECT o.order_id, o.total_amount AS stored_total, ROUND(COALESCE(i.calculated_total, 0), 2) AS calculated_total, ROUND(o.total_amount - COALESCE(i.calculated_total, 0), 2) AS difference FROM fact_orders o LEFT JOIN item_totals i ON i.order_id = o.order_id WHERE ABS(o.total_amount - COALESCE(i.calculated_total, 0)) > 0.005 ORDER BY ABS(difference) DESC, o.order_id ASC[
{
"id": "reconciliation-cte",
"kind": "cte_count",
"min": 1
}
]{
"abs_tolerance": "0.005",
"decimal_scale": 2,
"duplicate_policy": "multiset",
"max_rows": 10000,
"rel_tolerance": "0",
"row_order_significant": true
}{
"columns": [
{
"name": "order_id",
"type": "BIGINT"
},
{
"name": "stored_total",
"type": "DECIMAL(14,2)"
},
{
"name": "calculated_total",
"type": "DECIMAL(38,2)"
},
{
"name": "difference",
"type": "DECIMAL(38,2)"
}
],
"digest": "375e58234a18c2c3ff105d25ef0bb1f4b523b6f6b6b502a45f98abc6a6f6a1ca",
"row_count": 1,
"rows": [
[
900004,
"100.00",
"90.00",
"10.00"
]
]
}{
"columns": [
{
"name": "order_id",
"type": "BIGINT"
},
{
"name": "stored_total",
"type": "DECIMAL(14,2)"
},
{
"name": "calculated_total",
"type": "DECIMAL(38,2)"
},
{
"name": "difference",
"type": "DECIMAL(38,2)"
}
],
"extra": [],
"missing": [],
"row_count": 1,
"rows": [
[
900004,
"100.00",
"90.00",
"10.00"
]
]
}{
"visible_summary": "汇总每个订单的订单行净额,与订单头 total_amount 在 2 位小数精度下对账,仅输出有差异的订单并按指定顺序排列。",
"error_code": null,
"error_message": null,
"expected_digest": "375e58234a18c2c3ff105d25ef0bb1f4b523b6f6b6b502a45f98abc6a6f6a1ca",
"actual_digest": "c808a5d85ec5580efe39048c5d24e5f7eb6368f35bb39812f86ea682f770ca72",
"assumptions": []
}按全局事件序号排列,保留时间、级别、类型、消息和 payload。
SEQ 136case.startedinfo{
"status": "generating"
}SEQ 137prompt.builtinfo{
"status": "completed"
}SEQ 138provider.requestedinfo{
"status": "running"
}SEQ 139provider.deltainfo{
"text": "{\"plan\":{\"grain\":\"每行一个存在金额差异的订单。\",\"sources\":[\"fact_orders:订单头存储金额。\",\"fact_order_items:订单行数量、单价和折扣。\"],\"joins\":[\"按 fact_orders.order_id = fact_order_items.order_id 左连接,保留无订单行的订单。\"],\"filters\":[\"仅保留 stored_total 与 calculated_total 不相等的订单。\"],\"metrics\":[\"stored_total = ROUND(fact_orders.total_amount, 2)。\",\"calculated_total = ROUND(SUM(quantity * unit_price - discount_amount), 2),无订单行按 0 处理。\",\"difference = ROUND(stored_total - calculated_total, 2)。\"],\"steps\":[\"按 order_id 汇总订单行净额。\",\"计算并四舍五入存储金额、行汇总金额及差额。\",\"筛选差异订单,并按绝对差异降序、order_id 升序排序。\"],\"risks\":[\"无订单行的订单将以 calculated_total = 0 参与对账。\",\"差异判断基于四舍五入到 2 位后的金额。\"]},\"sql\":\"WITH reconciliation AS (\\n SELECT\\n o.order_id,\\n ROUND(o.total_amount, 2) AS stored_total,\\n ROUND(COALESCE(SUM(oi.quantity * oi.unit_price - oi.discount_amount), 0), 2) AS calculated_total\\n FROM fact_orders AS o\\n LEFT JOIN fact_order_items AS oi\\n ON o.order_id = oi.order_id\\n GROUP BY o.order_id, o.total_amount\\n), differences AS (\\n SELECT\\n order_id,\\n stored_total,\\n calculated_total,\\n ROUND(stored_total - calculated_total, 2) AS difference\\n FROM reconciliation\\n WHERE stored_total <> calculated_total\\n)\\nSELECT\\n order_id,\\n stored_total,\\n calculated_total,\\n difference\\nFROM differences\\nORDER BY ABS(difference) DESC, order_id ASC\",\"summary\":\"汇总每个订单的订单行净额,与订单头 total_amount 在 2 位小数精度下对账,仅输出有差异的订单并按指定顺序排列。\",\"assumptions\":[]}"
}SEQ 140provider.completedinfo{
"elapsed_ms": 14038.330458999553,
"status": "completed",
"token_usage": {
"completion_tokens": 363,
"prompt_tokens": 2399,
"total_tokens": 2762
}
}SEQ 141plan.completedinfo{
"grain": "每行一个存在金额差异的订单。",
"status": "completed",
"steps": 3
}SEQ 142sql.parsedinfo{
"status": "completed"
}SEQ 143score.completedinfo{
"score": 100,
"status": "completed"
}完整 JSON 字段,不经过页面裁剪。
{
"actual_digest": "c808a5d85ec5580efe39048c5d24e5f7eb6368f35bb39812f86ea682f770ca72",
"assumptions": [],
"attempt": 1,
"category": "data_quality",
"comparison": {
"abs_tolerance": "0.005",
"decimal_scale": 2,
"duplicate_policy": "multiset",
"max_rows": 10000,
"rel_tolerance": "0",
"row_order_significant": true
},
"difficulty": "medium",
"error_code": null,
"error_message": null,
"execution_ms": 236.52795900125057,
"expected_digest": "375e58234a18c2c3ff105d25ef0bb1f4b523b6f6b6b502a45f98abc6a6f6a1ca",
"expected_result_preview": {
"columns": [
{
"name": "order_id",
"type": "BIGINT"
},
{
"name": "stored_total",
"type": "DECIMAL(14,2)"
},
{
"name": "calculated_total",
"type": "DECIMAL(38,2)"
},
{
"name": "difference",
"type": "DECIMAL(38,2)"
}
],
"digest": "375e58234a18c2c3ff105d25ef0bb1f4b523b6f6b6b502a45f98abc6a6f6a1ca",
"row_count": 1,
"rows": [
[
900004,
"100.00",
"90.00",
"10.00"
]
]
},
"finished_at": "2026-08-29T22:09:48.684544",
"formatted_sql": "WITH reconciliation AS (\n SELECT\n o.order_id,\n ROUND(o.total_amount, 2) AS stored_total,\n ROUND(COALESCE(SUM(oi.quantity * oi.unit_price - oi.discount_amount), 0), 2) AS calculated_total\n FROM fact_orders AS o\n LEFT JOIN fact_order_items AS oi\n ON o.order_id = oi.order_id\n GROUP BY\n o.order_id,\n o.total_amount\n), differences AS (\n SELECT\n order_id,\n stored_total,\n calculated_total,\n ROUND(stored_total - calculated_total, 2) AS difference\n FROM reconciliation\n WHERE\n stored_total <> calculated_total\n)\nSELECT\n order_id,\n stored_total,\n calculated_total,\n difference\nFROM differences\nORDER BY\n ABS(difference) DESC,\n order_id ASC",
"generated_sql": "WITH reconciliation AS (\n SELECT\n o.order_id,\n ROUND(o.total_amount, 2) AS stored_total,\n ROUND(COALESCE(SUM(oi.quantity * oi.unit_price - oi.discount_amount), 0), 2) AS calculated_total\n FROM fact_orders AS o\n LEFT JOIN fact_order_items AS oi\n ON o.order_id = oi.order_id\n GROUP BY o.order_id, o.total_amount\n), differences AS (\n SELECT\n order_id,\n stored_total,\n calculated_total,\n ROUND(stored_total - calculated_total, 2) AS difference\n FROM reconciliation\n WHERE stored_total <> calculated_total\n)\nSELECT\n order_id,\n stored_total,\n calculated_total,\n difference\nFROM differences\nORDER BY ABS(difference) DESC, order_id ASC",
"generation_ms": null,
"id": 126,
"model_name": "GPT 当前会话桥接(流程验收)",
"model_run_id": 19,
"plan": {
"filters": [
"仅保留 stored_total 与 calculated_total 不相等的订单。"
],
"grain": "每行一个存在金额差异的订单。",
"joins": [
"按 fact_orders.order_id = fact_order_items.order_id 左连接,保留无订单行的订单。"
],
"metrics": [
"stored_total = ROUND(fact_orders.total_amount, 2)。",
"calculated_total = ROUND(SUM(quantity * unit_price - discount_amount), 2),无订单行按 0 处理。",
"difference = ROUND(stored_total - calculated_total, 2)。"
],
"risks": [
"无订单行的订单将以 calculated_total = 0 参与对账。",
"差异判断基于四舍五入到 2 位后的金额。"
],
"sources": [
"fact_orders:订单头存储金额。",
"fact_order_items:订单行数量、单价和折扣。"
],
"steps": [
"按 order_id 汇总订单行净额。",
"计算并四舍五入存储金额、行汇总金额及差额。",
"筛选差异订单,并按绝对差异降序、order_id 升序排序。"
]
},
"prompt": "你是 Text-to-SQL 数据分析与数据开发生成器。先形成简洁、可审计的结构化查询方案,再生成完成问题所需的 SQL;不要输出隐藏推理。\n\n方言与安全规则:\nUse DuckDB SQL. Return exactly one read-only query. Do not access files, URLs, extensions, or schemas outside the supplied tables.\n\n数据库结构:\n{\"semantic_relationships\":[{\"cardinality\":\"many_to_one\",\"from_entity\":\"order\",\"sql_on\":\"fact_orders.customer_id = dim_customers.customer_id\",\"to_entity\":\"customer\"},{\"cardinality\":\"many_to_one\",\"from_entity\":\"order\",\"sql_on\":\"fact_orders.channel_id = dim_channels.channel_id\",\"to_entity\":\"channel\"},{\"cardinality\":\"many_to_one\",\"from_entity\":\"order_item\",\"sql_on\":\"fact_order_items.order_id = fact_orders.order_id\",\"to_entity\":\"order\"},{\"cardinality\":\"many_to_one\",\"from_entity\":\"order_item\",\"sql_on\":\"fact_order_items.product_id = dim_products.product_id\",\"to_entity\":\"product\"},{\"cardinality\":\"many_to_one\",\"from_entity\":\"payment\",\"sql_on\":\"fact_payments.order_id = fact_orders.order_id\",\"to_entity\":\"order\"},{\"cardinality\":\"many_to_one\",\"from_entity\":\"return\",\"sql_on\":\"fact_returns.order_id = fact_order_items.order_id AND fact_returns.line_no = fact_order_items.line_no\",\"to_entity\":\"order_item\"}],\"tables\":[{\"columns\":[{\"data_type\":\"BIGINT\",\"name\":\"channel_id\",\"nullable\":false},{\"data_type\":\"VARCHAR\",\"name\":\"channel_name\",\"nullable\":false},{\"data_type\":\"VARCHAR\",\"name\":\"channel_type\",\"nullable\":false}],\"foreign_keys\":[],\"name\":\"dim_channels\",\"primary_key\":[\"channel_id\"]},{\"columns\":[{\"data_type\":\"BIGINT\",\"name\":\"customer_id\",\"nullable\":false},{\"data_type\":\"VARCHAR\",\"name\":\"customer_name\",\"nullable\":false},{\"data_type\":\"VARCHAR\",\"name\":\"city\",\"nullable\":true},{\"data_type\":\"DATE\",\"name\":\"signup_date\",\"nullable\":false},{\"data_type\":\"VARCHAR\",\"name\":\"segment\",\"nullable\":false}],\"foreign_keys\":[],\"name\":\"dim_customers\",\"primary_key\":[\"customer_id\"]},{\"columns\":[{\"data_type\":\"BIGINT\",\"name\":\"product_id\",\"nullable\":false},{\"data_type\":\"VARCHAR\",\"name\":\"product_name\",\"nullable\":false},{\"data_type\":\"VARCHAR\",\"name\":\"category\",\"nullable\":false},{\"data_type\":\"VARCHAR\",\"name\":\"brand\",\"nullable\":false},{\"data_type\":\"DECIMAL(14,2)\",\"name\":\"list_price\",\"nullable\":false}],\"foreign_keys\":[],\"name\":\"dim_products\",\"primary_key\":[\"product_id\"]},{\"columns\":[{\"data_type\":\"BIGINT\",\"name\":\"order_id\",\"nullable\":false},{\"data_type\":\"BIGINT\",\"name\":\"line_no\",\"nullable\":false},{\"data_type\":\"BIGINT\",\"name\":\"product_id\",\"nullable\":false},{\"data_type\":\"BIGINT\",\"name\":\"quantity\",\"nullable\":false},{\"data_type\":\"DECIMAL(14,2)\",\"name\":\"unit_price\",\"nullable\":false},{\"data_type\":\"DECIMAL(14,2)\",\"name\":\"discount_amount\",\"nullable\":false}],\"foreign_keys\":[{\"columns\":[\"order_id\"],\"referenced_columns\":[\"order_id\"],\"referenced_table\":\"fact_orders\"},{\"columns\":[\"product_id\"],\"referenced_columns\":[\"product_id\"],\"referenced_table\":\"dim_products\"}],\"name\":\"fact_order_items\",\"primary_key\":[\"order_id\",\"line_no\"]},{\"columns\":[{\"data_type\":\"BIGINT\",\"name\":\"order_id\",\"nullable\":false},{\"data_type\":\"BIGINT\",\"name\":\"customer_id\",\"nullable\":false},{\"data_type\":\"BIGINT\",\"name\":\"channel_id\",\"nullable\":false},{\"data_type\":\"DATE\",\"name\":\"order_date\",\"nullable\":false},{\"data_type\":\"VARCHAR\",\"name\":\"status\",\"nullable\":false},{\"data_type\":\"DECIMAL(14,2)\",\"name\":\"total_amount\",\"nullable\":false}],\"foreign_keys\":[{\"columns\":[\"customer_id\"],\"referenced_columns\":[\"customer_id\"],\"referenced_table\":\"dim_customers\"},{\"columns\":[\"channel_id\"],\"referenced_columns\":[\"channel_id\"],\"referenced_table\":\"dim_channels\"}],\"name\":\"fact_orders\",\"primary_key\":[\"order_id\"]},{\"columns\":[{\"data_type\":\"BIGINT\",\"name\":\"payment_id\",\"nullable\":false},{\"data_type\":\"BIGINT\",\"name\":\"order_id\",\"nullable\":false},{\"data_type\":\"TIMESTAMP\",\"name\":\"paid_at\",\"nullable\":false},{\"data_type\":\"VARCHAR\",\"name\":\"payment_method\",\"nullable\":false},{\"data_type\":\"DECIMAL(14,2)\",\"name\":\"amount\",\"nullable\":false},{\"data_type\":\"VARCHAR\",\"name\":\"status\",\"nullable\":false}],\"foreign_keys\":[{\"columns\":[\"order_id\"],\"referenced_columns\":[\"order_id\"],\"referenced_table\":\"fact_orders\"}],\"name\":\"fact_payments\",\"primary_key\":[\"payment_id\"]},{\"columns\":[{\"data_type\":\"BIGINT\",\"name\":\"return_id\",\"nullable\":false},{\"data_type\":\"BIGINT\",\"name\":\"order_id\",\"nullable\":false},{\"data_type\":\"BIGINT\",\"name\":\"line_no\",\"nullable\":false},{\"data_type\":\"TIMESTAMP\",\"name\":\"returned_at\",\"nullable\":false},{\"data_type\":\"BIGINT\",\"name\":\"return_qty\",\"nullable\":false},{\"data_type\":\"DECIMAL(14,2)\",\"name\":\"refund_amount\",\"nullable\":false},{\"data_type\":\"VARCHAR\",\"name\":\"reason\",\"nullable\":true}],\"foreign_keys\":[{\"columns\":[\"order_id\",\"line_no\"],\"referenced_columns\":[\"order_id\",\"line_no\"],\"referenced_table\":\"fact_order_items\"}],\"name\":\"fact_returns\",\"primary_key\":[\"return_id\"]}]}\n\n语义层与业务口径:\n{\"business_rules\":[\"完成订单仅指 fact_orders.status = 'completed'。\",\"净销售额为 quantity * unit_price - discount_amount。\",\"paid、refunded、failed 金额只按 fact_payments.status 分类。\",\"退货率为完成订单的 returned_qty / sold_qty,售出数量仅含完成订单。\",\"月份按 UTC Gregorian calendar 计算。\",\"订单头 total_amount 应等于订单行净额汇总,差异视为数据质量异常。\"],\"dimensions\":[{\"data_type\":\"VARCHAR\",\"description\":\"客户分群\",\"expression\":\"dim_customers.segment\",\"name\":\"customer_segment\"},{\"data_type\":\"VARCHAR\",\"description\":\"商品品类\",\"expression\":\"dim_products.category\",\"name\":\"product_category\"},{\"data_type\":\"VARCHAR\",\"description\":\"渠道类型\",\"expression\":\"dim_channels.channel_type\",\"name\":\"channel_type\"},{\"data_type\":\"VARCHAR\",\"description\":\"UTC Gregorian 月份\",\"expression\":\"strftime(fact_orders.order_date, '%Y-%m')\",\"name\":\"order_month\"}],\"entities\":[{\"description\":\"客户主数据\",\"grain\":\"每行一个客户\",\"name\":\"customer\",\"primary_key\":[\"customer_id\"],\"table\":\"dim_customers\"},{\"description\":\"商品主数据\",\"grain\":\"每行一个商品\",\"name\":\"product\",\"primary_key\":[\"product_id\"],\"table\":\"dim_products\"},{\"description\":\"渠道主数据\",\"grain\":\"每行一个渠道\",\"name\":\"channel\",\"primary_key\":[\"channel_id\"],\"table\":\"dim_channels\"},{\"description\":\"订单头\",\"grain\":\"每行一个订单\",\"name\":\"order\",\"primary_key\":[\"order_id\"],\"table\":\"fact_orders\"},{\"description\":\"订单行\",\"grain\":\"每行一个订单商品行\",\"name\":\"order_item\",\"primary_key\":[\"order_id\",\"line_no\"],\"table\":\"fact_order_items\"},{\"description\":\"支付尝试\",\"grain\":\"每行一笔支付\",\"name\":\"payment\",\"primary_key\":[\"payment_id\"],\"table\":\"fact_payments\"},{\"description\":\"退货记录\",\"grain\":\"每行一条订单行退货\",\"name\":\"return\",\"primary_key\":[\"return_id\"],\"table\":\"fact_returns\"}],\"metrics\":[{\"description\":\"已完成订单数\",\"expression\":\"COUNT(DISTINCT CASE WHEN fact_orders.status = 'completed' THEN fact_orders.order_id END)\",\"filters\":[\"fact_orders.status = 'completed'\"],\"grain\":\"聚合\",\"name\":\"completed_order_count\"},{\"description\":\"完成订单商品行净销售额\",\"expression\":\"SUM(fact_order_items.quantity * fact_order_items.unit_price - fact_order_items.discount_amount)\",\"filters\":[\"fact_orders.status = 'completed'\"],\"grain\":\"聚合\",\"name\":\"net_revenue\"},{\"description\":\"成功支付金额\",\"expression\":\"SUM(CASE WHEN fact_payments.status = 'paid' THEN fact_payments.amount ELSE 0 END)\",\"filters\":[],\"grain\":\"聚合\",\"name\":\"paid_amount\"},{\"description\":\"退款状态支付金额\",\"expression\":\"SUM(CASE WHEN fact_payments.status = 'refunded' THEN fact_payments.amount ELSE 0 END)\",\"filters\":[],\"grain\":\"聚合\",\"name\":\"refunded_amount\"},{\"description\":\"失败支付金额\",\"expression\":\"SUM(CASE WHEN fact_payments.status = 'failed' THEN fact_payments.amount ELSE 0 END)\",\"filters\":[],\"grain\":\"聚合\",\"name\":\"failed_amount\"},{\"description\":\"完成订单退货数量除以售出数量\",\"expression\":\"SUM(fact_returns.return_qty) / SUM(fact_order_items.quantity)\",\"filters\":[\"fact_orders.status = 'completed'\"],\"grain\":\"聚合\",\"name\":\"return_rate\"}],\"relationships\":[{\"cardinality\":\"many_to_one\",\"from_entity\":\"order\",\"sql_on\":\"fact_orders.customer_id = dim_customers.customer_id\",\"to_entity\":\"customer\"},{\"cardinality\":\"many_to_one\",\"from_entity\":\"order\",\"sql_on\":\"fact_orders.channel_id = dim_channels.channel_id\",\"to_entity\":\"channel\"},{\"cardinality\":\"many_to_one\",\"from_entity\":\"order_item\",\"sql_on\":\"fact_order_items.order_id = fact_orders.order_id\",\"to_entity\":\"order\"},{\"cardinality\":\"many_to_one\",\"from_entity\":\"order_item\",\"sql_on\":\"fact_order_items.product_id = dim_products.product_id\",\"to_entity\":\"product\"},{\"cardinality\":\"many_to_one\",\"from_entity\":\"payment\",\"sql_on\":\"fact_payments.order_id = fact_orders.order_id\",\"to_entity\":\"order\"},{\"cardinality\":\"many_to_one\",\"from_entity\":\"return\",\"sql_on\":\"fact_returns.order_id = fact_order_items.order_id AND fact_returns.line_no = fact_order_items.line_no\",\"to_entity\":\"order_item\"}]}\n\n问题:\n对账订单头金额与订单行净额。输出存在差异的 order_id、stored_total、calculated_total、difference,其中 calculated_total 为订单行 quantity * unit_price - discount_amount 之和,difference 为 stored_total - calculated_total,均四舍五入 2 位;按绝对差异降序、order_id 升序。\n\n规划要求:\nplan 只记录输出粒度、数据源、连接、过滤、指标、执行步骤和风险检查;每项保持简短。\nSQL 必须严格实现该 plan,并遵循语义层业务口径。\nsummary 只概括最终做法,assumptions 只列无法由上下文确定的必要假设。\n输出必须严格匹配此 JSON Schema:\n{\"$defs\":{\"QueryPlan\":{\"additionalProperties\":false,\"properties\":{\"filters\":{\"items\":{\"type\":\"string\"},\"title\":\"Filters\",\"type\":\"array\"},\"grain\":{\"title\":\"Grain\",\"type\":\"string\"},\"joins\":{\"items\":{\"type\":\"string\"},\"title\":\"Joins\",\"type\":\"array\"},\"metrics\":{\"items\":{\"type\":\"string\"},\"title\":\"Metrics\",\"type\":\"array\"},\"risks\":{\"items\":{\"type\":\"string\"},\"title\":\"Risks\",\"type\":\"array\"},\"sources\":{\"items\":{\"type\":\"string\"},\"title\":\"Sources\",\"type\":\"array\"},\"steps\":{\"items\":{\"type\":\"string\"},\"title\":\"Steps\",\"type\":\"array\"}},\"required\":[\"grain\",\"sources\",\"joins\",\"filters\",\"metrics\",\"steps\",\"risks\"],\"title\":\"QueryPlan\",\"type\":\"object\"}},\"additionalProperties\":false,\"properties\":{\"assumptions\":{\"items\":{\"type\":\"string\"},\"title\":\"Assumptions\",\"type\":\"array\"},\"plan\":{\"$ref\":\"#/$defs/QueryPlan\"},\"sql\":{\"title\":\"Sql\",\"type\":\"string\"},\"summary\":{\"title\":\"Summary\",\"type\":\"string\"}},\"required\":[\"plan\",\"sql\",\"summary\",\"assumptions\"],\"title\":\"GenerationOutput\",\"type\":\"object\"}",
"provider_request_id": null,
"question": "对账订单头金额与订单行净额。输出存在差异的 order_id、stored_total、calculated_total、difference,其中 calculated_total 为订单行 quantity * unit_price - discount_amount 之和,difference 为 stored_total - calculated_total,均四舍五入 2 位;按绝对差异降序、order_id 升序。",
"radar_dimension": "数据开发",
"raw_output": "{\"plan\":{\"grain\":\"每行一个存在金额差异的订单。\",\"sources\":[\"fact_orders:订单头存储金额。\",\"fact_order_items:订单行数量、单价和折扣。\"],\"joins\":[\"按 fact_orders.order_id = fact_order_items.order_id 左连接,保留无订单行的订单。\"],\"filters\":[\"仅保留 stored_total 与 calculated_total 不相等的订单。\"],\"metrics\":[\"stored_total = ROUND(fact_orders.total_amount, 2)。\",\"calculated_total = ROUND(SUM(quantity * unit_price - discount_amount), 2),无订单行按 0 处理。\",\"difference = ROUND(stored_total - calculated_total, 2)。\"],\"steps\":[\"按 order_id 汇总订单行净额。\",\"计算并四舍五入存储金额、行汇总金额及差额。\",\"筛选差异订单,并按绝对差异降序、order_id 升序排序。\"],\"risks\":[\"无订单行的订单将以 calculated_total = 0 参与对账。\",\"差异判断基于四舍五入到 2 位后的金额。\"]},\"sql\":\"WITH reconciliation AS (\\n SELECT\\n o.order_id,\\n ROUND(o.total_amount, 2) AS stored_total,\\n ROUND(COALESCE(SUM(oi.quantity * oi.unit_price - oi.discount_amount), 0), 2) AS calculated_total\\n FROM fact_orders AS o\\n LEFT JOIN fact_order_items AS oi\\n ON o.order_id = oi.order_id\\n GROUP BY o.order_id, o.total_amount\\n), differences AS (\\n SELECT\\n order_id,\\n stored_total,\\n calculated_total,\\n ROUND(stored_total - calculated_total, 2) AS difference\\n FROM reconciliation\\n WHERE stored_total <> calculated_total\\n)\\nSELECT\\n order_id,\\n stored_total,\\n calculated_total,\\n difference\\nFROM differences\\nORDER BY ABS(difference) DESC, order_id ASC\",\"summary\":\"汇总每个订单的订单行净额,与订单头 total_amount 在 2 位小数精度下对账,仅输出有差异的订单并按指定顺序排列。\",\"assumptions\":[]}",
"reference_sql": "WITH item_totals AS (SELECT order_id, SUM(quantity * unit_price - discount_amount) AS calculated_total FROM fact_order_items GROUP BY order_id) SELECT o.order_id, o.total_amount AS stored_total, ROUND(COALESCE(i.calculated_total, 0), 2) AS calculated_total, ROUND(o.total_amount - COALESCE(i.calculated_total, 0), 2) AS difference FROM fact_orders o LEFT JOIN item_totals i ON i.order_id = o.order_id WHERE ABS(o.total_amount - COALESCE(i.calculated_total, 0)) > 0.005 ORDER BY ABS(difference) DESC, o.order_id ASC",
"requested_model_id": "gpt-session-bridge",
"required_ast": [
{
"id": "reconciliation-cte",
"kind": "cte_count",
"min": 1
}
],
"resolved_model_id": "gpt-session-bridge",
"result_preview": {
"columns": [
{
"name": "order_id",
"type": "BIGINT"
},
{
"name": "stored_total",
"type": "DECIMAL(14,2)"
},
{
"name": "calculated_total",
"type": "DECIMAL(38,2)"
},
{
"name": "difference",
"type": "DECIMAL(38,2)"
}
],
"extra": [],
"missing": [],
"row_count": 1,
"rows": [
[
900004,
"100.00",
"90.00",
"10.00"
]
]
},
"run_id": 16,
"score": {
"ast_rules": [
{
"details": {
"actual": 2,
"required": 1
},
"id": "reconciliation-cte",
"kind": "cte_count",
"passed": true
}
],
"column_count": 5,
"column_names": 5,
"execution": 10,
"ordering": 10,
"protocol": 5,
"read_only_ast": 5,
"row_f1": 45,
"sql_capability": 15,
"total": 100
},
"stable_key": "order_total_reconciliation",
"started_at": "2026-08-29T22:09:34.369723",
"status": "completed",
"suite_content_hash": "5b5d98876ea35114f18ce6dfa48cc9800d88b6baba80d311b2f52552a38b31af",
"title": "订单头明细金额对账",
"token_usage": {
"completion_tokens": 363,
"prompt_tokens": 2399,
"total_tokens": 2762
},
"visible_summary": "汇总每个订单的订单行净额,与订单头 total_amount 在 2 位小数精度下对账,仅输出有差异的订单并按指定顺序排列。"
}