我只想获得一个值,即最大值,但在我添加p.Name之后,这个MAX不会工作。
SELECT
p.Name, MAX(r.TaxRate) AS TaxRate
FROM
Sales.SalesTaxRate r
JOIN
Person.StateProvince p ON r.StateProvinceID = p.StateProvinceID
JOIN
Person.CountryRegion c ON c.CountryRegionCode = p.CountryRegionCode
WHERE
c.Name = 'Canada'
GROUP BY
p.Name
答案 0 :(得分:0)
使用RANK
:
SELECT
Name, TaxRate
FROM(
SELECT
p.Name,
r.TaxRate,
rn = RANK() OVER(ORDER BY r.TaxRate DESC)
FROM Sales.SalesTaxRate r
JOIN Person.StateProvince p
ON r.StateProvinceID = p.StateProvinceID
JOIN Person.CountryRegion c
ON c.CountryRegionCode = p.CountryRegionCode
WHERE
c.Name = 'Canada'
)t
WHERE rn = 1
使用TOP WITH TIES
SELECT TOP 1 WITH TIES
p.Name,
r.TaxRate
FROM Sales.SalesTaxRate r
JOIN Person.StateProvince p
ON r.StateProvinceID = p.StateProvinceID
JOIN Person.CountryRegion c
ON c.CountryRegionCode = p.CountryRegionCode
WHERE
c.Name = 'Canada'
ORDER BY r.TaxRate DESC