difference - 确定最早成员和最小成员之间的年龄差异的sql查询

  显示原文与译文双语对照的内容
0 0

我试图确定家庭中最小的孩子成员和年龄最老的孩子之间的年龄差异。 我可以拉出所需的所有成员数据,但是我不知道如何找到它们之间的区别。


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')

时间: 原作者:

0 0

你可以使用 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

FIDDLE

...