네크 로맨싱 :
DENSE_RANK를 통해 MAX로 PARTITION BY보다 COUNT DISTINCT를 에뮬레이션하는 것은 비교적 간단합니다.
;WITH baseTable AS
(
SELECT 'RM1' AS RM, 'ADR1' AS ADR
UNION ALL SELECT 'RM1' AS RM, 'ADR1' AS ADR
UNION ALL SELECT 'RM2' AS RM, 'ADR1' AS ADR
UNION ALL SELECT 'RM2' AS RM, 'ADR2' AS ADR
UNION ALL SELECT 'RM2' AS RM, 'ADR2' AS ADR
UNION ALL SELECT 'RM2' AS RM, 'ADR3' AS ADR
UNION ALL SELECT 'RM3' AS RM, 'ADR1' AS ADR
UNION ALL SELECT 'RM2' AS RM, 'ADR1' AS ADR
UNION ALL SELECT 'RM3' AS RM, 'ADR1' AS ADR
UNION ALL SELECT 'RM3' AS RM, 'ADR2' AS ADR
)
,CTE AS
(
SELECT RM, ADR, DENSE_RANK() OVER(PARTITION BY RM ORDER BY ADR) AS dr
FROM baseTable
)
SELECT
RM
,ADR
,COUNT(CTE.ADR) OVER (PARTITION BY CTE.RM ORDER BY ADR) AS cnt1
,COUNT(CTE.ADR) OVER (PARTITION BY CTE.RM) AS cnt2
,MAX(CTE.dr) OVER (PARTITION BY CTE.RM ORDER BY CTE.RM) AS cntDistEmu
FROM CTE
참고 :
이는 해당 필드가 NON 허용 필드가 아니라고 가정합니다.
필드에 하나 이상의 NULL 항목이있는 경우 1을 빼야합니다.
COUNT
로ORDER BY
대신PARTITION BY
2008 년에 잘못 정의되어 나는 당신이 전혀 그것을 가지고시키는 것 놀라게하고있다. 당 문서 , 당신은 허용하지 않을ORDER BY
집계 함수에 대한.