• kapil_kk (7/20/2013)


    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

    Try with this data and see what happens..

    drop table mastertab

    CREATE table mastertab(

    op CHAR(2),

    msisdn VARCHAR (12),

    imei VARCHAR (20)

    );

    INSERT INTO mastertab

    (op,msisdn,imei)

    SELECT 'aa','0191','111222333' UNION ALL

    --SELECT 'aa','0191','111222444' UNION ALL

    SELECT 'aa','0192','111222333' UNION ALL

    --SELECT 'aa','0192','111222444' UNION ALL

    SELECT 'aa','0192','111222555' UNION ALL

    --SELECT 'aa','0193','111222333' UNION ALL

    SELECT 'aa','0193','111222334' UNION ALL --added

    SELECT 'aa','0193','111222335' UNION ALL --added

    SELECT 'aa','0193','111222336' UNION ALL --added

    SELECT 'aa','0193','111222337' UNION ALL --added

    SELECT 'aa','0193','111222338' UNION ALL --added

    SELECT 'bb','0171','222222333' UNION ALL

    SELECT 'bb','0171','222222444' UNION ALL

    SELECT 'bb','0172','222222444' UNION ALL

    SELECT 'cc','0152','333222444'

    ;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

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)