Guys i want to write an oracle sql query for table A of the following records
--------------------------
R_ID D_ID DEPT
---------------------------
1 200 CLERK
1 50 CLERK
2 190 MANAGER
2 开发者_高级运维 134 DPTY MANAGER
3 12 SERVICE MANAGER
and the result should be like below , ie the first row in all the R_ID
--------------------------
R_ID D_ID DEPT
---------------------------
1 200 CLERK
2 190 MANAGER
3 12 SERVICE MANAGER
kindly help me.
As Ronnis mentioned, I'm not sure how you are ordering the rows, but this is how you would do it in general. Note just substitute the correct ordering in the ORDER BY clauses.
SELECT DISTINCT R_ID
, first_value(D_ID) over (partition by R_ID order by D_ID) D_ID
, first_value(DEPT) over (partition by R_ID order by D_ID) DEPT
FROM your_table
ORDER BY R_ID;
Hope this helps!
SELECT R_ID, D_ID, DEPT
FROM
(
SELECT R_ID, D_ID, DEPT, ROW_NUMBER() over(partition by R_ID order by D_ID) r
) WHERE R=1
SELECT min(r_id), min(d_id), dept FROM the_table_with_no_name GROUP BY dept;
精彩评论