Files
nexus/wiki/concepts/SQL-View.md
2026-04-18 12:03:16 +08:00

32 lines
921 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "SQL View"
type: concept
tags: [数据库, SQL, 数据预处理]
---
## Definition
预处理的数据库视图View通过 SQL 查询定义,用于解析 JSON 字段、计算派生指标、聚合数据,使 BI 工具能直接使用数值字段。
## Common Use Cases
- 解析 JSON 字段:如 `JSON_EXTRACT(prodct_rating, '$.rating') AS rating`
- 计算派生指标:如 `(final_price * sold) AS total_gmv`
- 数据聚合:如按类目汇总销量、销售额
## Example
```sql
CREATE OR REPLACE VIEW view_products_cleaned AS
SELECT
id,
title,
category,
final_price,
sold,
JSON_EXTRACT(prodct_rating, '$.rating') AS rating,
JSON_EXTRACT(prodct_rating, '$.count') AS rating_count,
(final_price * sold) AS total_gmv
FROM products;
```
## Related Concepts
- [[Apache-Superset]]:使用 SQL View 作为数据集
- [[JSON-字段解析]]:从 JSON 数据中提取值