SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Sum Up the value accodring to the Department Structure.


Sum Up the value accodring to the Department Structure.

Author
Message
mohanaraj
mohanaraj
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

Group: General Forum Members
Points: 36 Visits: 36
Hai,

I want to sum up the child department values and display in the parent record

Below is the Table.

DepartmentId ParentId DeptName Incoming
1 ------------0------ PhnDeprt ------14.00
3 ------------1-------Facilities ------01.00
2 ------------0-------Calse ---------- 5.00
4 ------------2-------CalFacitity ------1.00
5 ----------- 4 -------CalFF -----------2.00

I want the result as

DepartmentId ParentId DeptName Incoming
1 ------------0------ PhnDeprt ------15.00
3 ------------1-------Facilities ------01.00
2 ------------0-------Calse ---------- 8.00
4 ------------2-------CalFacitity ------3.00
5 ----------- 4 -------CalFF -----------2.00

Here 'PhnDeprt' department have child 'Facilities' so in the result 1.00 + 14.00 = 15.00 this result will be displayed in the Parent record i.e. in the 'PhnDeprt''s Incoming column


Please advise and help me

Thanks in advance.
crazy4sql
crazy4sql
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1679 Visits: 4489
is facilities always 1? What type of field is storing value '1' or '15'? Is it int or varchar?

----------
Ashish
mohanaraj
mohanaraj
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

Group: General Forum Members
Points: 36 Visits: 36
Hai

Incoming Is the Decimal type field. This field have any type of values in decimal.
sinureddi
sinureddi
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 81
Hi,

Try the following code:

with Incoming as(
select DepartmentId, ParentId, Incoming as Incoming from #Department
where ParentId in (select DepartmentId from #department)
Union All
select d.DepartmentId, i.ParentId, d.Incoming as Incoming
from #Department d Inner Join Incoming i
ON
i.DepartmentId = d.ParentId
)


select d.DepartmentId, d.ParentId, (d.incoming+isnull(i.incoming,0)) as incoming
from
#Department d Left Outer Join (Select ParentId, Sum(Incoming) as incoming from Incoming group by ParentId) i
ON d.DepartmentId = i.ParentId

Srinivas Reddy.S
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16214 Visits: 19544
Here's a simple solution which assumes only three levels in the hierarchy, as in your sample data. Note how the sample data has been set up using a script:

DROP TABLE #Sample
CREATE TABLE #Sample (DepartmentId INT, ParentId INT, DeptName VARCHAR(20), Incoming DECIMAL(5,2))
INSERT INTO #Sample (DepartmentId, ParentId, DeptName, Incoming)
SELECT 1, 0, 'PhnDeprt', 14.00 UNION ALL
SELECT 3, 1, 'Facilities', 01.00 UNION ALL
SELECT 2, 0, 'Calse', 5.00 UNION ALL
SELECT 4, 2, 'CalFacitity', 1.00 UNION ALL
SELECT 5, 4, 'CalFF', 2.00

SELECT d.DepartmentId, d.ParentId, d.DeptName,
Level0Incoming = d.Incoming,
Level1Incoming = c1.Incoming,
Level2Incoming = c2.Incoming
FROM #Sample d
LEFT JOIN #Sample c1 ON c1.ParentId = d.DepartmentId
LEFT JOIN #Sample c2 ON c2.ParentId = c1.DepartmentId



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search