sql查询以确定最旧成员和最年轻成员之间的年龄差异

时间:2015-04-13 14:40:07

标签: sql difference

我正在尝试确定家庭中最小的孩子成员和年龄最大的孩子成员之间的年龄差异。我能够提取我想要/需要的所有会员数据,但我不知道如何找到他们年龄之间的差异......我只是不知道从哪里开始:

SELECT     Household.Name,Member.RecStatus, Member.FirstName, Member.LastName,  
                      Member.SSN, Member.DOB, DATEDIFF(Year, Member.DOB, GETDATE()), RelationshipCat.RelationshipDesc, FinancialPlanner.LastName AS Expr1
FROM         Member AS Member INNER JOIN
                      Household AS Household ON Member.HouseholdID = Household.HouseholdID INNER JOIN
                      RelationshipCat AS RelationshipCat ON Member.Relationship = RelationshipCat.Relationship INNER JOIN
                      FinancialPlanner AS FinancialPlanner ON Household.FinancialPlannerID = FinancialPlanner.FinancialPlannerID
                      Where member.Relationship in ('2', '14', '47', '69', '55', '12', '70')

1 个答案:

答案 0 :(得分:1)

您可以使用MINMAX找到最年轻和最年长的男士:

CREATE TABLE TestAge ( Age INT );
INSERT INTO TestAge VALUES (12), (13), (18), (24), (42), (17);

SELECT MAX(Age) - MIN(Age) AS [Age Diff]
FROM TestAge

<强> SQL FIDDLE