Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8950043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:21:35+00:00 2026-06-15T13:21:35+00:00

I am attempting to create a function that incorporates multiple ‘With’ Statements. My original

  • 0

I am attempting to create a function that incorporates multiple ‘With’ Statements.

My original Query is:

WITH EmpCount as 
  (
    SELECT job.EmployeeID, assign.PropertyID FROM EmployeeJobstatus job 
    JOIN Assignment assign ON job.JobID = assign.ID  GROUP by job.EmployeeID,
    assign.PropertyID
  ),
    NoDup as 
  (
    SELECT EmployeeID, Count(employeeID) as NO from EmpCount 
    Group by EmployeeID HAVING count(Employeeid) > 1
  )

    SELECT prop.Name, job.EmployeeID, Emp.EmpID, Emp.Name 
    from EmployeeJobStatus job
    JOIN Assignment assign ON assign.id = job.jobid
    JOIN Property prop ON prop.ID = assign.PropertyID
    JOIN Employee emp on emp.ID = job.EmployeeID
    WHERE EmployeeID IN (SELECT EmployeeID from NoDup)
    GROUP By prop.Name, EmployeeID, emp.EmpID,Emp.Name
    Order BY EmployeeID

This returns:

Name           EmployeeID   EmpID         Name
Property 1      23         1286333      LastNameRemoved1, Rachel A
Property 2      23         1286333      LastNameRemoved1, Rachel A
Property 2      76         1268329      LastNameRemoved2, Tamer A
Property 1      76         1268329      LastNameRemoved2, Tamer A
Property 3      135        1411933      LastNameRemoved3, Sarah E
Property 1      135        1411933      LastNameRemoved2, Sarah E

My function needs to return a ‘Y’ or a ‘N’ depending on whether or not they were cross property, utilizing the fields in EmployeeJobStatus StartDate and EndDate.

I have only started my function, as I have no idea what to do next.

CREATE function dbo.IsEmployeeCrossPropertyOnDate
 (@EmpID int, @AsOfDate datetime) 
  RETURNS INT AS

Any help, or a nudge in the right direction would be much appreciated. Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:21:36+00:00Added an answer on June 15, 2026 at 1:21 pm

    How about this…

    CREATE FUNCTION dbo.IsEmployeeCrossPropertyOnDate
        (
            @empid int
            , @asofdate datetime
        )
    RETURNS TABLE
    AS
    RETURN
        SELECT
            CASE MIN(a.propertyid)
                WHEN MAX(a.propertyid)
                    THEN 'N'
                ELSE 'Y'
            END CrossProp
        FROM 
            EmployeeJobStatus j
        JOIN 
            Assignment a
            ON a.id = j.jobid
        WHERE 
            j.employee = @empid
            AND j.startdate <= @AsOfDate
            AND j.endDate > @AsOfDate
        GROUP BY 
            j.employeeid;
    

    EDIT:

    You’d probably outer apply it, rather than cross. Nulls would then be employees with no assignments at the point in time.

    EDIT 2:

    Since you asked, below is a scalar function. Just be careful of scalars. Depending on where you use it in a query, the BEGIN END block can fool the optimizer into going RBAR.

    CREATE FUNCTION dbo.IsEmployeeCrossPropertyOnDate
        (
            @empid int
            , @asofdate datetime
        )
    RETURNS CHAR(1)
    AS
    BEGIN
        DECLARE @res CHAR(1) = 'N';
    
        SELECT @res = 
            CASE MIN(a.propertyid)
                WHEN MAX(a.propertyid)
                    THEN 'N'
                ELSE 'Y'
            END 
        FROM 
            EmployeeJobStatus j
        JOIN 
            Assignment a
            ON a.id = j.jobid
        WHERE 
            j.employee = @empid
            AND j.startdate <= @AsOfDate
            AND j.endDate > @AsOfDate
        GROUP BY 
            j.employeeid;
    
        RETURN @res;
    END;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to add a function that will create a csv for download
I'm attempting to create a function where I can pass names and values that
I'm attempting to create a recursive function that outputs a vector of strings that
I am attempting to create a constructor for my controller that references a function
I am attempting to create a generic mapping function that takes the values of
I'm attempting to write a function that will take a string and create a
I am attempting to create a report, utilizing a matrix, that only displays columns
I'm attempting to create a single Controller class to handle all foreseeable surveys that
I am attempting to create a search function for user profiles on my site.
I am attempting to write a dynamic dispatcher for a function that's templated on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.