Click here to Skip to main content
15,898,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table named datafile that contains records as below:

FamilyCode Name Age
1 ABC 23
1 DEF 25
2 GHI 58
2 JKL 23
2 PQR 56

And another table SCHEMETRN that contains records as below:

FamilyCode SchemeId
1 2
2 10

Now I have written query as
"SELECT distinct d.FamilyCode,first d.FullName,d.Age from datafile d inner join SCHEMETRN2 s2 on d.FamilyCode = s2.FamilyCode "

This gives me result as

FamilyCode Name Age
1 ABC 23
1 DEF 25
2 GHI 58
2 JKL 23
2 PQR 56

I only want one record of familycode 1 and one record of familycode 2..
How to get this?
Posted
Comments
syed shanu 25-Jul-13 21:00pm    
Hi,Do you need only Family code ,If so your query is correct ,because from your table different name and age for same Family code .
"SELECT distinct d.FamilyCode from datafile d inner join SCHEMETRN2 s2 on d.FamilyCode = s2.FamilyCode"

SQL
select B.FamilyCode,B.Name,B.age from datafile B,
(SELECT max(age) age,FamilyCode FROM datafile A WHERE A.FamilyCode IN(SELECT FamilyCode  FROM SCHEMETRN )
group by FamilyCode) C
where B.FamilyCode=C.FamilyCode and B.age=C.age



i got it in my sqlserver,and SCHEMETRN2 is SCHEMETRN !

the result is :

1 DEF 25
2 GHI 58
 
Share this answer
 
v3
Comments
berrymaria 25-Jul-13 23:29pm    
Not sure if that's the exact query OP wants. But hey! You did output it!
Well done! My +5! :)
Chui PuiKwan 26-Jul-13 1:49am    
ths.
berrymaria 26-Jul-13 2:15am    
you're welcome! :)
Rachna0309 26-Jul-13 6:56am    
Its not working in ms access
You need to modify SQL code and you will get the expected answer

Original Query
SELECT distinct d.FamilyCode,first d.FullName,d.Age from datafile d inner join SCHEMETRN2 s2 on d.FamilyCode = s2.FamilyCode

Modified Query
SQL
SELECT distinct d.FamilyCode,first d.FullName,d.Age from SCHEMETRN2 s2 inner join datafile d on s2.FamilyCode = d.FamilyCode 
 
Share this answer
 
You need to modify SQL code and you will get the expected answer

Original Query
SQL
SELECT distinct d.FamilyCode,first d.FullName,d.Age from datafile d inner join SCHEMETRN2 s2 on d.FamilyCode = s2.FamilyCode


Modified Query
SQL
SELECT distinct d.FamilyCode,first d.FullName,d.Age from SCHEMETRN2 s2 inner join datafile d on s2.FamilyCode = d.FamilyCode 
 
Share this answer
 
v2
Comments
berrymaria 25-Jul-13 23:24pm    
Post only once. Removed the other.
Besides, your query provided doesn't answer the OP.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900