mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-10 09:32:06 +00:00
Merge pull request #46547 from den-crane/patch-62
Doc. Better example of nested summing
This commit is contained in:
commit
d582cb0dcf
@ -130,10 +130,57 @@ then this nested table is interpreted as a mapping of `key => (values...)`, and
|
||||
Examples:
|
||||
|
||||
``` text
|
||||
[(1, 100)] + [(2, 150)] -> [(1, 100), (2, 150)]
|
||||
[(1, 100)] + [(1, 150)] -> [(1, 250)]
|
||||
[(1, 100)] + [(1, 150), (2, 150)] -> [(1, 250), (2, 150)]
|
||||
[(1, 100), (2, 150)] + [(1, -100)] -> [(2, 150)]
|
||||
DROP TABLE IF EXISTS nested_sum;
|
||||
CREATE TABLE nested_sum
|
||||
(
|
||||
date Date,
|
||||
site UInt32,
|
||||
hitsMap Nested(
|
||||
browser String,
|
||||
imps UInt32,
|
||||
clicks UInt32
|
||||
)
|
||||
) ENGINE = SummingMergeTree
|
||||
PRIMARY KEY (date, site);
|
||||
|
||||
INSERT INTO nested_sum VALUES ('2020-01-01', 12, ['Firefox', 'Opera'], [10, 5], [2, 1]);
|
||||
INSERT INTO nested_sum VALUES ('2020-01-01', 12, ['Chrome', 'Firefox'], [20, 1], [1, 1]);
|
||||
INSERT INTO nested_sum VALUES ('2020-01-01', 12, ['IE'], [22], [0]);
|
||||
INSERT INTO nested_sum VALUES ('2020-01-01', 10, ['Chrome'], [4], [3]);
|
||||
|
||||
OPTIMIZE TABLE nested_sum FINAL; -- emulate merge
|
||||
|
||||
SELECT * FROM nested_sum;
|
||||
┌───────date─┬─site─┬─hitsMap.browser───────────────────┬─hitsMap.imps─┬─hitsMap.clicks─┐
|
||||
│ 2020-01-01 │ 10 │ ['Chrome'] │ [4] │ [3] │
|
||||
│ 2020-01-01 │ 12 │ ['Chrome','Firefox','IE','Opera'] │ [20,11,22,5] │ [1,3,0,1] │
|
||||
└────────────┴──────┴───────────────────────────────────┴──────────────┴────────────────┘
|
||||
|
||||
SELECT
|
||||
site,
|
||||
browser,
|
||||
impressions,
|
||||
clicks
|
||||
FROM
|
||||
(
|
||||
SELECT
|
||||
site,
|
||||
sumMap(hitsMap.browser, hitsMap.imps, hitsMap.clicks) AS imps_map
|
||||
FROM nested_sum
|
||||
GROUP BY site
|
||||
)
|
||||
ARRAY JOIN
|
||||
imps_map.1 AS browser,
|
||||
imps_map.2 AS impressions,
|
||||
imps_map.3 AS clicks;
|
||||
|
||||
┌─site─┬─browser─┬─impressions─┬─clicks─┐
|
||||
│ 12 │ Chrome │ 20 │ 1 │
|
||||
│ 12 │ Firefox │ 11 │ 3 │
|
||||
│ 12 │ IE │ 22 │ 0 │
|
||||
│ 12 │ Opera │ 5 │ 1 │
|
||||
│ 10 │ Chrome │ 4 │ 3 │
|
||||
└──────┴─────────┴─────────────┴────────┘
|
||||
```
|
||||
|
||||
When requesting data, use the [sumMap(key, value)](../../../sql-reference/aggregate-functions/reference/summap.md) function for aggregation of `Map`.
|
||||
|
Loading…
Reference in New Issue
Block a user