遇到SQL Server问题

时间:2016-03-09 03:54:37

标签: sql-server

截至此日期,最年长的人的名字是什么?包括他或她的年龄,人名,电话号码和头衔。注意:只显示最老的人的姓名。

我一直在尝试使用SQL Server回答这个问题,我不理解它。

这是我现在的代码:

Use AdventureWorks2014
Go

SELECT 
    p.FirstName, LastName, Title
    Employee_1, JobTitle, BirthDate
FROM 
    Person.Person AS p
INNER JOIN 
    HumanResources.Employee AS Employee_1 ON p.BusinessEntityID = Employee_1.BusinessEntityID 
WHERE
    Employee_1 BirthDate * (SELECT % BirthDate AS [age] 
                            FROM HumanResources.Employee))

3 个答案:

答案 0 :(得分:2)

最早的出生日期是什么?

SELECT MIN(BirthDate) FROM Person.Person

谁有这个生日?

SELECT p.FirstName, LastName, Title
   Employee_1, JobTitle, BirthDate
FROM Person.Person AS p 
WHERE P.birthdate = (SELECT MIN(BirthDate) FROM Person.Person)

他们的年龄是多少年?

SELECT p.FirstName, LastName, Title
   Employee_1, JobTitle, BirthDate,
   DATEDIFF(YEAR,BirthDate,GETDATE()) AS Age
FROM Person.Person AS p 
WHERE P.birthdate = (SELECT MIN(BirthDate) FROM Person.Person)

答案 1 :(得分:2)

简单TOP查询应该适合您:

SELECT top 1 p.FirstName, LastName, Title
   Employee_1, JobTitle, BirthDate
FROM Person.Person AS p
INNER JOIN HumanResources.Employee AS Employee_1
ON p.BusinessEntityID = Employee_1.BusinessEntityID 
ORDER BY BirthDate

答案 2 :(得分:0)

在AdventureWorks2014中,有两个表有生日...所以找到它们中最老的

 SELECT TOP 1 FirstName, LastName 
 FROM (
   SELECT FirstName, LastName, BirthDate
   FROM ProspectiveBuyer

   UNION ALL

   SELECT FirstName, LastName, BirthDate
   FROM DimEmployee
) AS all_people
ORDER BY BirthDate ASC