RANK ISSUE

  • NOTICE THE DESIRED OUTPUT ON THE BOTTOM.

    SELECT 'table1' TABLENAME

    ,'col1' COLNAME

    INTO #temp

    UNION ALL

    SELECT 'table1'

    ,'col2'

    UNION ALL

    SELECT 'table1'

    ,'col3'

    UNION ALL

    SELECT 'table2'

    ,'col1'

    UNION ALL

    SELECT 'table2'

    ,'col2'

    UNION ALL

    SELECT 'table3'

    ,'col1'

    SELECT *

    FROM #temp

    --DESIRED OUTPUT ID COL:

    --ID TABLENAMECOLNAME

    --1 table1 col1

    --2 table1 col2

    --3 table1 col3

    --1 table2 col1

    --2 table2 col2

    --1 table3 col1

  • Snargables (3/7/2013)


    NOTICE THE DESIRED OUTPUT ON THE BOTTOM.

    SELECT 'table1' TABLENAME

    ,'col1' COLNAME

    INTO #temp

    UNION ALL

    SELECT 'table1'

    ,'col2'

    UNION ALL

    SELECT 'table1'

    ,'col3'

    UNION ALL

    SELECT 'table2'

    ,'col1'

    UNION ALL

    SELECT 'table2'

    ,'col2'

    UNION ALL

    SELECT 'table3'

    ,'col1'

    SELECT *

    FROM #temp

    --DESIRED OUTPUT ID COL:

    --ID TABLENAMECOLNAME

    --1 table1 col1

    --2 table1 col2

    --3 table1 col3

    --1 table2 col1

    --2 table2 col2

    --1 table3 col1

    Okay, so what is the problem?

  • I need to generate the id col values. I don’t know how to make a rank or rownumber reset.

    notice it goes back to one. When it gets to a new table. I 've been playing w dense rank but don’t see one where it goes back to 1.

    am I making any sense? for some reason i'm having a really hard time explaining it

  • run the insert and select. Notice there is no id col being retruned. It's in the desired output. i need to find a way to generate that id patter in my select

  • select

    ID = row_number() over (partition by TABLENAME order by COLNAME),

    TABLENAME,

    COLNAME

    from

    #temp;

  • ah. row number w partition. nice. i was about to cross apply it to itself to get the darn id to reset. thanks

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply