订购两个 med 查询

Order two meged queries

我有 Customers table 我想提取最短和最长的客户名称

我是这样做的:

(SELECT TOP 1 CustomerName, LEN(CustomerName) AS LENGTH FROM Customers ORDER BY LEN(CustomerName) ASC, CustomerName ASC)
UNION
(SELECT TOP 1 CustomerName, LEN(CustomerName) AS LENGTH FROM Customers ORDER BY LEN(CustomerName) DESC, CustomerName ASC)

我得到了两个正确的结果,但现在我想按 LENGTH 列对它们进行排序。我试过这个:

SELECT * FROM
(
(SELECT TOP 1 CustomerName, LEN(CustomerName) AS LENGTH FROM Customers ORDER BY LEN(CustomerName) ASC, CustomerName ASC)
UNION
(SELECT TOP 1 CustomerName, LEN(CustomerName) AS LENGTH FROM Customers ORDER BY LEN(CustomerName) DESC, CustomerName ASC)
)
ORDER BY LEN(CustomerName) ASC

但它给了我这个错误:Syntax error in JOIN operation.

我该怎么做?

select customername,len(customername) as namelength
from (SELECT MAX(LEN(CustomerName)) AS MAXLENGTH, MIN(LEN(CustomerName)) AS MINLENGTH 
      FROM Customers) lens
join customers c on c.len(customername) = lens.maxlength or c.len(customername) = lens.minlength
order by namelength desc, customername