> For the complete documentation index, see [llms.txt](https://imhuay.gitbook.io/studies/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://imhuay.gitbook.io/studies/notes/_archives/2023/01/sql-chang-yong-cha-xun-ji-lu.md).

# Hive/Spark SQL 常用查询记录

![last modify](https://img.shields.io/static/v1?label=last%20modify\&message=2023-02-19%2020%3A11%3A02\&color=yellowgreen\&style=flat-square)

> ***Keywords**: Hive/Spark SQL*

## 聚合操作

###

## 个人习惯

### 集合字段 `map/json`

* 作为一个额外字段, 可以把新的字段统一放到这里, 防止频繁修改表;

```sql
-- CREATE
CREATE TABLE db.table (
    ...
    , `features`    map < string, double >  COMMENT 'features'
    , `info_json`   string                  COMMENT 'extra info'
) COMMENT 'xxx 特征表'

-- INSERT

INSERT OVERWRITE TABLE db.table
SELECT ...
    , map(
          'f1', f1_double
        , 'f2', f2_int
        , ...
      ) AS features
    , concat('{'
        , '"k1":', k1_int, ','  -- 注意 key 要双引号
        , '"k2":', k2
      , '}') AS info_json
FROM ...

-- SELECT
SELECT ...
    ,  features['f1'] AS f1
    ,  features['f2'] AS f2
    ,  get_json_object(info_json, '$.k1') AS k1
    ,  get_json_object(info_json, '$.k2') AS k2
FROM db.table
```

### 小词表

```sql
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://imhuay.gitbook.io/studies/notes/_archives/2023/01/sql-chang-yong-cha-xun-ji-lu.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
