nushell/docs/commands/group-by.md
Waldir Pimenta 5ca9e12b7f
Fix whitespace and typos (#1481)
* Remove EOL whitespace in files other than docs

* Break paragraphs into lines

See http://rhodesmill.org/brandon/2012/one-sentence-per-line/ for the rationale

* Fix various typos

* Remove EOL whitespace in docs/commands/*.md
2020-03-14 06:23:41 +13:00

73 lines
9.3 KiB
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.

# group-by
This command creates a new table with the data from the table rows grouped by the column given.
## Examples
Let's say we have this table of all countries in the world sorted by their population:
```shell
> open countries_by_population.json | from-json | first 10
━━━┯━━━━━━┯━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━┯━━━━━━━━
# │ rank │ country or area │ UN continental region │ UN statistical region │ population 2018 │ population 2019 │ change
───┼──────┼─────────────────┼───────────────────────┼───────────────────────┼─────────────────┼─────────────────┼────────
01 │ China │ Asia │ Eastern Asia │ 1,427,647,786 │ 1,433,783,686 │ +0.4%
12 │ India │ Asia │ Southern Asia │ 1,352,642,280 │ 1,366,417,754 │ +1.0%
23 │ United States │ Americas │ Northern America │ 327,096,265 │ 329,064,917 │ +0.6%
34 │ Indonesia │ Asia │ South-eastern Asia │ 267,670,543 │ 270,625,568 │ +1.1%
45 │ Pakistan │ Asia │ Southern Asia │ 212,228,286 │ 216,565,318 │ +2.0%
56 │ Brazil │ Americas │ South America │ 209,469,323 │ 211,049,527 │ +0.8%
67 │ Nigeria │ Africa │ Western Africa │ 195,874,683 │ 200,963,599 │ +2.6%
78 │ Bangladesh │ Asia │ Southern Asia │ 161,376,708 │ 163,046,161 │ +1.0%
89 │ Russia │ Europe │ Eastern Europe │ 145,734,038 │ 145,872,256 │ +0.1%
910 │ Mexico │ Americas │ Central America │ 126,190,788 │ 127,575,529 │ +1.1%
━━━┷━━━━━━┷━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━┷━━━━━━━━
```
Here we have listed only the first 10 lines. In total this table has got 233 rows which is to big to get information easily out of it.
We can use the `group-by` command on 'UN statistical region' to create a table per continental region.
```shell
> open countries_by_population.json | from-json | group-by "UN continental region"
━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━
Asia │ Americas │ Africa │ Europe │ Oceania
──────────────────┼──────────────────┼──────────────────┼──────────────────┼──────────────────
[table: 51 rows][table: 53 rows][table: 58 rows][table: 48 rows][table: 23 rows]
━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━
```
Now we can already get some information like "which continental regions are there" and "how many countries are in each region".
If we want to see only the countries in the continental region of Oceania we can type:
```shell
> open countries_by_population.json | from-json | group-by "UN continental region" | get Oceania
━━━━┯━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━┯━━━━━━━━
# │ rank │ country or area │ UN continental region │ UN statistical region │ population 2018 │ population 2019 │ change
────┼──────┼────────────────────────────────┼───────────────────────┼───────────────────────────┼─────────────────┼─────────────────┼────────
055 │ Australia │ Oceania │ Australia and New Zealand │ 24,898,152 │ 25,203,198 │ +1.2%
198 │ Papua New Guinea │ Oceania │ Melanesia │ 8,606,323 │ 8,776,109 │ +2.0%
2125 │ New Zealand │ Oceania │ Australia and New Zealand │ 4,743,131 │ 4,783,063 │ +0.8%
3161 │ Fiji │ Oceania │ Melanesia │ 883,483 │ 889,953 │ +0.7%
4166 │ Solomon Islands │ Oceania │ Melanesia │ 652,857 │ 669,823 │ +2.6%
5181 │ Vanuatu │ Oceania │ Melanesia │ 292,680 │ 299,882 │ +2.5%
6183 │ New Caledonia │ Oceania │ Melanesia │ 279,993 │ 282,750 │ +1.0%
7185 │ French Polynesia │ Oceania │ Polynesia │ 277,679 │ 279,287 │ +0.6%
8188 │ Samoa │ Oceania │ Polynesia │ 196,129 │ 197,097 │ +0.5%
9191 │ Guam │ Oceania │ Micronesia │ 165,768 │ 167,294 │ +0.9%
10193 │ Kiribati │ Oceania │ Micronesia │ 115,847 │ 117,606 │ +1.5%
11194 │ Federated States of Micronesia │ Oceania │ Micronesia │ 112,640 │ 113,815 │ +1.0%
12196 │ Tonga │ Oceania │ Polynesia │ 110,589 │ 110,940 │ +0.3%
13207 │ Marshall Islands │ Oceania │ Micronesia │ 58,413 │ 58,791 │ +0.6%
14209 │ Northern Mariana Islands │ Oceania │ Micronesia │ 56,882 │ 56,188 │ 1.2%
15210 │ American Samoa │ Oceania │ Polynesia │ 55,465 │ 55,312 │ 0.3%
16221 │ Palau │ Oceania │ Micronesia │ 17,907 │ 18,008 │ +0.6%
17222 │ Cook Islands │ Oceania │ Polynesia │ 17,518 │ 17,548 │ +0.2%
18224 │ Tuvalu │ Oceania │ Polynesia │ 11,508 │ 11,646 │ +1.2%
19225 │ Wallis and Futuna │ Oceania │ Polynesia │ 11,661 │ 11,432 │ 2.0%
20226 │ Nauru │ Oceania │ Micronesia │ 10,670 │ 10,756 │ +0.8%
21231 │ Niue │ Oceania │ Polynesia │ 1,620 │ 1,615 │ 0.3%
22232 │ Tokelau │ Oceania │ Polynesia │ 1,319 │ 1,340 │ +1.6%
━━━━┷━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━┷━━━━━━━━
```