tags:

views:

108

answers:

1

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 every group, group by column Y value, and MIN of column Z value in a SQL single query.

Please could you help me out.

+1  A: 

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;
lins314159
Thank you, it does work.
Vipul