# Secure Text-to-SQL & Financial Data Analytics Solution — API Store Hub

> VI: Biến câu hỏi ngôn ngữ tự nhiên thành truy vấn SQL chỉ đọc (read-only), phân tích báo cáo tài chính BCTC và tóm tắt bất thường KPI tự động.
> EN: Turn natural-language business questions into validated read-only SQL queries, analyze financial statements, and automate executive KPI commentary.

- Industry: BI, Data Analytics & Financial Intelligence
- Canonical VI: https://apistorehub.com/vi/giai-phap/phan-tich-du-lieu-tai-chinh-sql
- Canonical EN: https://apistorehub.com/en/giai-phap/phan-tich-du-lieu-tai-chinh-sql

## Reference Architecture
Questions retrieve relevant table schemas via RAG, generate read-only `SELECT` SQL with `gpt-6.2-sol` or `claude-sonnet-5-5`, pass AST read-only validation, execute on a replica, and synthesize executive insights.

## Recommended Model Stack (Live ST Pricing)
| Role | Model | Live ST Rate | Why |
|---|---|---|---|
| Complex SQL (JOINs, CTEs, Window Functions) | `gpt-6.2-sol` | 6,700 ST / 1M tokens | High dialect accuracy across PostgreSQL, MariaDB, BigQuery, and ClickHouse. |
| Financial Filing Analysis & Risk Commentary | `claude-opus-5-5` | 15,300 ST / 1M tokens | Deep reasoning over balance sheets, cash flows, and variance anomalies. |
| Schema Routing & Intent Triage | `deepseek-v4-pro` | 3,600 ST / 1M tokens | Selects only relevant tables to keep SQL prompt context compact. |

## Deployment Steps
1. Provision a strict read-only database replica user with statement timeouts and row caps.
2. Document semantic table/column glossaries including currency units and KPI formulas.
3. Require structured `{ sql, explanation, chart_type }` JSON and verify read-only AST before execution.
4. Feed aggregated query results back to the LLM for executive commentary.

## Quickstart cURL
```bash
curl https://apistorehub.com/v1/chat/completions \
  -H "Authorization: Bearer sk-ash-your_key" \
  -H "Content-Type: application/json" \
  -d '{"model":"gpt-6.2-sol","response_format":{"type":"json_object"},"messages":[{"role":"system","content":"Generate read-only MariaDB SELECT SQL only. Return JSON {sql, notes}."},{"role":"user","content":"Table orders(id, amount_vnd, paid_at). Top 5 days by revenue in last 30 days."}]}'
```

## FAQ
- **How do I prevent destructive SQL like DROP or DELETE?**: Enforce defense in depth: a read-only database role plus backend SQL AST allowlisting for single `SELECT` queries only.
- **Why can Text-to-SQL miscalculate net revenue?**: Ambiguous business definitions. Supply a concise KPI glossary (e.g. excluding refunds/VAT) inside the prompt.
- **Can the pipeline also analyze quarterly financial PDFs?**: Yes. `gemini-4-neon` and `claude-opus-5-5` handle multi-quarter financial filings natively.
