all groups > sql server programming > september 2003 >
You're in the

sql server programming

group:

Multi-table Stored Procedure With Aggregates


Multi-table Stored Procedure With Aggregates mblacky2000 NO[at]SPAM hotmail.com
9/21/2003 10:13:46 PM
sql server programming:
Hi

I'm in the process of creating a maintenance cost report for the Pulp
and Paper company for whom I work. The company uses a Maximo
maintenance management software with a semi-normalised SQL Server 2000
backend. This stores the data from which the report is to be derived.

All maintenance costs at the mill are managed through workorders and
workorders can be set up in parent child hierarchies to manage shuts
and capital work etc.
I'm attempting to create report to display shut/capital costs. I have
got the workorder hierarchy successfully captured using a cursor loop
and now i wish to also calculate a sum of related costs for each
workorder from various other tables such as purchase requisitions and
material requisitions . Unfortunately there are multiple lines in many
of the tables that relating back to one workorder. I have attempted
this with left outer joins and aggregates but aren't having much luck.
If someone could point me in the right disrection I would be very
appreciative. I will include a diagram and my stored procedure to help
clarify things.

Thankyou for your time
Michael Black


Workorder1---M Purchase Requisitions=> Sum Puuchase Reqs cost For WO
1---M Service Requisitions => Sum Service Reqs cost for WO
1---M Service Receipts => Sum Service Receipts cost for that
WO
1---1 Location Description
Workorder1---M Purchase Requisitions=> Sum Puuchase Reqs cost For
WO
1---M Service Requisitions => Sum Service Reqs cost for
WO
1---M Service Receipts => Sum Service Receipts cost for
that WO
1---1 Location Description
Workorder1---M Purchase Requisitions=> Sum Puuchase Reqs cost
For WO
1---M Service Requisitions => Sum Service Reqs cost
for WO
1---M Service Receipts => Sum Service Receipts cost
for that WO
1---1 Location Description
Workorder 1---M Purchase Requisitions=> Sum Purchase Reqs
costFor WO
1---M Service Requisitions => Sum Service Reqs
cost for WO
1---M Service Receipts => Sum Service Rec Costs
for WO
1---1 Location Description









CREATE PROCEDURE CHHT_GetCapExpend
@WONUM VarChar (20)
/* @STARTDATE DATETIME,
@ENDDATE DATETIME */

AS

SELECT Distinct Wo.Wonum, WO.Description,
WO.parent,WO.location,WO.estlabhrs,WO.estlabcost,WO.estmatcost,
WO.esttoolcost,WO.estservcost,SUBSTRING(WO.location,5,2),
WO.ReportDate,
SUBSTRING(WO.Glaccount,8,6), WO.worktype, Wo.wopm6, l.description,
l1.description, wo.glaccount,
SUBSTRING(WO.glaccount,12,2), Sum(SR.linecost), Sum(MT.linecost),
Sum(SPRL.Linecost), Sum(MPRL.Linecost),
Sum(MR.TotalCost), Sum(POL.linecost)

From WORKORDER WO
LEFT OUTER JOIN Locations L on
SUBSTRING(WO.location,1,4) = L.location
LEFT OUTER JOIN Locations L1 on SUBSTRING(WO.location,1,6) =
L1.location
LEFT OUTER JOIN ServRecTrans SR on WO.Wonum = SR.Refwo
LEFT OUTER JOIN MatUseTrans MT on Wo.Wonum = MT.Refwo
LEFT OUTER JOIN PRLine SPRL on WO.Wonum = SPRL.Refwo
LEFT OUTER JOIN PRLine MPRL on WO.Wonum = MPRL.Refwo
LEFT OUTER JOIN PR on SPRL.PRNUM = PR.PRNUM
LEFT OUTER JOIN PR PRPR on MPRL.PRNUM = PRPR.PRNUM
LEFT OUTER JOIN MR on WO.Wonum = MR.Wonum
LEFT OUTER JOIN POLine POL on WO.Wonum = POL.RefWo


Where WO.WONUM = @wonum
Group By Wo.Wonum
/*and SUBSTRING(WO.location,1,4) = L.location and
SUBSTRING(WO.location,1,6) = L1.location and*/
/* WO.reportdate between @startdate and @enddate */
DECLARE SubWONums CURSOR LOCAL FOR
SELECT WONUM FROM WORKORDER WHERE PARENT = @WONUM
OPEN SubWONums
FETCH NEXT FROM SubWONums INTO @WONUM
WHILE @@FETCH_STATUS=0 BEGIN
EXEC CHHT_GetCapExpend @WONUM /*, @STARTDATE, @ENDDATE */
FETCH NEXT FROM SubWONums INTO @WONUM
END
CLOSE SubWONums
Multi-table Stored Procedure With Aggregates Raghavendra Narayana
9/22/2003 12:21:46 AM
http://www.microsoft.com/india/msdn/articles/117.aspx

Have a look at the concept/algoritham of this article.
May be helpful for your problem.

Cheers,
Raghavendra Narayana
MSDN India Community Star

[quoted text, click to view]
Re: Multi-table Stored Procedure With Aggregates mblacky2000 NO[at]SPAM hotmail.com
9/22/2003 10:20:54 PM
I Solved the problem using nested select statements to calculate
totals from the other cost tables for each individual workorder in the
following query.

SELECT Distinct Wo.Wonum, WO.Description,
WO.parent,WO.location,WO.estlabhrs,WO.estlabcost,WO.estmatcost,
WO.esttoolcost,WO.estservcost,SUBSTRING(WO.location,5,2),
WO.ReportDate, SUBSTRING(WO.Glaccount,8,6), WO.worktype, Wo.wopm6,
l.description, l1.description, wo.glaccount,
SUBSTRING(WO.glaccount,12,2),

(Select Sum(SR.linecost) From Servrectrans SR where WO.Wonum =
SR.Refwo),
(Select Sum(MT.linecost) From Matusetrans MT Where WO.Wonum =
MT.Refwo),
(Select Sum(MPRL.Linecost)From PRLine MPRL Where WO.Wonum = MPRL.Refwo
and MPRL.Service = 'N'),
(Select Sum(SPRL.Linecost)From PRLine SPRL Where WO.Wonum = SPRL.Refwo
and SPRL.Service = 'Y'),
ISNULL((Select Sum(MR.TotalCost)From MR where Wo.Wonum = MR.Wonum),0),
ISNULL((Select Sum(POL.linecost)From Poline POL, PO where WO.wonum =
POL.Refwo and PO.ponum = POL.Ponum and PO.Potype = 'REL'),0)

From WORKORDER WO
LEFT OUTER JOIN Locations L on
SUBSTRING(WO.location,1,4) = L.location
LEFT OUTER JOIN Locations L1 on SUBSTRING(WO.location,1,6) =
L1.location



AddThis Social Bookmark Button