sql查询没有显示我的要求的正确输出

时间:2013-09-16 09:54:52

标签: sql

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的最大年龄的查询retreival(选择该familyid的最大年龄的familyid,我希望看起来的输出如下所示)。请给出解决方案

FamilyID          age          HeadOfHousehold              HouseNo  
ALV01L11034A       74                 arpana                   34
ALV01L11059A        62                baby                      63

表结构是: 对于家庭成员表 - >

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

for familyfolder table->

familyid varchar(50) as primarykey, houseno varchar(50) as null

提前致谢

3 个答案:

答案 0 :(得分: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
顺便说一句:我不测试它。

答案 1 :(得分: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

答案 2 :(得分: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