Skip to content

Commit

Permalink
Update 'Aggregate queries in Morel' now that [MOREL-28] and [MOREL-29…
Browse files Browse the repository at this point in the history
…] have been fixed
  • Loading branch information
julianhyde committed Apr 14, 2020
1 parent 4aa58b8 commit ed8b7c0
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions blog/_posts/2020-04-09-aggregate-queries-in-morel.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,18 +234,20 @@ practice, while retaining strongly typing and type inference. For example:

```sml
- from e in emps
group e.deptno compute sumEmpno = sum of e.empno;
group e.deptno compute sumSal = sum of e.sal;
val it =
[{deptno=20,sumEmpno=38501},{deptno=10,sumEmpno=23555},
{deptno=30,sumEmpno=46116}] : {deptno:int, sumEmpno:int} list
[{deptno=20,sumSal=10875.0},{deptno=10,sumSal=8750.0},
{deptno=30,sumSal=9400.0}] : {deptno:int, sumSal:real} list
```

(Examples in this post use the new `=` syntax for renaming group keys
and aggregate functions that was introduced in
[[MOREL-24](https://github.com/julianhyde/morel/issues/24)] but has
not yet been released, rather than the old `as` syntax used in
morel-0.2. Also, some examples ignore the fact that
[`sum` cannot yet be applied to `real` values](https://github.com/julianhyde/morel/issues/28).)
morel-0.2, and also assume that `sum` and `+` have overloads for both
`int` and `real`, introduced in
[[MOREL-28](https://github.com/julianhyde/morel/issues/28)] and
[[MOREL-29](https://github.com/julianhyde/morel/issues/29)].)

The key extractor (`e.deptno`) and argument extractor (`e.sal`) are
not functions but expressions that are evaluated in the environment of
Expand All @@ -266,6 +268,17 @@ Here is another example:
group e.deptno, d.dname, e.job
compute sumSal = sum of e.sal,
minRemuneration = min of e.sal + e.commission;
val it =
[{deptno=30,dname="SALES",job="MANAGER",minRemuneration=2850.0,sumSal=2850.0},
{deptno=20,dname="RESEARCH",job="CLERK",minRemuneration=800.0,sumSal=1900.0},
{deptno=10,dname="ACCOUNTING",job="PRESIDENT",minRemuneration=5000.0,sumSal=5000.0},
{deptno=10,dname="ACCOUNTING",job="MANAGER",minRemuneration=2450.0,sumSal=2450.0},
{deptno=20,dname="RESEARCH",job="ANALYST",minRemuneration=3000.0,sumSal=6000.0},
{deptno=30,dname="SALES",job="CLERK",minRemuneration=950.0,sumSal=950.0},
{deptno=10,dname="ACCOUNTING",job="CLERK",minRemuneration=1300.0,sumSal=1300.0},
{deptno=20,dname="RESEARCH",job="MANAGER",minRemuneration=2975.0,sumSal=2975.0},
{deptno=30,dname="SALES",job="SALESMAN",minRemuneration=1500.0,sumSal=5600.0}]
: {deptno:int, dname:string, job:string, minRemuneration:real, sumSal:real} list
```

Several things are more advanced than the previous example. The key is
Expand Down Expand Up @@ -383,3 +396,6 @@ If you have comments, please reply on Twitter:
<div data_dnt="true">
{% twitter page.tweet limit=5 hide_media=true %}
</div>

This article
[has been updated](https://github.com/julianhyde/share/commits/master/blog/_posts/2020-04-09-aggregate-queries-in-morel.md).

0 comments on commit ed8b7c0

Please sign in to comment.