Remix.run Logo
snthpy 4 days ago

Here's a function to normalize values relative to the column range:

```prql

let normalize = func x -> ((x - min x)/((max x) - (min x)) | math.round 2)

from tracks

take 5

derive {ms_norm=(normalize milliseconds), bytes_norm=(normalize bytes)}

select {track_id, ms=milliseconds, ms_norm, bytes, bytes_norm}

```

which produces the following SQL:

```sql

WITH table_0 AS (

  SELECT
    track_id,
    bytes,
    milliseconds

  FROM
    tracks

  LIMIT
    5
)

SELECT track_id,

  milliseconds AS ms,

  ROUND(
    (milliseconds - MIN(milliseconds) OVER ()) / (
      MAX(milliseconds) OVER () - MIN(milliseconds) OVER ()
    ),
    2
  ) AS ms_norm,

  bytes,

  ROUND(
    (bytes - MIN(bytes) OVER ()) / (MAX(bytes) OVER () - MIN(bytes) OVER ()),
    2
  ) AS bytes_norm
FROM table_0

-- Generated by PRQL compiler version:0.13.2 (https://prql-lang.org)

```