Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
dhani
Posting Yak Master
132 Posts |
Posted - 2011-12-21 : 13:57:08
|
| Hello All,please help me in this scenario, i tried a lot but somehow seems to be my view is wrongInformation:Rownum MotherGrid ChildGrid PullarValue 2 1asAUSTRGIB 1asAUSTRaus MTHR 3 1asAUSTRGIB 1asAUSTRbal CHILD 4 1asAUSTRGIB 1asAUSTRhld MTHR 5 1auCLEMEGIB 1auCLEMEbal CHILD 6 1auCLEMEGIB 1auCLEMEbla MTHR 7 1auCLEMEGIB 1auCLEMEcgl MTHR 8 1auCLEMEGIB 1auCLEMEche MTHR 9 1BBDO_AFFIL 1auCLEME NULL 10 1BBDO_AFFIL 1coCOLOM NULL 11 7chChinabas 1chChinkinda MTHR 12 7chChinabas 1chChinBunda MTHR 13 7chChinabas 1chChinDamma MTHR 14 8AusDimmy 7chChinabas null 15 9abChandGIB 8AusDimmy null 16 9abChandGIB 8AusLitteur CHILD 17 9abChandGIB 8AusKroneuer MTHR in the above roe_num is just for explanation purpose1)MotherGrid will have group of values, in each group it may have one Pullarvalue=Child, if this is the case then outputcolumn(ResultCol_Output) will beCgildGrid column value for the entire group. (for example 1st set & 2nd set above excel)2)if the given group MotherGrid value is not ends with GIB then you may have leveldown parentso 7chchinabas has all pullarvalues to be "mthr" thereis no "child" so check any exist child is equal to parent (ie., 11,12,13 parent equal to 14 child, since 14 parent not ends with GIB then next 14 parent exist in 15 child also 15th parent ends with GIB so it all these rows will have childgrid of child value which is 8auslitteur).3) if no pullar value given & there no parent = child (i mean level down like we did in step 2) then just replace with what ever child grid value is or simply null also fineplease assit me,expected outcome will beRownum MotherGrid ChildGrid PullarValue ResultCol_Output2 1asAUSTRGIB 1asAUSTRaus MTHR 1asAUSTRbal3 1asAUSTRGIB 1asAUSTRbal CHILD 1asAUSTRbal4 1asAUSTRGIB 1asAUSTRhld MTHR 1asAUSTRbal5 1auCLEMEGIB 1auCLEMEbal CHILD 1auCLEMEbal6 1auCLEMEGIB 1auCLEMEbla MTHR 1auCLEMEbal7 1auCLEMEGIB 1auCLEMEcgl MTHR 1auCLEMEbal8 1auCLEMEGIB 1auCLEMEche MTHR 1auCLEMEbal9 1BBDO_AFFIL 1auCLEME NULL NULL10 1BBDO_AFFIL 1coCOLOM NULL NULL11 7chChinabas 1chChinkinda MTHR 8AusLitteur12 7chChinabas 1chChinBunda MTHR 8AusLitteur13 7chChinabas 1chChinDamma MTHR 8AusLitteur14 8AusDimmy 7chChinabas null 8AusLitteur15 9abChandGIB 8AusDimmy null 8AusLitteur16 9abChandGIB 8AusLitteur CHILD 8AusLitteur17 9abChandGIB 8AusKroneuer MTHR 8AusLitteur---> sql code script Starts Herecreate table MyParTable( MotherGrid varchar(40), ChildGrid varchar(40), PullarValue varchar(40), ResultCol_Output varchar(40)) insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('1asAUSTRGIB', '1asAUSTRaus', 'MTHR')insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('1asAUSTRGIB', '1asAUSTRbal', 'CHILD')insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('1asAUSTRGIB', '1asAUSTRhld', 'MTHR')insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('1auCLEMEGIB', '1auCLEMEbal', 'CHILD')insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('1auCLEMEGIB', '1auCLEMEbla', 'MTHR')insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('1auCLEMEGIB', '1auCLEMEcgl', 'MTHR')insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('1auCLEMEGIB', '1auCLEMEche', 'MTHR')insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('1BBDO_AFFIL', '1auCLEME', NULL)insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('1BBDO_AFFIL', '1coCOLOM', NULL)insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('7chChinabas', '1chChinkinda', 'MTHR')insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('7chChinabas', '1chChinBunda', 'MTHR')insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('7chChinabas', '1chChinDamma', 'MTHR')insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('8AusDimmy', '7chChinabas', null)insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('9abChandGIB', '8AusDimmy', null)insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('9abChandGIB', '8AusLitteur', 'CHILD')insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('9abChandGIB', '8AusKroneuer', 'MTHR')---> sql code script Ends HereThanks in advanceThanks a ton in advancei tried a lot, i need another viewplease ALLL |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-22 : 06:20:16
|
something likecreate table MyParTable(MotherGrid varchar(40),ChildGrid varchar(40),PullarValue varchar(40),ResultCol_Output varchar(40))insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('1asAUSTRGIB', '1asAUSTRaus', 'MTHR')insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('1asAUSTRGIB', '1asAUSTRbal', 'CHILD')insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('1asAUSTRGIB', '1asAUSTRhld', 'MTHR')insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('1auCLEMEGIB', '1auCLEMEbal', 'CHILD')insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('1auCLEMEGIB', '1auCLEMEbla', 'MTHR')insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('1auCLEMEGIB', '1auCLEMEcgl', 'MTHR')insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('1auCLEMEGIB', '1auCLEMEche', 'MTHR')insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('1BBDO_AFFIL', '1auCLEME', NULL)insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('1BBDO_AFFIL', '1coCOLOM', NULL)insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('7chChinabas', '1chChinkinda', 'MTHR')insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('7chChinabas', '1chChinBunda', 'MTHR')insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('7chChinabas', '1chChinDamma', 'MTHR')insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('8AusDimmy', '7chChinabas', null)insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('9abChandGIB', '8AusDimmy', null)insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('9abChandGIB', '8AusLitteur', 'CHILD')insert into MyParTable(MotherGrid ,ChildGrid ,PullarValue) values ('9abChandGIB', '8AusKroneuer', 'MTHR')UPDATE tSET t.ResultCol_Output= t1.ResultCol_OutputFROM MyParTable tCROSS APPLY (SELECT TOP 1 ChildGrid AS ResultCol_Output FROM MyParTable WHERE MotherGrid=t.MotherGrid AND PullarValue='CHILD' ) t1 ;WITH CTEAS(SELECT MotherGrid,ChildGrid,CAST( MotherGrid AS varchar(max)) AS path,1 AS Level,t.PullarValueFROM MyParTable tUNION ALLSELECT t.MotherGrid,t.ChildGrid, c.path+ '/' + t.MotherGrid ,Level +1 ,t.PullarValueFROM CTE cINNER JOIN MyParTable tON t.ChildGrid = c.MotherGrid) UPDATE tSET t.ResultCol_Output = x.ChildGridFROM MyParTable tCROSS APPLY( SELECT DISTINCT t1.ChildGrid FROM (SELECT LEFT([path],CHARINDEX('/',[path]+'/')-1) AS val,* FROM CTE) c INNER JOIN (SELECT LEFT([path],CHARINDEX('/',[path]+'/')-1) AS val,MAX([level]) AS lvl FROM CTE GROUP BY LEFT([path],CHARINDEX('/',[path]+'/')-1))c2 ON c2.lvl=c.[Level] AND c2.val= c.val INNER JOIN MyParTable t1 ON t1.MotherGrid = c.MotherGrid AND t1.PullarValue='CHILD' WHERE c.Level>1 AND t.MotherGrid=c.val )x WHERE ResultCol_Output IS NULL SELECT * FROM MyParTable output-----------------------------------------------------MotherGrid ChildGrid PullarValue ResultCol_Output1asAUSTRGIB 1asAUSTRaus MTHR 1asAUSTRbal1asAUSTRGIB 1asAUSTRbal CHILD 1asAUSTRbal1asAUSTRGIB 1asAUSTRhld MTHR 1asAUSTRbal1auCLEMEGIB 1auCLEMEbal CHILD 1auCLEMEbal1auCLEMEGIB 1auCLEMEbla MTHR 1auCLEMEbal1auCLEMEGIB 1auCLEMEcgl MTHR 1auCLEMEbal1auCLEMEGIB 1auCLEMEche MTHR 1auCLEMEbal1BBDO_AFFIL 1auCLEME NULL NULL1BBDO_AFFIL 1coCOLOM NULL NULL7chChinabas 1chChinkinda MTHR 8AusLitteur7chChinabas 1chChinBunda MTHR 8AusLitteur7chChinabas 1chChinDamma MTHR 8AusLitteur8AusDimmy 7chChinabas NULL 8AusLitteur9abChandGIB 8AusDimmy NULL 8AusLitteur9abChandGIB 8AusLitteur CHILD 8AusLitteur9abChandGIB 8AusKroneuer MTHR 8AusLitteur------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|