Click here to Skip to main content
16,018,637 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey..

I have a query ,in that Getting Two result as Same Id ..I want to get second result of same id ..


 Name      Id
----------------
 A         1
 B         2
 C         3
 D         3
 E         4
 F         4


IN this , I want the second result of same Id like

  Name     Id
---------------
  A        1
  B        2
  D        3
  F        4   




Is there is any query to get like this .....
Posted

If your Name's sorting is ascending, if can use Max() to get the last detail
SQL
Select * from yourTable where (id, name) = any (select id, Max(name) from yourTable)
 
Share this answer
 
select * from Table where ID in (select top 1 ID from Table where ID=3 order by NAME desc)
If you not solve paste you query we can help on that.

I have create Demo As like your query refer this:

This are my tables and their fields
SQL
UnitTypes:
UnitTypeID(int)  UnitTypeName(varchar)

Units:
UnitID(int) UnitTypeID(int) UnitName(varchar)


Products:
ProductID(int) ProductName(varchar) UnitID(int) UnitTypeID(int)



This is query for you solution:
SQL
select Distinct UT.UnitTypeID,UT.UnitTypeName,U.UnitID,U.UnitName,P.ProductID,P.ProductName,P.UnitId from UnitTypes UT
inner join Units U on U.UnitTypeID=UT.UnitTypeID
inner join Products P on P.UnitId=U.UnitID and P.ProductID in (select MAX(ProductID) from Products where p.UnitId=UnitID)
order by U.UnitID


Accept as answer and vote is solve your problem.
 
Share this answer
 
v2
Comments
aravindnass 3-Jul-13 3:41am    
I am not able to solve .. the top 1 keyword not supported in mysql
jaideepsinh 3-Jul-13 3:45am    
Can you paste you query then i can modify.
aravindnass 3-Jul-13 5:38am    
here is my query -

SELECT o.`FirstName`,,o.`LoginId` FROM `apartmentdetails` AS ad
INNER JOIN `apartmentallotment` AS aa
ON ad.`ApartmentId`=aa.`AparmentId`
INNER JOIN `ownerregistration` AS o
ON o.`OwnerId`=aa.`RegisteredUserId`

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