0
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

提前致谢

4

3 回答 3

0

如果您的 DBMS 支持 OLAP 函数,则它是一个简单的 ROW_NUMBER:

SELECT *
FROM
  (
    SELECT
      HouseHoldMembers.FamilyID
      ,YEAR(GetDate()) - YEAR(DOB) AS Age
      ,FirstName + ' ' + LastName 'HeadOfHousehold'
      ,FamilyFolder.HouseNo
      ,ROW_NUMBER() OVER(PARTITION BY FamilyId ORDER BY YEAR(GetDate()) - YEAR(DOB) DESC
      ) AS rn
    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' + '%' d`
  ) AS dt
WHERE rn = 1
于 2013-09-16T10:09:38.120 回答
0

我认为你可以使用max(age)group by(FamilyID)得到你的答案。

像这样:

select FamilyID,max(Age) 
from 
(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'+'%')
group by FamilyID

顺便说一句:我不测试它。

于 2013-09-16T10:06:49.987 回答
0

查询是否可以使用 ms sql:

  SELECT a.FamilyID,
         a.Age,
         a.HeadOfHousehold
         a.HouseNo
  FROM(
  SELECT        hm.FamilyID,
                Year(GetDate())-Year(DOB) AS Age,
                FirstName+' '+LastName AS 'HeadOfHousehold',
                ff.HouseNo,
                ,ROW_NUMBER() OVER(PARTITION BY hm.FamilyId ORDER BY YEAR(GetDate()) - YEAR(DOB) DESC) as rnk
FROM HouseHoldMembers hm
LEFT OUTER JOIN FamilyFolder ff ON ff.FamilyID=hm.FamilyID
WHERE hm.FirstName = 'laxmi'
  AND hm.FamilyID LIKE 'alv'+'%')a
  WHERE a.rnk = 1
于 2013-09-16T10:26:05.547 回答