简体   繁体   中英

Using a UDF function in a query

I have the following Code which points at the DateDimension which works out the actual mins of business opening hours and closing ours. I am hoping to use the output of the query to pass on to my main query to identify the sum total of minutes business is open. This will allow me to exclude bank holidays and sundays if necessary. However I am having problems creating the function. Cannot find either column "dbo" or the user-defined function or aggregate "dbo.GetWorkingDaysMin", or the name is ambiguous.

this end query I expect to look like this

SELECT   dbo.GetWorkingDaysMin([WebLeadRecd_DKey],[ContactSucces_DKey]), COLUMN1, column2 etc AS Name 
FROM  [dbo].[TEST_DATES] 

this is the function below

alter FUNCTION GetWorkingDaysMin
    ( 
    @Date1 INT,
    @Date2 INT 
     )

    RETURNS table AS
    RETURN 
    (

    SELECT SUM(TotalMinsOpen) AS TotalMinOpen, SUM(TotalMinsClosed) AS TotalMinClosed, 
    FROM [dbo].[DIM_DATE]
    WHERE DateKey BETWEEN @Date1 AND @Date2

    )

I am just wondering if this is the best way to exclude bank holidays and sundays, the opening hours of the business are 8am - 6pm Mon -Fri and Satu 8am - 1pm. Also perhaps you can advise on the next stage as well, I am thinking about using cases statements to do a bit of airthmetics but am I setting myseld up for a failure taking that approach or is there a better way?

You have defined a table valued function because you are returning two values. Then you are using it as a scalar function. Instead, call it as:

SELECT  wdm.*, td.*
FROM dbo.TEST_DATES td CROSS APPLY
     dbo.GetWorkingDaysMin(td.WebLeadRecd_DKey, td.ContactSucces_DKey) wdm;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM