Auto-sync: 2026-04-18 12:03
This commit is contained in:
32
wiki/concepts/SQL-View.md
Normal file
32
wiki/concepts/SQL-View.md
Normal file
@@ -0,0 +1,32 @@
|
||||
---
|
||||
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 数据中提取值
|
||||
Reference in New Issue
Block a user