MySQL 数据分组后取第一条数据
SQL SERVER (mssql) 数据分组后取第一条数据
SQL 如下
找状态=1的数据,按 HospitalId,DeptId 组合并倒序排序,每组里面取第一条数据
SELECT * FROM ( SELECT *, ROW_NUMBER() OVER (PARTITION BY HospitalId, DeptId ORDER BY CreateDate DESC ) AS new_index FROM HospitalInfo WHERE Status = 1 ) t WHERE t.new_index = 1;