语法是有效的 SQL,但尚未在 SQL-Server 中实现。
尝试重写:
; WITH cte AS
( SELECT DISTINCT make, model
FROM VehicleModelYear
)
SELECT COUNT(*) AS total
FROM cte ;
或者:
; WITH cte AS
( SELECT COUNT(DISTINCT model) AS cnt
FROM VehicleModelYear
GROUP BY make
)
SELECT SUM(cnt) AS total
FROM cte ;
或者:
; WITH cte AS
( SELECT NULL AS n
FROM VehicleModelYear
GROUP BY make, model
)
SELECT COUNT(*) AS total
FROM cte ;
最后,修改了上面的第二个和第三个查询,没有子查询:
SELECT DISTINCT
SUM(COUNT(DISTINCT model)) OVER () AS total
FROM VehicleModelYear
GROUP BY make ;
SELECT DISTINCT COUNT(*) OVER () AS total
FROM VehicleModelYear
GROUP BY make, model ;
@Alexander Fedorenko的最新补充:
SELECT TOP (1)
SUM(COUNT(DISTINCT model)) OVER () AS total
FROM VehicleModelYear
GROUP BY make ;
SELECT TOP (1) COUNT(*) OVER () AS total
FROM VehicleModelYear
GROUP BY make, model ;
或者:
; WITH cte AS
( SELECT DENSE_RANK() OVER(ORDER BY make, model) AS dr
FROM VehicleModelYear
)
SELECT MAX(dr) AS total
FROM cte ;