October 8, 2012 at 3:19 pm
Hi,
I am new to sql server 2008 functions, I am facing an issue and request, if some can help.
I have a table like
id_taskid_fathertasklevel_task duracion_taskpercent_taskname_task
11 176NULLTask 1
21 260NULLTask 1.1
32 32100Task 1.1.1
42 338NULLTask 1.1.2
54 410100Task 1.1.2.1
64 42220Task 1.1.2.2
74 4535Task 1.1.2.3
84 4140Task 1.1.2.4
92 320NULLTask 1.1.3
109 42020Task 1.1.3.1
111 29NULLTask 1.2
1211 320Task 1.2.1
1311 37NULLTask 1.2.2
1413 4250Task 1.2.2.1
1513 4550Task 1.2.2.2
161 2100Task 1.3
171 2200Task 1.4
181 26NULLTask 1.5
1918 36NULLTask 1.5.1
2019 46NULLTask 1.5.1.1
2120 5610Task 1.5.1.1.1
I am trying to create a recurive function.
Calculation starts from the bottom
(5*6)+10 = 40
it goes for the column above this percent_task column
then
4+(6*65)
until the name_task is same (untill 1.5, starting from 1.5.1... to 1.5)
when it changes 1.4..then new caluculation
and at the end we need to do the sum
sum of ( 1.5+ 1.4+1.3.....like that..
I am trying to create a recursive function, but no luck.
Create function [dbo].[test1](@p_id_task int)
RETURNS @output TABLE(data VARCHAR(256))
begin
declare @i int
while @i in ( select top 100 id_task from [dbo].[test1](@p_id_task) where id_task<=@p_id_task and id_fathertask>=1 and status is null
order by id_task desc
)
begin
DECLARE @start INT
select @start = (level_task*duracion_task)+ COALESCE(percent_task,0) from [Reportes].[dbo].[FunctionTST1] where status='Processed' and
id_task=@i and status is null
INSERT INTO @output (data) values (@start)
end
return
end
Can anyone help me on this?
October 8, 2012 at 5:33 pm
I haven't been able to quite figure out what it is you want in the end, but the following code traverses your data in the proper order starting at the top.
Based on this, what is it you are trying to accomplish?
Create table dbo.TestData (
id_task int,
id_fathertask int,
level_task int,
duracion_task int,
percent_task int null,
name_task varchar(24)
);
go
insert into dbo.TestData
values
(1,1,1,76,NULL,'Task 1'),
(2,1,2,60,NULL,'Task,1.1'),
(3,2,3,2,100,'Task,1.1.1'),
(4,2,3,38,NULL,'Task,1.1.2'),
(5,4,4,10,100,'Task,1.1.2.1'),
(6,4,4,22,20,'Task,1.1.2.2'),
(7,4,4,5,35,'Task,1.1.2.3'),
(8,4,4,1,40,'Task,1.1.2.4'),
(9,2,3,20,NULL,'Task,1.1.3'),
(10,9,4,20,20,'Task,1.1.3.1'),
(11,1,2,9,NULL,'Task,1.2'),
(12,11,3,2,0,'Task,1.2.1'),
(13,11,3,7,NULL,'Task,1.2.2'),
(14,13,4,2,50,'Task,1.2.2.1'),
(15,13,4,5,50,'Task,1.2.2.2'),
(16,1,2,10,0,'Task,1.3'),
(17,1,2,20,0,'Task,1.4'),
(18,1,2,6,NULL,'Task,1.5'),
(19,18,3,6,NULL,'Task,1.5.1'),
(20,19,4,6,NULL,'Task,1.5.1.1'),
(21,20,5,6,10,'Task,1.5.1.1.1');
go
with BaseData as (
select
id_task,
id_fathertask,
level_task,
duracion_task,
percent_task,
name_task,
task_level = 1 -- for comparision purposes
from
dbo.TestData
where
id_task = id_fathertask
union all
select
td.id_task,
td.id_fathertask,
td.level_task,
td.duracion_task,
td.percent_task,
td.name_task,
task_level = bd1.task_level + 1
from
dbo.TestData td
inner join BaseData bd1
on (td.id_fathertask = bd1.id_task and td.id_task <> td.id_fathertask)
)
select * from BaseData order by name_task;
go
drop table dbo.TestData;
go
October 9, 2012 at 8:17 am
Thanks for your response.
Well, I am trying to have a recursive function for the below calculation
Calculation starts fromt the last task 1.5.1.1.1
(level_task*duaration_task) + percent_task (5*6)+10 = 40
the result goes to percent_task of above task that is 1.5.1.1
in 1.5.1.1 (4*6)+40(result from the below) = 64
goes like that untill task 1.5, where 1.5 will have 94 as the value.
From 1.4 new calaculation, since it doesnt have any child task value will be 40
For task 1.3 = 20
like that for task 1.2 = 123
In the End we need to add the task 1.5+1.4.+1.3+1.2+1.1
I hope, this is more understandable.
October 9, 2012 at 8:33 am
Your data structure for this is going to cause you nothing but grief. How do you know what the "root" of the task is?
This is a running total type of problem but at the base is this denormalized table. You don't need a recursive function for this. Take a look at this article from Jeff Moden on running totals. http://www.sqlservercentral.com/articles/T-SQL/68467/[/url]
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 9, 2012 at 11:08 am
Thanks, I am going through your link.
But we can pass the parameter for the function.
October 9, 2012 at 12:10 pm
sqlSer12 (10/9/2012)
Thanks, I am going through your link.But we can pass the parameter for the function.
I put together an example of this using the quirky update method. Since you never posted any ddl or sample data I used what Lynn posted.
The code below works on the example you provided. I also tried for Task,1.2.2 but I have no idea if it does what you want.
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TestData]') AND type in (N'U'))
DROP TABLE [dbo].TestData
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetCalculationForTestData]') AND type in (N'P'))
DROP PROCEDURE [dbo].[GetCalculationForTestData]
go
Create table dbo.TestData (
id_task int,
id_fathertask int,
level_task int,
duracion_task int,
percent_task int null,
name_task varchar(24)
);
go
insert into dbo.TestData
values
(1,1,1,76,NULL,'Task 1'),
(2,1,2,60,NULL,'Task,1.1'),
(3,2,3,2,100,'Task,1.1.1'),
(4,2,3,38,NULL,'Task,1.1.2'),
(5,4,4,10,100,'Task,1.1.2.1'),
(6,4,4,22,20,'Task,1.1.2.2'),
(7,4,4,5,35,'Task,1.1.2.3'),
(8,4,4,1,40,'Task,1.1.2.4'),
(9,2,3,20,NULL,'Task,1.1.3'),
(10,9,4,20,20,'Task,1.1.3.1'),
(11,1,2,9,NULL,'Task,1.2'),
(12,11,3,2,0,'Task,1.2.1'),
(13,11,3,7,NULL,'Task,1.2.2'),
(14,13,4,2,50,'Task,1.2.2.1'),
(15,13,4,5,50,'Task,1.2.2.2'),
(16,1,2,10,0,'Task,1.3'),
(17,1,2,20,0,'Task,1.4'),
(18,1,2,6,NULL,'Task,1.5'),
(19,18,3,6,NULL,'Task,1.5.1'),
(20,19,4,6,NULL,'Task,1.5.1.1'),
(21,20,5,6,10,'Task,1.5.1.1.1');
go
create Procedure GetCalculationForTestData
(
@TaskName varchar(50)
)
as begin
Create table #TestData
(
id_task int primary key, --MUST have a clustered index for the running total to work
id_fathertask int,
level_task int,
duracion_task int,
percent_task int null,
name_task varchar(24),
Calculation int --need to store the calculation so we added this extra column
);
Insert #TestData (id_task, id_fathertask, level_task, duracion_task, percent_task, name_task)
select id_task, id_fathertask, level_task, duracion_task, percent_task, name_task
from TestData
where name_task like @TaskName + '%'
declare @Calc int = 0
;with cteOrdered as
(
select top 2147483648 *
from #TestData
order by id_fathertask desc
)
update cteOrdered
set @Calc = Calculation = (level_task * duracion_task) + isnull(percent_task, @Calc)
from cteOrdered WITH (TABLOCKX)
OPTION (MAXDOP 1)
select * from #TestData
order by id_fathertask desc
end
go
exec GetCalculationForTestData @TaskName = 'Task,1.2.2'
exec GetCalculationForTestData @TaskName = 'Task,1.5'
If you use this make sure you read the running totals article I referenced and understand what it is doing. You are the one who is going to have to support this at 3am when it crashes in production.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 11, 2012 at 5:54 pm
Thanks a lot. However, the requirement got changed and had to modify the procedure. This is the proc
alter Procedure GetCalculationForTestData
(
@taskId int,
@fatherId int,
@taskLevel int
)
as begin
Create table #TestFinal
(
id_task int primary key,
id_fathertask int,
level_task int,
duracion_task int,
percent_task int null,
name_task varchar(24),
Calculation int,
Calculation1 int
);
Insert #TestFinal (id_task, id_fathertask, level_task, duracion_task, percent_task, name_task)
select id_task, id_fathertask, level_task, duracion_task, percent_task, name_task
from [Reportes].[dbo].[FunctionTST1]
While(@fatherId <> 0)
Begin
While(@taskLevel <> 0)
Begin
Create table #TestData
(
id_task int primary key,
id_fathertask int,
level_task int,
duracion_task int,
percent_task int null,
name_task varchar(24),
Calculation int,
Calculation1 int
);
Insert #TestData (id_task, id_fathertask, level_task, duracion_task, percent_task, name_task)
select id_task, id_fathertask, level_task, duracion_task, percent_task, name_task
from [Reportes].[dbo].[FunctionTST1]
where level_task = @taskLevel and id_fathertask = @fatherId
declare @Calc int = 0
declare @Calc1 int = 0
declare @temp int = 0
;with cteOrdered as
(
select * from #TestData
)
update cteOrdered
set @Calc = Calculation = (percent_task * duracion_task)
from cteOrdered WITH (TABLOCKX)
OPTION (MAXDOP 1)
update cteOrdered
set @Calc1 = Calculation1 = sum(Calculation)/sum(duracion_task)
from cteOrdered WITH (TABLOCKX)
OPTION (MAXDOP 1)
update #TestFinal set percent_task = @Calc1 where @taskId = @fatherId
drop table #TestData
set @temp = @taskLevel -1
execute GetCalculationForTestData1 @taskId,@fatherId,@temp
End
set @temp=0
set @temp = @fatherId - 1
execute GetCalculationForTestData1 @taskId,@temp,@taskLevel
End
select * from #TestFinal
End
But I am having one error on line 62. Invalid object name 'cteOrdered'. Can anyone help me to solve this?
October 11, 2012 at 9:21 pm
I am guessing that you didn't read through the article I suggested and understand this code. You can't reference the cte by multiple statements after it is declared. You need to combine your two update statements. I noticed you also removed the order by in the cte. This order is MANDATORY or you don't know what order the results will be returned. And why oh why did you put this into nested while loops??? You totally destroyed any chance at making this remotely quick. You need to start over here.
Can you post ddl (create table scripts), sample data (insert statements) and desired output based on your sample data? There is no need to put this in nested loops.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 11, 2012 at 10:23 pm
Thank you so much for your response. I am sorry for changing the code.
I cannot combine two update statements, since I need the first update value to calculate the next one.
Here is the code and the logic which I am using.
CREATE TABLE [dbo].[FunctionTST1](
[id_task] [int] NULL,
[id_fathertask] [int] NULL,
[level_task] [int] NULL,
[duracion_task] [float] NULL,
[percent_task] [float] NULL,
[name_task] [nvarchar](50) NULL
);
insert into [dbo].[FunctionTST1]
values
(1,1,1,76,NULL,'Task 1'),
(2,1,2,60,NULL,'Task,1.1'),
(3,2,3,2,100,'Task,1.1.1'),
(4,2,3,38,NULL,'Task,1.1.2'),
(5,4,4,10,100,'Task,1.1.2.1'),
(6,4,4,22,20,'Task,1.1.2.2'),
(7,4,4,5,35,'Task,1.1.2.3'),
(8,4,4,1,40,'Task,1.1.2.4'),
(9,2,3,20,NULL,'Task,1.1.3'),
(10,9,4,20,20,'Task,1.1.3.1'),
(11,1,2,9,NULL,'Task,1.2'),
(12,11,3,2,0,'Task,1.2.1'),
(13,11,3,7,NULL,'Task,1.2.2'),
(14,13,4,2,50,'Task,1.2.2.1'),
(15,13,4,5,50,'Task,1.2.2.2'),
(16,1,2,10,0,'Task,1.3'),
(17,1,2,20,0,'Task,1.4'),
(18,1,2,6,NULL,'Task,1.5'),
(19,18,3,6,NULL,'Task,1.5.1'),
(20,19,4,6,NULL,'Task,1.5.1.1'),
(21,20,5,6,10,'Task,1.5.1.1.1');
The actual result is 25.37 and if convert into whole numbers during all the calclulations then it is 25.
The aim of this is to find the percent_task for task1. Table is kind of a tree with Task1 as the root. Task one has 5 children Task 1.1, Task 1.2, Task 1.3, Task 1.4 and Task 1.5. Task 1.5 has one child Task 1.5.1 and it has one child Task 1.5.1.1. And Task 1.5.1.1 has one child Task 1.5.1.1.1 and so on.
Task 1.1.2 has four children Task 1.1.2.1, Task 1.1.2.2, Task 1.1.2.3 and Task 1.1.2.4. and so on in the table.
We need to start calculation from the leaf nodes. and result of the calulation replaces the percent_task of its parent.
For example to calculate percent_task for Task 1.1.2, we need to use the value of his children.
10*100 = 1000
22*20 = 440
5*35 = 175
1*40 = 40
-----------
381655
1655/38 = 43.55(44) = percent_task of task 1.1.2
Hence the values of Task 1.1 = 38, Task 1.2 = 38.8, Task 1.3 = 0, Task 1.4 = 0 and Task 1.5 = 10 and final value is 25
Hope the above one is understandable. I do not have much exp in this. Hence I developed the code from whatever you pasted and googling.
October 12, 2012 at 7:55 am
Can you post what you want for desired output? Just create a temp table and hard code inserts into it. I can't understand your explanation very well and the data just has me scratching me head.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 12, 2012 at 8:39 am
Table looks like below. For Ex. row 3, Id_task = 3, level_task=3 menas it is at level 3, id_fathertask = 2 that means, its parent is the one which has id_task=2, that is the one in the second row and so on. Aim is to calculate the value of the root that is Task 1. The value of Task 1 should be 25 after calculations.
It is kind of project plan and differnt level and sub level inside. id_task is kind of a primary key.
id_task id_fathertask level_task duracion_task percent_task name_task
1 1 1 76 NULL Task 1
2 1 2 60 NULL Task 1.1
3 2 3 2 100 Task 1.1.1
4 2 3 38 NULL Task 1.1.2
5 4 4 10 100 Task 1.1.2.1
6 4 4 22 20 Task 1.1.2.2
7 4 4 5 35 Task 1.1.2.3
8 4 4 1 40 Task 1.1.2.4
9 2 3 20 NULL Task 1.1.3
10 9 4 20 20 Task 1.1.3.1
11 1 2 9 NULL Task 1.2
12 11 3 2 0 Task 1.2.1
13 11 3 7 NULL Task 1.2.2
14 13 4 2 50 Task 1.2.2.1
15 13 4 5 50 Task 1.2.2.2
16 1 2 10 0 Task 1.3
17 1 2 20 0 Task 1.4
18 1 2 6 NULL Task 1.5
19 18 3 6 NULL Task 1.5.1
20 19 4 6 NULL Task 1.5.1.1
21 20 5 6 10 Task 1.5.1.1.1
October 12, 2012 at 9:17 am
sqlSer12 (10/12/2012)
Table looks like below. For Ex. row 3, Id_task = 3, level_task=3 menas it is at level 3, id_fathertask = 2 that means, its parent is the one which has id_task=2, that is the one in the second row and so on. Aim is to calculate the value of the root that is Task 1. The value of Task 1 should be 25 after calculations.It is kind of project plan and differnt level and sub level inside. id_task is kind of a primary key.
id_task id_fathertask level_task duracion_task percent_task name_task
1 1 1 76 NULL Task 1
2 1 2 60 NULL Task 1.1
3 2 3 2 100 Task 1.1.1
4 2 3 38 NULL Task 1.1.2
5 4 4 10 100 Task 1.1.2.1
6 4 4 22 20 Task 1.1.2.2
7 4 4 5 35 Task 1.1.2.3
8 4 4 1 40 Task 1.1.2.4
9 2 3 20 NULL Task 1.1.3
10 9 4 20 20 Task 1.1.3.1
11 1 2 9 NULL Task 1.2
12 11 3 2 0 Task 1.2.1
13 11 3 7 NULL Task 1.2.2
14 13 4 2 50 Task 1.2.2.1
15 13 4 5 50 Task 1.2.2.2
16 1 2 10 0 Task 1.3
17 1 2 20 0 Task 1.4
18 1 2 6 NULL Task 1.5
19 18 3 6 NULL Task 1.5.1
20 19 4 6 NULL Task 1.5.1.1
21 20 5 6 10 Task 1.5.1.1.1
I want to know what you want as output from your query. And to make it legible inserting into a temp table will make it a lot easier to deal with.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 12, 2012 at 10:00 am
Final result should be 25 or 25.37 to be precise. This the sample table data. Task is calulate the value of root of the tree that is Task1(First row) by using its children. For Ex. thirs row id_task=3 it is unique for every row and identify the task number. level_task = 3, that is it belongs to the third level in the tree. id_fathertask = 2 means, its parent the one which has id_task as 2, that is Task 1.1(second row). The result of the calculation should go to the percent_task of their parent. Hence we start the calculation from the lowest level in the tree and result goes to its parent and finally we find the value of the root.
id_task id_fathertask level_task duracion_task percent_task name_task
1 1 1 76 NULL Task 1
2 1 2 60 NULL Task 1.1
3 2 3 2 100 Task 1.1.1
4 2 3 38 NULL Task 1.1.2
5 4 4 10 100 Task 1.1.2.1
6 4 4 22 20 Task 1.1.2.2
7 4 4 5 35 Task 1.1.2.3
8 4 4 1 40 Task 1.1.2.4
9 2 3 20 NULL Task 1.1.3
10 9 4 20 20 Task 1.1.3.1
11 1 2 9 NULL Task 1.2
12 11 3 2 0 Task 1.2.1
13 11 3 7 NULL Task 1.2.2
14 13 4 2 50 Task 1.2.2.1
15 13 4 5 50 Task 1.2.2.2
16 1 2 10 0 Task 1.3
17 1 2 20 0 Task 1.4
18 1 2 6 NULL Task 1.5
19 18 3 6 NULL Task 1.5.1
20 19 4 6 NULL Task 1.5.1.1
21 20 5 6 10 Task 1.5.1.1.1
October 12, 2012 at 10:08 am
Can you post the table with the intermediate amounts? It is very unclear what you are trying to do here. Given the procedure you created what would be the values you pass in?
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
October 12, 2012 at 10:20 am
id_taskid_fathertasklevel_taskduracion_taskpercent_taskname_task
11176NULLTask 125
21260NULLTask 1.138
3232100Task 1.1.1100
42338NULLTask 1.1.244
54410100Task 1.1.2.1100
6442220Task 1.1.2.220
744535Task 1.1.2.335
844140Task 1.1.2.440
92320NULLTask 1.1.320
10942020Task 1.1.3.120
11129NULLTask 1.239
1211320Task 1.2.10
131137NULLTask 1.2.250
14134250Task 1.2.2.150
15134550Task 1.2.2.250
1612100Task 1.30
1712200Task 1.40
18126NULLTask 1.510
191836NULLTask 1.5.110
201946NULLTask 1.5.1.110
21205610Task 1.5.1.1.110
Viewing 15 posts - 1 through 15 (of 22 total)
You must be logged in to reply to this topic. Login to reply