SELECT DISTINCT HouseHoldMembers.FamilyID,
Year(GetDate())-Year(DOB) as Age,
FirstName+' '+LastName 'HeadOfHousehold',
FamilyFolder.HouseNo
from HouseHoldMembers
left outer join FamilyFolder on FamilyFolder.FamilyID=HouseHoldMembers.FamilyID
WHERE HouseHoldMembers.FamilyID
in (select familyid from HouseHoldMembers where FirstName like 'laxmi')
and HouseHoldMembers.FamilyID like 'alv'+'%'
为此,我得到了——输出是:
FamilyID age HeadOfHousehold HouseNo
ALV01L11034A 25 ashoka 34
ALV01L11034A 74 arpana 34
ALV01L11034A 62 laxmi 34
ALV01L11059A 34 shama 63
ALV01L11059A 62 baby 63
ALV01L11059A 35 laxmi 63
但是,我想显示每个familyid的最大年龄的查询检索(选择符合该特定家庭最大年龄的familyid,下面解释我想要的输出)。请给出解决方案
FamilyID age HeadOfHousehold HouseNo
ALV01L11034A 74 arpana 34
ALV01L11059A 62 baby 63
表结构为:for familymembers table->
memberid varchar(50) as primarykey,firstname varchar(50) as null,lastname varchar(50) as null, relationwithhead varchar(50) as null,familyid varchar(50) as null,dob datetime as null
对于家庭文件夹表->
familyid varchar(50) as primarykey, houseno varchar(50) as null
提前致谢