Skip to content

Commit

Permalink
Update top-n-and-remain.md
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltTan committed May 19, 2023
1 parent 3e7c610 commit f0d6579
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions content/en/altinity-kb-queries-and-syntax/top-n-and-remain.md
Expand Up @@ -183,3 +183,36 @@ ORDER BY res
null49000050
└──────┴──────────┘
```

## Using WITH TOTALS

The total number will include the top rows as well so the remainder must be calculated by the application

```
SELECT
k,
sum(number) AS res
FROM top_with_rest
GROUP BY k
WITH TOTALS
ORDER BY res DESC
LIMIT 10
┌─k───┬───res─┐
│ 999 │ 99945 │
│ 998 │ 99845 │
│ 997 │ 99745 │
│ 996 │ 99645 │
│ 995 │ 99545 │
│ 994 │ 99445 │
│ 993 │ 99345 │
│ 992 │ 99245 │
│ 991 │ 99145 │
│ 990 │ 99045 │
└─────┴───────┘
Totals:
┌─k─┬──────res─┐
│ │ 49995000 │
└───┴──────────┘
```

0 comments on commit f0d6579

Please sign in to comment.