• Thanks! That makes it so that those of us helping have enough information to work with. I took the liberty of turning your sort logic into a table too so you can actually do something with it.

    create table #SortSomething

    (

    pcondition varchar(3),

    SortOrder int

    )

    insert #SortSomething(SortOrder, pcondition)

    select 1, 'AF' union all

    select 2, 'CA' union all

    select 3, 'CAD' union all

    select 4, 'AST' union all

    select 5, 'hf'

    ;with SortedData as

    (

    select cc.*, s.SortOrder, ROW_NUMBER() over(partition by ID order by s.SortOrder) as RowNum

    from CustomCode cc

    left join #SortSomething s on cc.pcondition = s.pcondition

    )

    select ID,

    case when RowNum = 1 AND SortOrder is not null then pcondition else null end as pcondition,

    case when RowNum = 1 AND SortOrder is not null then Pinten else null end as Pinten,

    case when RowNum > 1 then pcondition

    when RowNum = 1 AND SortOrder is null then scondition

    else null end as scondition,

    case when RowNum > 1 then Pinten

    when RowNum = 1 AND SortOrder is null then sinten

    else null end as sinten

    from SortedData sd

    order by sd.ID, RowNum

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/