Solved

Syntax help with SQL Function

Posted on 2011-03-06
6
366 Views
Last Modified: 2012-05-11
I have a funtion with one parameter that returns a sum of all wages paid for a particular job from a table called TimeSheets. I use this funtion in a stored procedure but now I find I need fringe costs (Union Dues) also. For every row of wages there are four lines of fringe costs in another related table that I need to include as part of the total wages.

Here is the function I use to return labor costs against a job:

ALTER FUNCTION [dbo].[fn_LaborCosts]
               (@JobNumber char(5))
RETURNS MONEY
AS
  BEGIN
  DECLARE @TotalAmount money
  SELECT @TotalAmount = (SELECT sum(ts.LaborAmount)
  FROM      TimeSheets      ts
            
  WHERE ts.JobNumber = @JobNumber)
                        
  RETURN @TotalAmount

Here is how I use it in the Stored procedure:

ALTER PROCEDURE [dbo].[spSelectJobs]
AS
SELECT      
      JobNumber,
      JobDescription,
      ProposalPrice,
      dbo.fn_LaborCosts(j.JobNumber) AS TotalLabor
                        
FROM   dbo.Jobs   j

The PK for the Fringe table is TsKey so for every row in the TimeSheets table there are four rows in the Fringe table with the TsKey. I need to add up all the fringe costs and add them to the total labor costs and I am not sure how to accomplish this. Thanks for any help on this.
0
Comment
Question by:AkAlan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 41

Accepted Solution

by:
Sharath earned 250 total points
ID: 35050113
Alter the function like this.
ALTER FUNCTION [dbo].[FN_LABORCOSTS] 
              (@JobNumber CHAR(5)) 
RETURNS MONEY 
AS 
  BEGIN 
    DECLARE  @TotalAmount MONEY 
     
    SELECT @TotalAmount = SUM(ts.LaborAmount + ISNULL(f.FringeCost,0)) 
      FROM TimeSheets ts 
           LEFT JOIN (  SELECT TsKey, 
                          SUM(FringeCost) FringeCost 
                     FROM Fringe 
                 GROUP BY TsKey) f 
             ON ts.TsKey = f.TsKey 
     WHERE ts.JobNumber = @JobNumber 
     
    RETURN @TotalAmount 
  END

Open in new window

0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 250 total points
ID: 35052154
Or just simply (no points please):
ALTER FUNCTION [dbo].[FN_LABORCOSTS] (@JobNumber CHAR(5))

RETURNS MONEY

AS

BEGIN 
     
RETURN (
   SELECT   SUM(ts.LaborAmount + ISNULL(f.FringeCost, 0))
   FROM     TimeSheets ts
            LEFT JOIN (SELECT   TsKey,
                                SUM(FringeCost) FringeCost
                       FROM     Fringe
                       GROUP BY TsKey) f ON ts.TsKey = f.TsKey
   WHERE    ts.JobNumber = @JobNumber 
     )
END

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 35063539
Sharath_123,

My apologies, as you can see I did try, unfortunately it seems the author overlooked my request.

Feel free to object.

Anthony
0
 
LVL 6

Author Comment

by:AkAlan
ID: 35070776
Ace, I just realized what happened here. I will try and rectify it. I thought the second post was you with a cleaner solution than your first.
0
 
LVL 41

Expert Comment

by:Sharath
ID: 35071490
acperkins -  Not an issue. :) Expected a split, but that's ok.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question