Click here to Skip to main content
11,427,997 members (43,903 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-Server-2008
i have two tables
one having primary key with columns
empid,empname
second having foreign key with columns
empid,empdoj,empclient,empstatus

foreign key table having duplicate values
i have two join two table and select distinct row using the order of emp date of joining
please help me
Posted 28-Dec-12 5:29am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

i have two join two table and select distinct row using the order of emp date of joining
Not too sure, but sounds like you need this:
SELECT 
  mstEmployees.empid,
  mstEmployeesDetails.empname,
  mstEmployeesDetails.empclient,
  mstEmployeesDetails.empstatus
FROM
  mstEmployees
INNER JOIN
  mstEmployeesDetails ON mstEmployees.empid = mstEmployeesDetails.empid 
ORDER BY
  mstEmployeesDetails.empdoj
  Permalink  
Comments
Member 9576671 at 28-Dec-12 21:59pm
   
thanks it's working but result contain duplicate values .it display max doj record only
Sandeep Mewara at 29-Dec-12 2:04am
   
If there are any duplicate data it will display as is. It was assumed that empid was unique. Further, there is no query to get just MAX record.

Try:
SELECT
DISTINCT mstEmployees.empid,
mstEmployeesDetails.empname,
mstEmployeesDetails.empclient,
mstEmployeesDetails.empstatus
FROM
mstEmployees
INNER JOIN
mstEmployeesDetails ON mstEmployees.empid = mstEmployeesDetails.empid
ORDER BY
mstEmployeesDetails.empdoj
Member 9576671 at 29-Dec-12 2:29am
   
the above query shows error
ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
Sandeep Mewara at 29-Dec-12 2:34am
   
Yeah right. Just add 'empdoj' in your select after empstatus. You need to include it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.150428.2 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100