You are on page 1of 1

What I want is this.

storeID ------1111 1111 1111 1131 1131 1131 1131 DealerCD -------980038 980041 980041 987789 983434 988232 988234 DealerName ---------John Bill Bill Greg Rudy Amy Chris DealerPermit ---------PAC NAC TTR PAC PAC NAC NAC Count ----2 2 2 4 4 4 4

What I want to do is partition by storeID and for each store I want to count the distinct dealers in that store. The query that I have is something like this """" select storeID, DealerCD,DealerName,DealerPermit, count(DealerCD) over (partition by storeid) from Mytable group by storeID, DealerCD, DealerName, DealerPermit."""""""" what I am getting is for storeID 1111 I am getting the count as 3 and for storeI D 1131 as 4. I want to get the count=2 for storeID = 1111 since there are only t wo distinct dealers working in that store. if i put in a count(distinct DealerCD), teradata throws out a error saying "dist inct is not permitted in a over() clause" Any help would be highly appreciated

SELECT T.STOREID, T.DEALERCD, T.DEALERNAME, T.DEALERPERMIT ,SUM(CASE WHEN T.PREVDEALERCD IS NULL OR T.PREVDEALERCD <> T.DEALERCD THEN 1 ELS E 0 END) OVER(PARTITION BY STOREID) FROM ( SELECT S.STOREID, S.DEALERCD, S.DEALERNAME, S.DEALERPERMIT ,MAX(DEALERCD) OVER(PARTITION BY STOREID ORDER BY DEALERCD ROWS BETWEEN 1 PRECED ING AND 1 PRECEDING) PREVDEALERCD FROM MYDATATBL S ) T ;

You might also like