32 lines
921 B
Markdown
32 lines
921 B
Markdown
---
|
||
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 数据中提取值 |