简体   繁体   中英

How do I write this sql query and group by?

I am trying to write a query using the dataset in the image. I would like job id in one column, the search leader name in one column, the search owner in the next, and the associate in the last and group by the Job id. I would also like the role type to be the column names

在此处输入图像描述

To get the latest data record, group them by job_id and sort them down by user_id and fetch the first row. All this with the ROW_NUMBER() function

SELECT job_id,user_id,user_name,search_role
FROM
  (SELECT *,
       ROW_NUMBER() OVER(PARTITION BY job_id ORDER BY user_id DESC) as seq
  FROM table) t
WHERE seq = 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM