Author |
Topic |
samir.first
Starting Member
34 Posts |
Posted - 2013-09-18 : 04:38:20
|
I need Query To Update ParentId Depends on Code After Updatethe Table is:ID Code ParentID1 11 Null2 1101 13 1102 14 1103 15 12 NULL6 1201 57 1202 58 120201 7else .........I need Best Query |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-09-18 : 04:47:51
|
[code]DECLARE @Sample TABLE ( ID INT NOT NULL, Code VARCHAR(100) NOT NULL, ParentID INT );INSERT @Sample ( ID, Code )VALUES (1, '11'), (2, '1101'), (3, '1102'), (4, '1103'), (5, '12'), (6, '1201'), (7, '1202'), (8, '120201');-- SwePesoUPDATE sSET s.ParentID = f.IDFROM @Sample AS sCROSS APPLY ( SELECT MIN(x.ID) FROM @Sample AS x WHERE x.Code = LEFT(s.Code, LEN(s.Code) - 2) ) AS f(ID);SELECT *FROM @Sample;[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
sigmas
Posting Yak Master
172 Posts |
Posted - 2013-09-18 : 05:13:44
|
[code]UPDATE ASET ParentID = B.IDFROM @Sample AJOIN @Sample BON LEFT(A.code, LEN(A.code) - 2) = B.code[/code] |
|
|
samir.first
Starting Member
34 Posts |
Posted - 2013-09-18 : 07:31:48
|
thank you SwePeso but number (2) in ( LEN(s.Code) - 2)) is declare not static111101110100011101000100001else |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-09-18 : 07:51:01
|
[code]-- Prepare sample dataDECLARE @Sample TABLE ( ID INT NOT NULL, Code VARCHAR(100) NOT NULL, ParentID INT );INSERT @Sample ( ID, Code )VALUES (1, '11'), (2, '1101'), (9, '11010001'), (0, '1101000100001'), (3, '1102'), (4, '1103'), (5, '12'), (6, '1201'), (7, '1202'), (8, '120201');-- SwePesoUPDATE sSET s.ParentID = f.IDFROM @Sample AS sCROSS APPLY ( SELECT TOP(1) x.ID FROM @Sample AS x WHERE s.Code LIKE x.Code + '%' AND LEN(s.Code) > LEN(x.Code) ORDER BY LEN(x.Code) DESC ) AS f(ID);-- Display resultSELECT *FROM @SampleORDER BY Code;[/code] Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
sigmas
Posting Yak Master
172 Posts |
Posted - 2013-09-18 : 08:35:41
|
[code]update sset parentID = (select top 1 id from @sample where code < s.code and len(code) < len(s.code) order by code desc)from @sample s;[/code] |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-09-18 : 10:12:30
|
Sigmas and mine solution behave differently.-- Prepare sample dataDECLARE @Sample TABLE ( ID INT NOT NULL, Code VARCHAR(100) NOT NULL, ParentID INT, Sigmas INT );INSERT @Sample ( ID, Code )VALUES (1, '11'), (2, '1101'), (9, '11010001'), (0, '1101000100001'), (3, '1102'), (4, '1103'), (5, '12'), (6, '1201'), (7, '1202'), (8, '9999');-- SwePesoUPDATE sSET s.ParentID = f.IDFROM @Sample AS sCROSS APPLY ( SELECT TOP(1) x.ID FROM @Sample AS x WHERE s.Code LIKE x.Code + '%' AND LEN(s.Code) > LEN(x.Code) ORDER BY LEN(x.Code) DESC ) AS f(ID);UPDATE sSET Sigmas = ( SELECT TOP(1) x.ID FROM @Sample AS x WHERE x.Code < s.Code AND LEN(x.Code) < LEN(s.Code) ORDER BY x.Code DESC )FROM @Sample AS s;-- Display resultSELECT *FROM @SampleORDER BY Code; Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
sigmas
Posting Yak Master
172 Posts |
Posted - 2013-09-18 : 12:28:44
|
Base on sample data posted by open poster the roots node are two characters.You added a node called root that is out of the root range! an orphan node ('9999'), the orphan node needs parent node ('99')If you add following row to the table my solution will work fine.INSERT INTO @Sample (ID, code) VALUES (99, '99'); |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2013-09-18 : 14:10:59
|
Yes, they do. But we can't know. Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
sigmas
Posting Yak Master
172 Posts |
Posted - 2013-09-19 : 11:38:04
|
So far we have not received feedback from the OP. |
|
|
|
|
|