構文は有効な 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 ;
最後に、上記の 2 番目と 3 番目のクエリは、サブクエリなしで変更されています。
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 ;