How to get non-group by column X value of the first row of every group by column Y of table T?
I have a table T with columns X, Y and Z. I need to retrieve non-group by column X value of the first row of ev开发者_StackOverflow中文版ery group, group by column Y value, and MIN of column Z value in a SQL single query.
Please could you help me out.
I've assumed that you've got a column x_dt that can be used to determine the first row of a Y group.
SELECT
x,
y,
z
FROM (SELECT
x,
y,
MIN(z) OVER (PARTITION BY y) AS z,
ROW_NUMBER() OVER (PARTITION BY y ORDER BY x_dt) AS rn
FROM T) T2
WHERE rn = 1;
精彩评论