t-SQL - Determine the amount of days employee worked - within an existing code (to calculate the amount of employee's FTE per Period)










0















My goal is to calculate the number of Full-Time Equivalent personnel (FTE) - Full and Part time - per working period



I have the following code to calculate the number of employees FT / PT groupped by Region and Status (FT=1/PT=2):



Code 1 (Main):



DECLARE @MonthBOP date = '07-01-2017'
DECLARE @MonthEOP date = '09-30-2018'
DECLARE @GLRegionsWHID int = 705
DECLARE @GLProgramsWHID int = -1

SELECT
glrn.WHID AS [RegionID]
,dol.WHID AS [StatusType]
,COUNT (*) AS [CountClients]

FROM [WH].[Dimension].[PaycomEmployee] empl
INNER JOIN Dimension.PaycomDOLStatus dol ON empl.PaycomDOLStatusFK = dol.WHID
INNER JOIN Dimension.PaycomDepartment dept ON empl.PaycomDepartmentFK = dept.WHID
INNER JOIN Dimension.GLProgram glpgm ON dept.GLProgramFK = glpgm.WHID
INNER JOIN Dimension.GLRegion glrn ON glpgm.GLRegionFK = glrn.WHID

WHERE

((dol.WHID IN (1,2)))
AND ((glrn.WHID IN (@GLRegionsWHID)) OR (-1 IN (@GLRegionsWHID)))
AND ((glpgm.WHID IN (@GLProgramsWHID)) OR (-1 IN (@GLProgramsWHID)))

GROUP BY
glrn.WHID
,dol.WHID


The result is the following (below):



enter image description here



But I also have another code part, which determines - the employee working period:



Code 2:



Min(@MonthEOP, empl.DateTerminated) - Max(@MonthBOP, empl.DateHired) + 1 =
[Number Days employed in Period]
WHERE empl.DateTerminated > @MonthBOP


My question is - How to combine correctly the main code (Code 1) and the additional part (Code 2) - so it would be all in one code
or - any sort of reference in Code 1 into Code 2



My goal is to determine the Employee Working Period and to bring it into
Code 1 (Main)



Thank you for help!










share|improve this question




























    0















    My goal is to calculate the number of Full-Time Equivalent personnel (FTE) - Full and Part time - per working period



    I have the following code to calculate the number of employees FT / PT groupped by Region and Status (FT=1/PT=2):



    Code 1 (Main):



    DECLARE @MonthBOP date = '07-01-2017'
    DECLARE @MonthEOP date = '09-30-2018'
    DECLARE @GLRegionsWHID int = 705
    DECLARE @GLProgramsWHID int = -1

    SELECT
    glrn.WHID AS [RegionID]
    ,dol.WHID AS [StatusType]
    ,COUNT (*) AS [CountClients]

    FROM [WH].[Dimension].[PaycomEmployee] empl
    INNER JOIN Dimension.PaycomDOLStatus dol ON empl.PaycomDOLStatusFK = dol.WHID
    INNER JOIN Dimension.PaycomDepartment dept ON empl.PaycomDepartmentFK = dept.WHID
    INNER JOIN Dimension.GLProgram glpgm ON dept.GLProgramFK = glpgm.WHID
    INNER JOIN Dimension.GLRegion glrn ON glpgm.GLRegionFK = glrn.WHID

    WHERE

    ((dol.WHID IN (1,2)))
    AND ((glrn.WHID IN (@GLRegionsWHID)) OR (-1 IN (@GLRegionsWHID)))
    AND ((glpgm.WHID IN (@GLProgramsWHID)) OR (-1 IN (@GLProgramsWHID)))

    GROUP BY
    glrn.WHID
    ,dol.WHID


    The result is the following (below):



    enter image description here



    But I also have another code part, which determines - the employee working period:



    Code 2:



    Min(@MonthEOP, empl.DateTerminated) - Max(@MonthBOP, empl.DateHired) + 1 =
    [Number Days employed in Period]
    WHERE empl.DateTerminated > @MonthBOP


    My question is - How to combine correctly the main code (Code 1) and the additional part (Code 2) - so it would be all in one code
    or - any sort of reference in Code 1 into Code 2



    My goal is to determine the Employee Working Period and to bring it into
    Code 1 (Main)



    Thank you for help!










    share|improve this question


























      0












      0








      0








      My goal is to calculate the number of Full-Time Equivalent personnel (FTE) - Full and Part time - per working period



      I have the following code to calculate the number of employees FT / PT groupped by Region and Status (FT=1/PT=2):



      Code 1 (Main):



      DECLARE @MonthBOP date = '07-01-2017'
      DECLARE @MonthEOP date = '09-30-2018'
      DECLARE @GLRegionsWHID int = 705
      DECLARE @GLProgramsWHID int = -1

      SELECT
      glrn.WHID AS [RegionID]
      ,dol.WHID AS [StatusType]
      ,COUNT (*) AS [CountClients]

      FROM [WH].[Dimension].[PaycomEmployee] empl
      INNER JOIN Dimension.PaycomDOLStatus dol ON empl.PaycomDOLStatusFK = dol.WHID
      INNER JOIN Dimension.PaycomDepartment dept ON empl.PaycomDepartmentFK = dept.WHID
      INNER JOIN Dimension.GLProgram glpgm ON dept.GLProgramFK = glpgm.WHID
      INNER JOIN Dimension.GLRegion glrn ON glpgm.GLRegionFK = glrn.WHID

      WHERE

      ((dol.WHID IN (1,2)))
      AND ((glrn.WHID IN (@GLRegionsWHID)) OR (-1 IN (@GLRegionsWHID)))
      AND ((glpgm.WHID IN (@GLProgramsWHID)) OR (-1 IN (@GLProgramsWHID)))

      GROUP BY
      glrn.WHID
      ,dol.WHID


      The result is the following (below):



      enter image description here



      But I also have another code part, which determines - the employee working period:



      Code 2:



      Min(@MonthEOP, empl.DateTerminated) - Max(@MonthBOP, empl.DateHired) + 1 =
      [Number Days employed in Period]
      WHERE empl.DateTerminated > @MonthBOP


      My question is - How to combine correctly the main code (Code 1) and the additional part (Code 2) - so it would be all in one code
      or - any sort of reference in Code 1 into Code 2



      My goal is to determine the Employee Working Period and to bring it into
      Code 1 (Main)



      Thank you for help!










      share|improve this question
















      My goal is to calculate the number of Full-Time Equivalent personnel (FTE) - Full and Part time - per working period



      I have the following code to calculate the number of employees FT / PT groupped by Region and Status (FT=1/PT=2):



      Code 1 (Main):



      DECLARE @MonthBOP date = '07-01-2017'
      DECLARE @MonthEOP date = '09-30-2018'
      DECLARE @GLRegionsWHID int = 705
      DECLARE @GLProgramsWHID int = -1

      SELECT
      glrn.WHID AS [RegionID]
      ,dol.WHID AS [StatusType]
      ,COUNT (*) AS [CountClients]

      FROM [WH].[Dimension].[PaycomEmployee] empl
      INNER JOIN Dimension.PaycomDOLStatus dol ON empl.PaycomDOLStatusFK = dol.WHID
      INNER JOIN Dimension.PaycomDepartment dept ON empl.PaycomDepartmentFK = dept.WHID
      INNER JOIN Dimension.GLProgram glpgm ON dept.GLProgramFK = glpgm.WHID
      INNER JOIN Dimension.GLRegion glrn ON glpgm.GLRegionFK = glrn.WHID

      WHERE

      ((dol.WHID IN (1,2)))
      AND ((glrn.WHID IN (@GLRegionsWHID)) OR (-1 IN (@GLRegionsWHID)))
      AND ((glpgm.WHID IN (@GLProgramsWHID)) OR (-1 IN (@GLProgramsWHID)))

      GROUP BY
      glrn.WHID
      ,dol.WHID


      The result is the following (below):



      enter image description here



      But I also have another code part, which determines - the employee working period:



      Code 2:



      Min(@MonthEOP, empl.DateTerminated) - Max(@MonthBOP, empl.DateHired) + 1 =
      [Number Days employed in Period]
      WHERE empl.DateTerminated > @MonthBOP


      My question is - How to combine correctly the main code (Code 1) and the additional part (Code 2) - so it would be all in one code
      or - any sort of reference in Code 1 into Code 2



      My goal is to determine the Employee Working Period and to bring it into
      Code 1 (Main)



      Thank you for help!







      sql-server tsql ssms






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 5:48









      marc_s

      579k12911171263




      579k12911171263










      asked Nov 14 '18 at 0:41









      Hell-1931Hell-1931

      768




      768






















          1 Answer
          1






          active

          oldest

          votes


















          0














          You can't use MIN() or MAX() like you do in code2.



          Use CASE instead.



          CASE
          WHEN @MonthBOP > empl.DateTerminated THEN @MonthBOP
          WHEN @MonthEOP < empl.DateTerminated THEN @MonthEOP
          ELSE empl.DateTerminated
          END
          -
          CASE
          WHEN @MonthEOP < empl.DateHired THEN @MonthEOP
          WHEN @MonthBOP > empl.DateHired THEN @MonthBOP
          ELSE empl.DateHired
          END
          AS [Number Days employed in Period]





          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291542%2ft-sql-determine-the-amount-of-days-employee-worked-within-an-existing-code%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You can't use MIN() or MAX() like you do in code2.



            Use CASE instead.



            CASE
            WHEN @MonthBOP > empl.DateTerminated THEN @MonthBOP
            WHEN @MonthEOP < empl.DateTerminated THEN @MonthEOP
            ELSE empl.DateTerminated
            END
            -
            CASE
            WHEN @MonthEOP < empl.DateHired THEN @MonthEOP
            WHEN @MonthBOP > empl.DateHired THEN @MonthBOP
            ELSE empl.DateHired
            END
            AS [Number Days employed in Period]





            share|improve this answer





























              0














              You can't use MIN() or MAX() like you do in code2.



              Use CASE instead.



              CASE
              WHEN @MonthBOP > empl.DateTerminated THEN @MonthBOP
              WHEN @MonthEOP < empl.DateTerminated THEN @MonthEOP
              ELSE empl.DateTerminated
              END
              -
              CASE
              WHEN @MonthEOP < empl.DateHired THEN @MonthEOP
              WHEN @MonthBOP > empl.DateHired THEN @MonthBOP
              ELSE empl.DateHired
              END
              AS [Number Days employed in Period]





              share|improve this answer



























                0












                0








                0







                You can't use MIN() or MAX() like you do in code2.



                Use CASE instead.



                CASE
                WHEN @MonthBOP > empl.DateTerminated THEN @MonthBOP
                WHEN @MonthEOP < empl.DateTerminated THEN @MonthEOP
                ELSE empl.DateTerminated
                END
                -
                CASE
                WHEN @MonthEOP < empl.DateHired THEN @MonthEOP
                WHEN @MonthBOP > empl.DateHired THEN @MonthBOP
                ELSE empl.DateHired
                END
                AS [Number Days employed in Period]





                share|improve this answer















                You can't use MIN() or MAX() like you do in code2.



                Use CASE instead.



                CASE
                WHEN @MonthBOP > empl.DateTerminated THEN @MonthBOP
                WHEN @MonthEOP < empl.DateTerminated THEN @MonthEOP
                ELSE empl.DateTerminated
                END
                -
                CASE
                WHEN @MonthEOP < empl.DateHired THEN @MonthEOP
                WHEN @MonthBOP > empl.DateHired THEN @MonthBOP
                ELSE empl.DateHired
                END
                AS [Number Days employed in Period]






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 14 '18 at 6:16

























                answered Nov 14 '18 at 6:04









                MatBailieMatBailie

                59.7k1476111




                59.7k1476111





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291542%2ft-sql-determine-the-amount-of-days-employee-worked-within-an-existing-code%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo