Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.databases.ms-sqlserver > #2241
| Path | csiph.com!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Erland Sommarskog <esquel@sommarskog.se> |
| Newsgroups | comp.databases.ms-sqlserver |
| Subject | Re: Calculating the percentile |
| Date | Thu, 14 Mar 2024 14:55:22 +0100 |
| Organization | Erland Sommarskog |
| Lines | 30 |
| Message-ID | <XnsB13597CDC7B36Yazorman@127.0.0.1> (permalink) |
| References | <20240311144248.432ce7f1a182654f52237410@g{oogle}mail.com> |
| MIME-Version | 1.0 |
| Content-Type | text/plain; charset=windows-1252 |
| Content-Transfer-Encoding | 8bit |
| Injection-Info | dont-email.me; posting-host="435fa701f050f778012518ef71edda4a"; logging-data="1727835"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18wsHJPfKp8joh6tv2d5vBk" |
| User-Agent | Xnews/2006.08.24 Mime-proxy/2.1.c.0 (Win32) |
| Cancel-Lock | sha1:4uPSu4aCDZm4ug52bmLwV9EaqJg= |
| Xref | csiph.com comp.databases.ms-sqlserver:2241 |
Show key headers only | View raw
Anton Shepelev (anton.txt@g{oogle}mail.com) writes:
> I am trying to solve a trivial problem, and my solution
> turns out unexpectedly cumbersome and likeky suboptimal.
> Given a table storing values (val) over dimension (dim), I
> want to query the total, mean and median of the value over
> the dimension. My solution is:
>
> SELECT
> dim ,
> SUM(val) tot,
> AVG(val) avg,
> med
> FROM
> ( SELECT
> dim,
> val,
> PERCENTILE_CONT( 0.5 )
> WITHIN GROUP (ORDER BY val)
> OVER (PARTITION BY dim) AS med
> FROM #test
> ) w_med
> GROUP BY dim, med
>
If you are on SQL 2022, you can use the new function
APPROXIMATIVE_PERCENTILE_CONT which is a regular aggregate function.
But as the name says, it is approximative. It's intended for large
data sets where exact calculation would take a lot of time. I don't
know how well it works on smaller data sets.
Back to comp.databases.ms-sqlserver | Previous | Next — Previous in thread | Find similar
Calculating the percentile Anton Shepelev <anton.txt@g{oogle}mail.com> - 2024-03-11 14:42 +0300
Re: Calculating the percentile Erland Sommarskog <esquel@sommarskog.se> - 2024-03-14 14:55 +0100
csiph-web