-
I want to find the minimum row per group, but only if there is a single minimum per group: import ibis
ibis.options.interactive = True
t = ibis.examples.penguins.fetch()
is_smallest = t.body_mass_g == t.body_mass_g.min()
t2 = t.group_by("island").mutate(
is_smallest=is_smallest,
n_smallest=is_smallest.sum(),
)
smallest_per_island = t2.filter(t2.is_smallest, t2.n_smallest == 1) This gives the duckdb sql SELECT
"t0"."species",
"t0"."island",
"t0"."bill_length_mm",
"t0"."bill_depth_mm",
"t0"."flipper_length_mm",
"t0"."body_mass_g",
"t0"."sex",
"t0"."year",
"t0"."body_mass_g" = MIN("t0"."body_mass_g") OVER (PARTITION BY "t0"."island" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS "is_smallest",
SUM(
CAST("t0"."body_mass_g" = MIN("t0"."body_mass_g") OVER (PARTITION BY "t0"."island" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS INT)
) OVER (PARTITION BY "t0"."island" ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS "n_smallest"
FROM "penguins" AS "t0" which results in
|
Beta Was this translation helpful? Give feedback.
Answered by
cpcloud
Jun 15, 2024
Replies: 1 comment
-
There's no SQL that can do that in a single query of which I am aware. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
cpcloud
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's no SQL that can do that in a single query of which I am aware.