473,769 Members | 5,871 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].[itTransactionPr ocess] Script Date:
05/01/2007 10:42:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFI ER ON
GO
CREATE TABLE [dbo].[itTransactionPr ocess](
[TransactionID] [int] IDENTITY(1,1) NOT NULL,
[LotNumber] [int] NOT NULL,
[CurrentProcessS tepID] [int] NOT NULL,
[NextProcessStep ID] [int] NULL,
[CategoryID] [int] NULL,
[ProductID] [int] NULL,
[ProductVariantI D] [int] NULL,
[ParentTransacti onID] [int] NULL,
[TransactionDate Entered] [datetime] NULL,
[TransactionDate Exit] [datetime] NULL,
[Settlement] [money] NULL,
[Completed] [int] NULL,
CONSTRAINT [PK_itTransactio nProcess] PRIMARY KEY CLUSTERED
(
[TransactionID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORE COMPUTE = OFF, IGNORE_DUP_KEY
= OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCK S = 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].
[getPendingSettl ementDetails] Script Date: 05/01/2007 10:47:47
******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFI ER ON
GO
ALTER PROCEDURE [dbo].[getPendingSettl ementDetails]
AS

declare @LotNumbersTabl e table(LotNumber int)
insert into @LotNumbersTabl e EXEC GetPendingSettl ementsLotNumber s

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

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

select @LotNumber = min(LotNumber) from @LotNumbersTabl e
while @LotNumber is not null begin
Select @DateRecieved = min(Transaction DateEntered) from
itTransactionPr ocess where LotNumber = @LotNumber
Select @DateComplete = max(Transaction DateExit) from
itTransactionPr ocess where LotNumber = @LotNumber and Settlement is
not null
SET @NumberOfDaysFo rSettlement = DATEDIFF(DAY, @DateRecieved,
@DateComplete)
Select @Company = Company from SP_Active_Lot_D eliveries where LotID =
@LotNumber
Select @Contact = ContactName from SP_Active_Lot_D eliveries where
LotID = @LotNumber
INSERT INTO @ResultsTable (LotNumber, DateReceived, DateComplete,
SettlementLengt h, Company, Contact) Values
(@LotNumber, @DateRecieved, @DateComplete,
@NumberOfDaysFo rSettlement, @company, @contact)
select @LotNumber = min(LotNumber) from @LotNumbersTabl e where
LotNumber @LotNumber
end
Select * From @ResultsTable where SettlementLengt h is not null

here is sample data
"TransactionID" ,"LotNumber","C urrentProcessSt epID","NextProc essStepID","Cat egoryID","Produ ctID","ProductV ariantID","Pare ntTransactionID ","TransactionD ateEntered","Tr ansactionDateEx it","Settlement ","Complete d"
"628","47","1", "2","5","",""," ","","2007-05-01
10:23:15.747000 000","",""
"629","47","1", "3","17","","", "","","2007-05-01
10:23:15.747000 000","0.25",""
"630","47","1", "4","34","","", "","","2007-05-01
10:23:15.747000 000","-0.15",""
"631","47","1", "3","38","","", "","","2007-05-01
10:23:15.747000 000","-0.15",""
"632","47","1", "4","33","","", "","","2007-05-01
10:23:15.747000 000","-0.35",""
"633","47","1", "3","15","","", "","","2007-05-01
10:23:15.747000 000","10",""
"634","47","2", "3","86","","", "628","2007-05-01
10:23:15.747000 000","2007-05-01 10:32:41.320000 000","-0.35",""
"635","47","3", "","17","",""," 629","2007-05-01
10:23:15.747000 000","","",""
"636","47","4", "","34","",""," 630","2007-05-01
10:23:15.747000 000","","",""
"637","47","3", "","38","",""," 631","2007-05-01
10:23:15.747000 000","","",""
"638","47","4", "","33","",""," 632","2007-05-01
10:23:15.747000 000","","",""
"639","47","3", "","15","",""," 633","2007-05-01
10:23:15.747000 000","","",""
"640","47","2", "3","85","","", "628","2007-05-01
10:24:47.983000 000","2007-05-01 10:32:41.320000 000","0.05",""
"641","47","2", "4","88","","", "628","2007-05-01
10:24:56.343000 000","2007-05-01 10:32:41.333000 000","0.8",""
"642","47","2", "4","9","",""," 628","2007-05-01
10:25:07.517000 000","2007-05-01 10:32:41.333000 000","-0.15",""
"643","47","2", "4","100","","" ,"628","2007-05-01
10:25:22.470000 000","2007-05-01 10:32:41.333000 000","-0.35",""
"644","47","2", "4","90","","", "628","2007-05-01
10:25:44.297000 000","2007-05-01 10:32:41.333000 000","-0.35",""
"645","47","2", "4","12","","", "628","2007-05-01
10:25:59.347000 000","2007-05-01 10:32:41.333000 000","-0.15",""
"646","47","2", "4","26","","", "628","2007-05-01
10:26:12.610000 000","2007-05-01 10:32:41.333000 000","-0.35",""
"647","47","2", "3","94","","", "628","2007-05-01
10:26:29.523000 000","2007-05-01 10:32:41.333000 000","-3",""
"648","47","2", "3","95","","", "628","2007-05-01
10:26:47.323000 000","2007-05-01 10:32:41.333000 000","-0.35",""
"649","47","2", "3","38","","", "628","2007-05-01
10:27:01.450000 000","2007-05-01 10:32:41.333000 000","-0.15",""
"650","47","2", "4","33","","", "628","2007-05-01
10:27:15.533000 000","2007-05-01 10:32:41.333000 000","-0.35",""
"651","47","2", "4","34","","", "628","2007-05-01
10:27:33.767000 000","2007-05-01 10:32:41.333000 000","-0.15",""
"652","47","2", "3","96","","", "628","2007-05-01
10:27:46.850000 000","2007-05-01 10:32:41.350000 000","-0.35",""
"653","47","2", "3","97","","", "628","2007-05-01
10:28:00.917000 000","2007-05-01 10:32:41.350000 000","0.05",""
"654","47","2", "4","36","","", "628","2007-05-01
10:28:10.813000 000","2007-05-01 10:32:41.350000 000","-15",""
"655","47","2", "4","37","","", "628","2007-05-01
10:28:25.347000 000","2007-05-01 10:32:41.350000 000","0.35",""
"656","47","2", "3","98","","", "628","2007-05-01
10:28:36.917000 000","2007-05-01 10:32:41.350000 000","-0.35",""
"694","47","2", "10","26","","" ,"628","2007-05-01
10:32:17.170000 000","2007-05-01 10:32:41.350000 000","",""
"695","47","2", "10","35","","" ,"628","2007-05-01
10:32:27.883000 000","2007-05-01 10:32:41.350000 000","45",""
"696","47","3", "","86","",""," 634","2007-05-01
10:32:41.320000 000","","",""
"697","47","3", "","85","",""," 640","2007-05-01
10:32:41.333000 000","","",""
"698","47","4", "","88","",""," 641","2007-05-01
10:32:41.333000 000","","",""
"699","47","4", "","9","","","6 42","2007-05-01
10:32:41.333000 000","","",""
"700","47","4", "","100","","", "643","2007-05-01
10:32:41.333000 000","","",""
"701","47","4", "","90","",""," 644","2007-05-01
10:32:41.333000 000","","",""
"702","47","4", "","12","",""," 645","2007-05-01
10:32:41.333000 000","","",""
"703","47","4", "","26","",""," 646","2007-05-01
10:32:41.333000 000","","",""
"704","47","3", "","94","",""," 647","2007-05-01
10:32:41.333000 000","","",""
"705","47","3", "","95","",""," 648","2007-05-01
10:32:41.333000 000","","",""
"706","47","3", "","38","",""," 649","2007-05-01
10:32:41.333000 000","","",""
"707","47","4", "","33","",""," 650","2007-05-01
10:32:41.333000 000","","",""
"708","47","4", "","34","",""," 651","2007-05-01
10:32:41.333000 000","","",""
"709","47","3", "","96","",""," 652","2007-05-01
10:32:41.350000 000","","",""
"710","47","3", "","97","",""," 653","2007-05-01
10:32:41.350000 000","","",""
"711","47","4", "","36","",""," 654","2007-05-01
10:32:41.350000 000","","",""
"712","47","4", "","37","",""," 655","2007-05-01
10:32:41.350000 000","","",""
"713","47","3", "","98","",""," 656","2007-05-01
10:32:41.350000 000","","",""
"714","47","10" ,"","26","","", "694","2007-05-01
10:32:41.350000 000","","",""
"715","47","10" ,"","35","","", "695","2007-05-01
10:32:41.350000 000","","",""
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 1715
The procedure I entered above is incorrect.

here is the correct one.

ALTER Procedure [dbo].[GetPendingSettl ementsLotNumber s]
(
@CurrentParentT ransactionID 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(Transacti onID int, ParentTransacti onID
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, ParentTransacti onID 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, ParentTransacti onID,
TreeLevel, LotNumber, SettlementPrice )
--SELECT TransactionID, @LastTreeLevel, Label
--FROM tree
--WHERE ParentTransacti onID = @CurrentParentT ransactionID
--ORDER BY Label desc
Select TransactionID, ParentTransacti onID, @LastTreeLevel,
LotNumber, Settlement
From itTransactionPr ocess
Where ParentTransacti onID = @CurrentParentT ransactionID and
Settlement is null
ORDER BY ParentTransacti onID 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, ParentTransacti onID, TreeLevel, LotNumber,
SettlementPrice
FROM @UnvisitedNodes
WHERE StackID = (SELECT max(StackID) FROM @UnvisitedNodes )
--Select * from @IndentedTree
Delete From @IndentedTree where TransactionID in (Select
ParentTransacti onID from itTransactionPr ocess)

--get the top item's ID
set @CurrentParentT ransactionID = (
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 =
@CurrentParentT ransactionID

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

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****@sommarsk og.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_i d" 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_proces s_step_id" as opposed to a mere "process_st ep" 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*******@eart hlink.netwrote in message
news:11******** **************@ o5g2000hsb.goog legroups.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_delete t...@greenms.co mwrote:
"--CELKO--" <jcelko...@eart hlink.netwrote in message

news:11******** **************@ o5g2000hsb.goog legroups.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
5388
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 called reportlab.pth in the pythod dir. The only line in this file is supposed to be the path to the reportlib dir. Then I'm instructed to open the ... ok I'll close and reopen ... the python cmd line interpreter and type import reportlab
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 David Horowitz's live newscast. Stollman handed Horowitz a written statement and ordered him to read it while holding a gun on him. Unbeknownst to Stollman, KNBC immediately switched to a commercial, not permitting the statement to be transmitted. ...
3
1329
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
1617
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 than A97 and the code below (for compactdatabase) gets caught and throws an error related to the BE still being open. I would really appreciate any suggestions. I first ran into this with FileCopy and settled on a warning to close all forms...
9
13213
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 stack and continue execution at the point of the initial call. Is that possible? Thanks, Bill
13
2852
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 or set filters or many other things and I don't know how to do this. I have just learned singleton AND ienumerable, so miraculously got this to work so far. I could do with a bit of help to go further with it. If need by, I may have to change the...
46
2537
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 work involves creating custom packages of our software product for golf courses that purchase our software. The course data is kept as a back up in the event the course needs us to replace their custom files. Each course has a folder of it's own...
20
4284
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 is structured as an upside-down tree, and (if I remember correctly) never more than 4 levels. The site basically grew (like the creeping black blob) ... all the pages were created in Notepad over the last
9
2637
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 object.The object is triangulated. The rays can undergo multiple reflections, diffractions etc of the same object i.e. a ray hits a surface of the object, undergoes reflection resulting in a reflected ray which can again hit a surface, corner or edge...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9863
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7409
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5299
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.