• you can try this-

    ;WITH CTE AS

    (

    select op,imei, DENSE_RANK() over (order by op,imei) rankno

    FROM mastertab

    )

    select c.op, COUNT(distinct rankno) - 1 cnt

    FROM CTE c

    GROUP BY c.op

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/