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 |
bamabala
Starting Member
11 Posts |
Posted - 2011-12-02 : 05:05:22
|
hi,I have to find all the parents related a batch and also its child.Here is the scenario,In my table i have two columnsBatchID OldBatchID8172 80838642 81728642 8265Now i have only the batchid 8172. by passing this as input i need to find all the above values.I tried like thisWITH Batch_CTE AS ( (SELECT DISTINCT TOH.[BatchID], TOH.[OldBatchID], level = 0FROM [BRD_IQD_PDB].[dbo].[tblIQTempOrderHeader] TOH INNER JOIN #BatchIDs B ON TOH.OldBatchID = B.BatchID )UNION ALLSELECT TOH.[BatchID], TOH.[OldBatchID], level = level + 1FROM [BRD_IQD_PDB].[dbo].[tblIQTempOrderHeader] TOH INNER JOIN Batch_CTE bcte ON TOH.[BatchID] = bcte.[OldBatchID])SELECT * FROM Batch_CTE But i am getting only the below result.BatchID OldBatchID8172 80838642 8172Kindly help me to solve this issue. |
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-12-02 : 05:39:25
|
As you only want to go down one level in the hierarchy then (actually no need ofr a cte butdeclare @t table (BatchID int, OldBatchID int)insert @t select 8172, 8083insert @t select 8642, 8172insert @t select 8642, 8265declare @BatchID int = 8172;with cte as(select BatchID, OldBatchID from @t where BatchID = @BatchIDunionselect BatchID, OldBatchID from @t where OldBatchID = @BatchID)select * from @t where BatchID in (select BatchID from cte)BatchID OldBatchID----------- -----------8172 80838642 81728642 8265==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
bamabala
Starting Member
11 Posts |
Posted - 2011-12-02 : 06:59:45
|
thanks for your reply.But the issue is the batchid 8642 has following as oldbatchids7265,7772,7839,8172,8233,8234,8235,8238These oldbatchids may be old batch to some other batches or it may have old batchidSo i need to find out all the batches and oldbatches.BatchID OldBatchID8172 80838083 If it has oldbacth8642 81728642 72658642 77727265 If it has oldbacthBatch 8642I have to get the result like this. |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-12-02 : 07:05:08
|
Not sure what you want.Pass in 8172get all the entries with 8172 as the BatchIDget all the BatchIDs with 8172 as the OldBatchIDget all the entries with those BatchIDsget all the BatchIDs with those BatchIDs as the OldBatchID...==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
bamabala
Starting Member
11 Posts |
Posted - 2011-12-02 : 07:14:35
|
I have need to pass 8172 as input and i need to get its oldbatchids and batchids and their corresponding batchids and oldnatchids and in turn.I am not sure am i clarified my requirement. |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-12-02 : 07:15:44
|
maybedeclare @t table (BatchID int, OldBatchID int)insert @t select 8172, 8083insert @t select 8642, 8172insert @t select 8642, 7265insert @t select 8642, 7772insert @t select 7772, 0001insert @t select 0001, 8172declare @BatchID int = 8172;with cte as(select BatchID, OldBatchID, seq = 0 from @t where BatchID = @BatchIDunionselect BatchID, OldBatchID, seq = 1 from @t where OldBatchID = @BatchIDunion allselect t.BatchID, t.OldBatchID, seq = seq+1 from cte join @t t on cte.OldBatchID = t.BatchID)select * from @t where BatchID in (select BatchID from cte)BatchID OldBatchID----------- -----------8172 80838642 81728642 72658642 77721 8172==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
bamabala
Starting Member
11 Posts |
Posted - 2011-12-02 : 07:31:42
|
I have already tried the above query, but the batch 7265 has an oldbatch 7773. So the result should contain the below row too. Batch OldBatchID7265 7773 |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-12-02 : 07:59:52
|
Can't see thatdeclare @t table (BatchID int, OldBatchID int)insert @t select 8172, 8083insert @t select 8642, 8172insert @t select 8642, 7265insert @t select 8642, 7772insert @t select 7772, 0001insert @t select 0001, 8172==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
bamabala
Starting Member
11 Posts |
Posted - 2011-12-02 : 08:04:05
|
I have checked it in my original table.declare @t table (BatchID int, OldBatchID int)insert @t select 8172, 8083insert @t select 8642, 8172insert @t select 8642, 7265insert @t select 8642, 7772insert @t select 7772, 0001insert @t select 0001, 8172insert @t select 7265, 7773check this its not giving the last record in the result. |
|
|
bamabala
Starting Member
11 Posts |
Posted - 2011-12-06 : 07:14:38
|
Kindly Can any one help to solve this issue... |
|
|
bamabala
Starting Member
11 Posts |
Posted - 2011-12-08 : 06:28:11
|
Hi,I have found one solution for the above problem and it works fine.declare @t table (BatchID int, OldBatchID int)insert @t select 8172, 8083insert @t select 8642, 8172insert @t select 8642, 7265insert @t select 8642, 7772insert @t select 7772, 0001insert @t select 7265, 0002insert @t select 0001, 8172declare @BatchID int = 8172;WITH Batch_CTE AS ( ( SELECT [BatchID], [OldBatchID] FROM @t WHERE [BatchID] = @BatchID AND [OldBatchID] IS NOT NULL UNION SELECT [BatchID], [OldBatchID] FROM @t WHERE [OldBatchID] = @BatchID ) UNION ALL SELECT T.[BatchID], T.[OldBatchID] FROM @t T INNER JOIN Batch_CTE bcte ON T.[OldBatchID] = bcte.[BatchID] WHERE T.[OldBatchID] IS NOT NULL ), Batch_CTE1 AS ( SELECT T.[BatchID], T.[OldBatchID] FROM @t T WHERE EXISTS( SELECT 1 FROM Batch_CTE BC WHERE BC.[BatchID] = T.[BatchID] ) --TOH.BatchID IN(SELECT BatchID FROM Batch_CTE) UNION ALL SELECT T.[BatchID], T.[OldBatchID] FROM @t T INNER JOIN Batch_CTE1 bcte ON bcte.[OldBatchID] = T.[BatchID] WHERE T.[OldBatchID] IS NOT NULL ) SELECT DISTINCT [BatchID],[OldBatchID] FROM Batch_CTE1 ORDER BY [OldBatchID] |
|
|
bamabala
Starting Member
11 Posts |
Posted - 2011-12-08 : 07:09:54
|
declare @t table (BatchID int, OldBatchID int)insert @t select 8172, 8083insert @t select 8642, 8172insert @t select 8642, 7265insert @t select 8642, 7772insert @t select 7772, 0001insert @t select 7265, 0002insert @t select 0003, 7265insert @t select 0003, 7266insert @t select 0001, 8172If i am having the above values in my table i could not get the correct answer.Kindly help to work out this. |
|
|
bamabala
Starting Member
11 Posts |
Posted - 2011-12-12 : 02:23:40
|
i have found a soltion for this.declare @t table (BatchID int, OldBatchID int)insert @t select 8172, 8083insert @t select 8642, 8172insert @t select 8642, 7265insert @t select 8642, 7772insert @t select 7772, 0001insert @t select 7265, 0002insert @t select 0003, 7265insert @t select 0003, 7266insert @t select 0001, 8172declare @BatchID int = 8172BEGINDECLARE @rows INT = 0 CREATE TABLE #reached ( nodeID INT ) CREATE UNIQUE CLUSTERED INDEX nodeID1 ON #reached(nodeID) WITH IGNORE_DUP_KEY INSERT INTO #reached VALUES (@BatchID) SET @rows = @@ROWCOUNT WHILE @rows > 0 BEGIN INSERT INTO #reached SELECT DISTINCT BatchID FROM @t AS e INNER JOIN #reached AS p ON e.OldBatchID = p.nodeID SET @rows = @@ROWCOUNT INSERT INTO #reached SELECT DISTINCT OldBatchID FROM @t AS e INNER JOIN #reached AS p ON e.BatchID = p.nodeID SET @rows = @rows + @@ROWCOUNT END SELECT BatchID, OldBatchID FROM @t t1 INNER JOIN #reached r ON t1.BatchID = r.nodeID UNION SELECT BatchID, OldBatchID FROM @t t1 INNER JOIN #reached r ON t1.OldBatchID = r.nodeID DROP TABLE #reached; END |
|
|
|
|
|
|
|