Something like this?--sample datadeclare @TBL_DontAllowProductToEnroll table (DAlow_ID int, Orp_DAlow_ProductId int, DAlow_Orp_Id int, pln_DAlow_PlanTypeId int, Dallow_Type char(1))insert @TBL_DontAllowProductToEnroll select 1, 403, 404, 403, 'D'union all select 2, 798, 793, 798, 'D'union all select 3, 798, 792, 798, 'D'union all select 4, 798, 788, 798, 'D'union all select 5, 838, 796, 838, 'D'union all select 6, 838, 790, 838, 'D'--query; with a as (select *, row_number() over (partition by Orp_DAlow_ProductId order by DAlow_ID desc) as row from @TBL_DontAllowProductToEnroll)select DAlow_ID as MD_ID, Orp_DAlow_ProductId as Orp_MD_SourceProductId, DAlow_Orp_Id as Orp_MD_DestProductID, '=' as MD_MathOperator, case row when 1 then null else 'OR' end as MD_LogOperatorfrom a order by MD_ID/* resultsMD_ID Orp_MD_SourceProductId Orp_MD_DestProductID MD_MathOperator MD_LogOperator----------- ---------------------- -------------------- --------------- --------------1 403 404 = NULL2 798 793 = OR3 798 792 = OR4 798 788 = NULL5 838 796 = OR6 838 790 = NULL*/
Ryan Randall Solutions are easy. Understanding the problem, now, that's the hard part.