473,503 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need to go down path to find if everything is settled (recursive possibly)

I have the following table.
GO
/****** Object: Table [dbo].[itTransactionProcess] Script Date:
05/01/2007 10:42:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[itTransactionProcess](
[TransactionID] [int] IDENTITY(1,1) NOT NULL,
[LotNumber] [int] NOT NULL,
[CurrentProcessStepID] [int] NOT NULL,
[NextProcessStepID] [int] NULL,
[CategoryID] [int] NULL,
[ProductID] [int] NULL,
[ProductVariantID] [int] NULL,
[ParentTransactionID] [int] NULL,
[TransactionDateEntered] [datetime] NULL,
[TransactionDateExit] [datetime] NULL,
[Settlement] [money] NULL,
[Completed] [int] NULL,
CONSTRAINT [PK_itTransactionProcess] PRIMARY KEY CLUSTERED
(
[TransactionID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY
= OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

Sample data is as follows
Basically what I need to do is return the lotid where all path have a
settlement date.

this is my current procedure

/****** Object: StoredProcedure [dbo].
[getPendingSettlementDetails] Script Date: 05/01/2007 10:47:47
******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[getPendingSettlementDetails]
AS

declare @LotNumbersTable table(LotNumber int)
insert into @LotNumbersTable EXEC GetPendingSettlementsLotNumbers

Declare @ResultsTable table(LotNumber int, Company varchar(150),
Contact varchar(150), DateReceived datetime, DateComplete datetime,
SettlementLength int)
Declare @LotNumber int

Declare @DateRecieved datetime, @DateComplete datetime
Declare @NumberOfDaysForSettlement int
Declare @Company varchar(150)
Declare @Contact varchar(150)

select @LotNumber = min(LotNumber) from @LotNumbersTable
while @LotNumber is not null begin
Select @DateRecieved = min(TransactionDateEntered) from
itTransactionProcess where LotNumber = @LotNumber
Select @DateComplete = max(TransactionDateExit) from
itTransactionProcess where LotNumber = @LotNumber and Settlement is
not null
SET @NumberOfDaysForSettlement = DATEDIFF(DAY, @DateRecieved,
@DateComplete)
Select @Company = Company from SP_Active_Lot_Deliveries where LotID =
@LotNumber
Select @Contact = ContactName from SP_Active_Lot_Deliveries where
LotID = @LotNumber
INSERT INTO @ResultsTable (LotNumber, DateReceived, DateComplete,
SettlementLength, Company, Contact) Values
(@LotNumber, @DateRecieved, @DateComplete,
@NumberOfDaysForSettlement, @company, @contact)
select @LotNumber = min(LotNumber) from @LotNumbersTable where
LotNumber @LotNumber
end
Select * From @ResultsTable where SettlementLength is not null

here is sample data
"TransactionID","LotNumber","CurrentProcessStepID" ,"NextProcessStepID","CategoryID","ProductID","Pro ductVariantID","ParentTransactionID","TransactionD ateEntered","TransactionDateExit","Settlement","Co mpleted"
"628","47","1","2","5","","","","","2007-05-01
10:23:15.747000000","",""
"629","47","1","3","17","","","","","2007-05-01
10:23:15.747000000","0.25",""
"630","47","1","4","34","","","","","2007-05-01
10:23:15.747000000","-0.15",""
"631","47","1","3","38","","","","","2007-05-01
10:23:15.747000000","-0.15",""
"632","47","1","4","33","","","","","2007-05-01
10:23:15.747000000","-0.35",""
"633","47","1","3","15","","","","","2007-05-01
10:23:15.747000000","10",""
"634","47","2","3","86","","","628","2007-05-01
10:23:15.747000000","2007-05-01 10:32:41.320000000","-0.35",""
"635","47","3","","17","","","629","2007-05-01
10:23:15.747000000","","",""
"636","47","4","","34","","","630","2007-05-01
10:23:15.747000000","","",""
"637","47","3","","38","","","631","2007-05-01
10:23:15.747000000","","",""
"638","47","4","","33","","","632","2007-05-01
10:23:15.747000000","","",""
"639","47","3","","15","","","633","2007-05-01
10:23:15.747000000","","",""
"640","47","2","3","85","","","628","2007-05-01
10:24:47.983000000","2007-05-01 10:32:41.320000000","0.05",""
"641","47","2","4","88","","","628","2007-05-01
10:24:56.343000000","2007-05-01 10:32:41.333000000","0.8",""
"642","47","2","4","9","","","628","2007-05-01
10:25:07.517000000","2007-05-01 10:32:41.333000000","-0.15",""
"643","47","2","4","100","","","628","2007-05-01
10:25:22.470000000","2007-05-01 10:32:41.333000000","-0.35",""
"644","47","2","4","90","","","628","2007-05-01
10:25:44.297000000","2007-05-01 10:32:41.333000000","-0.35",""
"645","47","2","4","12","","","628","2007-05-01
10:25:59.347000000","2007-05-01 10:32:41.333000000","-0.15",""
"646","47","2","4","26","","","628","2007-05-01
10:26:12.610000000","2007-05-01 10:32:41.333000000","-0.35",""
"647","47","2","3","94","","","628","2007-05-01
10:26:29.523000000","2007-05-01 10:32:41.333000000","-3",""
"648","47","2","3","95","","","628","2007-05-01
10:26:47.323000000","2007-05-01 10:32:41.333000000","-0.35",""
"649","47","2","3","38","","","628","2007-05-01
10:27:01.450000000","2007-05-01 10:32:41.333000000","-0.15",""
"650","47","2","4","33","","","628","2007-05-01
10:27:15.533000000","2007-05-01 10:32:41.333000000","-0.35",""
"651","47","2","4","34","","","628","2007-05-01
10:27:33.767000000","2007-05-01 10:32:41.333000000","-0.15",""
"652","47","2","3","96","","","628","2007-05-01
10:27:46.850000000","2007-05-01 10:32:41.350000000","-0.35",""
"653","47","2","3","97","","","628","2007-05-01
10:28:00.917000000","2007-05-01 10:32:41.350000000","0.05",""
"654","47","2","4","36","","","628","2007-05-01
10:28:10.813000000","2007-05-01 10:32:41.350000000","-15",""
"655","47","2","4","37","","","628","2007-05-01
10:28:25.347000000","2007-05-01 10:32:41.350000000","0.35",""
"656","47","2","3","98","","","628","2007-05-01
10:28:36.917000000","2007-05-01 10:32:41.350000000","-0.35",""
"694","47","2","10","26","","","628","2007-05-01
10:32:17.170000000","2007-05-01 10:32:41.350000000","",""
"695","47","2","10","35","","","628","2007-05-01
10:32:27.883000000","2007-05-01 10:32:41.350000000","45",""
"696","47","3","","86","","","634","2007-05-01
10:32:41.320000000","","",""
"697","47","3","","85","","","640","2007-05-01
10:32:41.333000000","","",""
"698","47","4","","88","","","641","2007-05-01
10:32:41.333000000","","",""
"699","47","4","","9","","","642","2007-05-01
10:32:41.333000000","","",""
"700","47","4","","100","","","643","2007-05-01
10:32:41.333000000","","",""
"701","47","4","","90","","","644","2007-05-01
10:32:41.333000000","","",""
"702","47","4","","12","","","645","2007-05-01
10:32:41.333000000","","",""
"703","47","4","","26","","","646","2007-05-01
10:32:41.333000000","","",""
"704","47","3","","94","","","647","2007-05-01
10:32:41.333000000","","",""
"705","47","3","","95","","","648","2007-05-01
10:32:41.333000000","","",""
"706","47","3","","38","","","649","2007-05-01
10:32:41.333000000","","",""
"707","47","4","","33","","","650","2007-05-01
10:32:41.333000000","","",""
"708","47","4","","34","","","651","2007-05-01
10:32:41.333000000","","",""
"709","47","3","","96","","","652","2007-05-01
10:32:41.350000000","","",""
"710","47","3","","97","","","653","2007-05-01
10:32:41.350000000","","",""
"711","47","4","","36","","","654","2007-05-01
10:32:41.350000000","","",""
"712","47","4","","37","","","655","2007-05-01
10:32:41.350000000","","",""
"713","47","3","","98","","","656","2007-05-01
10:32:41.350000000","","",""
"714","47","10","","26","","","694","2007-05-01
10:32:41.350000000","","",""
"715","47","10","","35","","","695","2007-05-01
10:32:41.350000000","","",""
If you follow transaction id 714 up through the parent transaction ids
it doesn't not have a settlement cost yet lot 47 shows up as settled.

Thanks for you help.

May 1 '07 #1
6 1696
The procedure I entered above is incorrect.

here is the correct one.

ALTER Procedure [dbo].[GetPendingSettlementsLotNumbers]
(
@CurrentParentTransactionID int = 0
)
as
/*------------------------------------------------------------
Lists the contents of a table designed to represent a multi-
branch tree. The result set takes the form:
ItemID TreeLevel Label
Tree traversal is done non-recursively (to avoid SQL Server's
limit of 32 nested procedure calls)
------------------------------------------------------------*/
--table to hold the result set: a tree structure arranged by level
declare @IndentedTree table(TransactionID int, ParentTransactionID
int, TreeLevel int, LotNumber int, SettlementPrice decimal(32,9))
--table to track where we are in the tree
--this represents a stack turned upside down (with the most resent
item on
--the bottom)
declare @UnvisitedNodes table(StackID int identity(1,1), TransactionID
int, ParentTransactionID int, TreeLevel int, LotNumber int,
SettlementPrice decimal(32,9))
declare @LastTreeLevel int
set nocount on
--initialize the unvisited nodes list
set @LastTreeLevel = 0
INSERT INTO @UnvisitedNodes (TransactionID, ParentTransactionID,
TreeLevel, LotNumber, SettlementPrice)
--SELECT TransactionID, @LastTreeLevel, Label
--FROM tree
--WHERE ParentTransactionID = @CurrentParentTransactionID
--ORDER BY Label desc
Select TransactionID, ParentTransactionID, @LastTreeLevel,
LotNumber, Settlement
From itTransactionProcess
Where ParentTransactionID = @CurrentParentTransactionID and
Settlement is null
ORDER BY ParentTransactionID desc
--Select * from @UnvisitedNodes
--loop through levels of the tree structure
while ((SELECT count(*) FROM @UnvisitedNodes) <0)
begin
--add the top item to the result set
INSERT INTO @IndentedTree
SELECT TransactionID, ParentTransactionID, TreeLevel, LotNumber,
SettlementPrice
FROM @UnvisitedNodes
WHERE StackID = (SELECT max(StackID) FROM @UnvisitedNodes)
--Select * from @IndentedTree
Delete From @IndentedTree where TransactionID in (Select
ParentTransactionID from itTransactionProcess)

--get the top item's ID
set @CurrentParentTransactionID = (
SELECT TransactionID
FROM @UnvisitedNodes
WHERE StackID = (SELECT max(StackID) FROM @UnvisitedNodes))

--get the top item's indentation level
set @LastTreeLevel = (
SELECT TreeLevel
FROM @UnvisitedNodes
WHERE StackID = (SELECT max(StackID) FROM @UnvisitedNodes))
--delete that item from the list

DELETE FROM @UnvisitedNodes WHERE TransactionID =
@CurrentParentTransactionID

--add the children of the current item to the top of the list of
unvisited
--nodes
INSERT INTO @UnvisitedNodes (TransactionID, ParentTransactionID,
TreeLevel, LotNumber)
--SELECT TransactionID, @LastTreeLevel + 1, LotNumber
--FROM tree
--WHERE ParentTransactionID = @CurrentParentTransactionID
--ORDER BY LotNumber desc
Select TransactionID, ParentTransactionID, @LastTreeLevel + 1,
LotNumber
From itTransactionProcess
Where ParentTransactionID = @CurrentParentTransactionID and
Settlement is null
ORDER BY ParentTransactionID desc
end
--return the result set
Select LotId From ItLots where LotID not in
(SELECT LotNumber
FROM @IndentedTree) and LotID in (Select LotNumber from
itTransactionProcess)

May 1 '07 #2
Please disreguard post.

Everything work but when I moved the database over to a new server the
default values didn't stick to so the parent trasactionid was never
initiallized to 0.

Thanks,

May 1 '07 #3
Designing Solutions WD (mi**************@gmail.com) writes:
Please disreguard post.

Everything work but when I moved the database over to a new server the
default values didn't stick to so the parent trasactionid was never
initiallized to 0.
Which version of SQL Server are you? If you are on SQL 2005, you should
look into a new features know as Common Table Expressions, or CTEs for
short. A special form is recursive CTE which permits you to handle a
hierarchy in a single statement. Look up the topic "WITH common table
expression" in Books Online for further details.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
May 1 '07 #4
> Lists the contents of a table designed to represent a multi-branch tree. The result set takes the form: ItemID TreeLevel Label Tree traversal is done non-recursively (to avoid SQL Server's limit of 32 nested procedure calls) <<

Have you gotten a copy of TREES & HIERARCHIES IN SQL? There are
several ways to model trees without *any* procedural traversal code at
all.

You also have more NULL-able columns int his one table than the entire
payroll data base of a major automobile.

You used an IDENTITY as a key; is the real key (lot_nbr,
process_step) ?

Many of your data element names are just plain wrong. Think how silly
"category_id" is for an attribute; it is either "<some kind
of>_category" or "<some kind of>_id", but not both an identifier
(unique to one and only one instance of an entity) and a category
(value appears in many entities). Ditto monsters like
"current_process_step_id" as opposed to a mere "process_step" or
"process_id" instead. Get a copy of ISO-11179 rules for data element
names.

Learn what data and meta data are so you will not mix them in the
table.

May 2 '07 #5


"--CELKO--" <jc*******@earthlink.netwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
>> Lists the contents of a table designed to represent a multi-branch
tree. The result set takes the form: ItemID TreeLevel Label Tree
traversal is done non-recursively (to avoid SQL Server's limit of 32
nested procedure calls) <<

Have you gotten a copy of TREES & HIERARCHIES IN SQL? There are
several ways to model trees without *any* procedural traversal code at
all.

You also have more NULL-able columns int his one table than the entire
payroll data base of a major automobile.
Now I'm curious, how many cars have a payroll database. :-)


--
Greg Moore
SQL Server DBA Consulting Remote and Onsite available!
Email: sql (at) greenms.com http://www.greenms.com/sqlserver.html
May 2 '07 #6
On May 1, 11:00 pm, "Greg D. Moore \(Strider\)"
<mooregr_deletet...@greenms.comwrote:
"--CELKO--" <jcelko...@earthlink.netwrote in message

news:11**********************@o5g2000hsb.googlegro ups.com...
> Lists the contents of a table designed to represent a multi-branch
tree. The result set takes the form: ItemID TreeLevel Label Tree
traversal is done non-recursively (to avoid SQL Server's limit of 32
nested procedure calls) <<
Have you gotten a copy of TREES & HIERARCHIES IN SQL? There are
several ways to model trees without *any* procedural traversal code at
all.
You also have more NULL-able columns int his one table than the entire
payroll data base of a major automobile.

Now I'm curious, how many cars have a payroll database. :-)
You gotta be careful... they'll try to sneak one in with their option
packages...

May 2 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5355
by: Active8 | last post by:
I did this once and can't remember how <blush> so I read the reportlab user guid. It says to unzip the reportlab archive - this is on w2k, BTW, with Python23 - to a directory and make a file...
0
521
by: Gary Stollman | last post by:
My Father is a Clone//ETs plotting to overthrow the human race! On August 19, 1987 a gun-carrying Gary Stollman entered the studio of Los Angeles's KNBC television, crashing consumer reporter...
3
1312
by: Michael Hoffman | last post by:
Currently it returns Path('None'). This means I have to do a check on input before pathifying it to make sure it is not None. Perhaps it should throw ValueError? -- Michael Hoffman
3
1603
by: Tim Marshall | last post by:
I'm having a vexing problem with using filecopy and compactdatabase on a Jet back end I'm sure I did not have with A97. It seems to me that A2003 is much slower in shutting down the back end ldb...
9
13151
by: Bill Borg | last post by:
Hello, I call a function recursively to find an item that exists *anywhere* down the chain. Let's say I find it five layers deep. Now I've got what I need and want to break out of that whole...
13
2830
by: David | last post by:
Hi all, I have a singleton ienumerable that collects data from a database. I have listed the code below. It has the usual methods of current, move and reset. I need to go to a certain position...
46
2483
by: Bruce W. Darby | last post by:
This will be my very first VB.Net application and it's pretty simple. But I've got a snag in my syntax somewhere. Was hoping that someone could point me in the right direction. The history: My...
20
4222
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
9
2614
by: pereges | last post by:
Hello I need some ideas for designing a recursive function for my ray tracing program. The idea behind ray tracing is to follow the electromagnetic rays from the source, as they hit the...
0
7204
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7342
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7464
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1516
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.