t-SQL - Determine the amount of days employee worked - within an existing code (to calculate the amount of employee's FTE per Period)
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):
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
add a comment |
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):
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
add a comment |
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):
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
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):
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
sql-server tsql ssms
edited Nov 14 '18 at 5:48
marc_s
574k12811091256
574k12811091256
asked Nov 14 '18 at 0:41
Hell-1931Hell-1931
698
698
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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]
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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]
add a comment |
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]
add a comment |
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]
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]
edited Nov 14 '18 at 6:16
answered Nov 14 '18 at 6:04
MatBailieMatBailie
59.3k1475110
59.3k1475110
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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