Select the first row of a column in a group of rows within another column

*爱你&永不变心* 提交于 2021-02-11 15:01:18

问题


I have to display a list of the first row of a column within a group of rows within another column

Basic Query:

SELECT * FROM Table_Example cxp

My Table

    |plan   |code   |des   |month   |
----+-------+-------+------+--------+
    | 150B  | Alpha | etc.a| 1      |
----+-------+-------+------+--------+
    | 150B  | Beta  | etc.b| 2      |
----+-------+-------+------+--------+
    | 2600C | Alpha | etc.a| 6      |
----+-------+-------+------+--------+
    | 2600C | Alpha | etc.a| 7      |
----+-------+-------+------+--------+
    | 2600C | Beta  | etc.b| 3      |
----+-------+-------+------+--------+
    | 2600C | Beta  | etc.b| 7      |
----+-------+-------+------+--------+
    | 2600C | Beta  | etc.b| 9      |
----+-------+-------+------+--------+
    | 2600C | Beta  | etc.b| 2      |
----+-------+-------+------+--------+
    | 2600C | Gama  | etc.c| 4      |
----+-------+-------+------+--------+
    | 2600C | Gama  | etc.c| 12     |
----+-------+-------+------+--------+
    | 2600C | Delta | etc.d| 22     |
----+-------+-------+------+--------+
    | 42F   | Alpha | etc.a| 15     |

Expected Result

    |plan   |code   |des   |month   |
----+-------+-------+------+--------+
    | 2600C | Alpha | etc.a| 6      |
----+-------+-------+------+--------+
    | 2600C | Beta  | etc.b| 3      |
----+-------+-------+------+--------+
    | 2600C | Gama  | etc.g| 4      |
----+-------+-------+------+--------+
    | 2600C | Delta | etc.d| 22     |
----+-------+-------+------+--------+

Transcript Result: Having the variable '@plan', only show the rows that match that value (example: if '@plan' is 2600C) and within that group of rows, only show the first row of each different 'code' value

What i tried to do

SELECT * FROM Table_Example cxp WHERE plan = '2600C'

    |plan   |code   |des   |month   |
----+-------+-------+------+--------+
    | 2600C | Alpha | etc.a| 6      |
----+-------+-------+------+--------+
    | 2600C | Alpha | etc.a| 7      |
----+-------+-------+------+--------+
    | 2600C | Beta  | etc.b| 3      |
----+-------+-------+------+--------+
    | 2600C | Beta  | etc.b| 7      |
----+-------+-------+------+--------+
    | 2600C | Beta  | etc.b| 9      |
----+-------+-------+------+--------+
    | 2600C | Beta  | etc.b| 2      |
----+-------+-------+------+--------+
    | 2600C | Gama  | etc.c| 4      |
----+-------+-------+------+--------+
    | 2600C | Gama  | etc.c| 12     |
----+-------+-------+------+--------+
    | 2600C | Delta | etc.d| 22     |

But i can't get only the first row of each different 'code' I was thinking in a SubQuery but i tried a lot of them and i cant get the correct result or it returns only the 'code' column without the other columns like:

|code   |
+-------+
| Alpha |     
+-------+
| Beta  |       
+-------+
| Gama  |      
+-------+
| Delta |      
+-------+

回答1:


You can use row_number()

select * from
(
  SELECT *,row_number() over(partition by code order by month) as rnk
  FROM Table_Example cxp
  WHERE plan = '2600C'
)A where rnk=1


来源:https://stackoverflow.com/questions/64628483/select-the-first-row-of-a-column-in-a-group-of-rows-within-another-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!