sql server - In sql, How to get the highest unique primary key record, out of multiple foreign key of same ID records -


in project have stored procedure.

alter procedure [dbo].[usp_selectallaspirantdetails] begin  select distinct a.[aid]       ,a.[name]       ,a.[gender]       ,a.[contactno]       ,a.[emailid]       ,a.[skillset]       ,a.[experience]       ,a.[companyname]        ,aq.[qualification]       ,aq.[institutename]       ,aq.[yearofpass]       ,aq.[percentage]         ,max(aq.id) [aspirant] full join aspirantqualification aq on a.aid=aq.aid   group a.aid,a.[name],a.[gender],a.[contactno],a.[emailid],a.[skillset],a.                   [experience],a.[companyname] ,aq.[qualification],aq.[institutename] ,         aq.[yearofpass],aq.[percentage]   order a.aid desc       

in aspirant aid primary key , aspirantqualification foreign key. want last entered record aspirantqualification, if have entered 10th,inter, degree qualifications entered. want degree record of aid display in gridview.

write as:

create  procedure [dbo].[usp_selectallaspirantdetails] begin   ;with cte ( select aid,id,qualification,institutename,yearofpass,percentage,        row_number () on (partition aid order id desc) rownum aspirantqualification ) select a.[aid]       ,a.[name]       ,a.[gender]       ,a.[contactno]       ,a.[emailid]       ,a.[skillset]       ,a.[experience]       ,a.[companyname]        ,aq.[qualification]       ,aq.[institutename]       ,aq.[yearofpass]       ,aq.[percentage]        ,aq.id [aspirant]  left join cte aq on a.aid=aq.aid , aq.rownum = 1  end 

check demo here.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -