Query who looks if employee has more than 14 vacation days










1















I want a query that checks if a employee has more than 14 vacation days in a year. I want to make a trigger of it. It is important that this is 14 days in total. But the employee could have 4 days in one month and 10 days in another. He don't need to take the days in one go.



I have something like this (query) I'm using SQL Server



select employeeid
from time
where datediff(day, dateStart, dateEnd) >=1
and year(dateEnd) = 2018
and timecat= 'vacationdays'
group by employeeid
having count(*) >=13


I thought I could use a datediff and have this count like 13 times. But it doesn't work.










share|improve this question






















  • Some sample data would really help here. Also, if you're using SQL Server, please add that tag to your question. SQL is just a language, not a specific product.

    – Tim Biegeleisen
    Nov 16 '18 at 10:55















1















I want a query that checks if a employee has more than 14 vacation days in a year. I want to make a trigger of it. It is important that this is 14 days in total. But the employee could have 4 days in one month and 10 days in another. He don't need to take the days in one go.



I have something like this (query) I'm using SQL Server



select employeeid
from time
where datediff(day, dateStart, dateEnd) >=1
and year(dateEnd) = 2018
and timecat= 'vacationdays'
group by employeeid
having count(*) >=13


I thought I could use a datediff and have this count like 13 times. But it doesn't work.










share|improve this question






















  • Some sample data would really help here. Also, if you're using SQL Server, please add that tag to your question. SQL is just a language, not a specific product.

    – Tim Biegeleisen
    Nov 16 '18 at 10:55













1












1








1








I want a query that checks if a employee has more than 14 vacation days in a year. I want to make a trigger of it. It is important that this is 14 days in total. But the employee could have 4 days in one month and 10 days in another. He don't need to take the days in one go.



I have something like this (query) I'm using SQL Server



select employeeid
from time
where datediff(day, dateStart, dateEnd) >=1
and year(dateEnd) = 2018
and timecat= 'vacationdays'
group by employeeid
having count(*) >=13


I thought I could use a datediff and have this count like 13 times. But it doesn't work.










share|improve this question














I want a query that checks if a employee has more than 14 vacation days in a year. I want to make a trigger of it. It is important that this is 14 days in total. But the employee could have 4 days in one month and 10 days in another. He don't need to take the days in one go.



I have something like this (query) I'm using SQL Server



select employeeid
from time
where datediff(day, dateStart, dateEnd) >=1
and year(dateEnd) = 2018
and timecat= 'vacationdays'
group by employeeid
having count(*) >=13


I thought I could use a datediff and have this count like 13 times. But it doesn't work.







sql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 10:53









DutchFatBoysDutchFatBoys

487




487












  • Some sample data would really help here. Also, if you're using SQL Server, please add that tag to your question. SQL is just a language, not a specific product.

    – Tim Biegeleisen
    Nov 16 '18 at 10:55

















  • Some sample data would really help here. Also, if you're using SQL Server, please add that tag to your question. SQL is just a language, not a specific product.

    – Tim Biegeleisen
    Nov 16 '18 at 10:55
















Some sample data would really help here. Also, if you're using SQL Server, please add that tag to your question. SQL is just a language, not a specific product.

– Tim Biegeleisen
Nov 16 '18 at 10:55





Some sample data would really help here. Also, if you're using SQL Server, please add that tag to your question. SQL is just a language, not a specific product.

– Tim Biegeleisen
Nov 16 '18 at 10:55












2 Answers
2






active

oldest

votes


















1














You need to sum the date differences:



select employeeid, sum(datediff(day, dateStart, dateEnd) + 1) AS total
from time
where year(dateEnd) = 2018
and timecat= 'vacationdays'
group by employeeid
having SUM(datediff(day, dateStart, dateEnd) + 1) > 14


Consider the +1 in SUM because datediff returns 1 for two dates like '2018-11-16' and '2018-11-17', but if these are the dateStart and dateEnd you want the result to be 2.

There is still one problem remaining: what happens if dateStart and dateEnd are not dates of the same year!






share|improve this answer

























  • Yes that problem I've also considered. But in my case that is not a problem. Thank you!

    – DutchFatBoys
    Nov 16 '18 at 11:46


















0














Count(*) just counts the number of rows.
This means if have an employee with two vacations one of 10 days and one of 4 days, (i assume that) this are only two rows in rows in your table and count star returns 2.



You can you the function SUM()



select employeeid, sum( datediff(day, dateStart, dateEnd) ) as sum
from time
where datediff(day, dateStart, dateEnd) >=1
and year(dateEnd) = 2018
and timecat= 'vacationdays'
group by employeeid
having sum >=13


I don't know the structure of your table, therefore I'm not quite sure if the query is correct, but here a further helpful links.



https://www.w3schools.com/sql/sql_count_avg_sum.asp



Getting the sum of a datediff result






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%2f53336413%2fquery-who-looks-if-employee-has-more-than-14-vacation-days%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You need to sum the date differences:



    select employeeid, sum(datediff(day, dateStart, dateEnd) + 1) AS total
    from time
    where year(dateEnd) = 2018
    and timecat= 'vacationdays'
    group by employeeid
    having SUM(datediff(day, dateStart, dateEnd) + 1) > 14


    Consider the +1 in SUM because datediff returns 1 for two dates like '2018-11-16' and '2018-11-17', but if these are the dateStart and dateEnd you want the result to be 2.

    There is still one problem remaining: what happens if dateStart and dateEnd are not dates of the same year!






    share|improve this answer

























    • Yes that problem I've also considered. But in my case that is not a problem. Thank you!

      – DutchFatBoys
      Nov 16 '18 at 11:46















    1














    You need to sum the date differences:



    select employeeid, sum(datediff(day, dateStart, dateEnd) + 1) AS total
    from time
    where year(dateEnd) = 2018
    and timecat= 'vacationdays'
    group by employeeid
    having SUM(datediff(day, dateStart, dateEnd) + 1) > 14


    Consider the +1 in SUM because datediff returns 1 for two dates like '2018-11-16' and '2018-11-17', but if these are the dateStart and dateEnd you want the result to be 2.

    There is still one problem remaining: what happens if dateStart and dateEnd are not dates of the same year!






    share|improve this answer

























    • Yes that problem I've also considered. But in my case that is not a problem. Thank you!

      – DutchFatBoys
      Nov 16 '18 at 11:46













    1












    1








    1







    You need to sum the date differences:



    select employeeid, sum(datediff(day, dateStart, dateEnd) + 1) AS total
    from time
    where year(dateEnd) = 2018
    and timecat= 'vacationdays'
    group by employeeid
    having SUM(datediff(day, dateStart, dateEnd) + 1) > 14


    Consider the +1 in SUM because datediff returns 1 for two dates like '2018-11-16' and '2018-11-17', but if these are the dateStart and dateEnd you want the result to be 2.

    There is still one problem remaining: what happens if dateStart and dateEnd are not dates of the same year!






    share|improve this answer















    You need to sum the date differences:



    select employeeid, sum(datediff(day, dateStart, dateEnd) + 1) AS total
    from time
    where year(dateEnd) = 2018
    and timecat= 'vacationdays'
    group by employeeid
    having SUM(datediff(day, dateStart, dateEnd) + 1) > 14


    Consider the +1 in SUM because datediff returns 1 for two dates like '2018-11-16' and '2018-11-17', but if these are the dateStart and dateEnd you want the result to be 2.

    There is still one problem remaining: what happens if dateStart and dateEnd are not dates of the same year!







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 16 '18 at 11:12

























    answered Nov 16 '18 at 11:02









    forpasforpas

    19.5k4830




    19.5k4830












    • Yes that problem I've also considered. But in my case that is not a problem. Thank you!

      – DutchFatBoys
      Nov 16 '18 at 11:46

















    • Yes that problem I've also considered. But in my case that is not a problem. Thank you!

      – DutchFatBoys
      Nov 16 '18 at 11:46
















    Yes that problem I've also considered. But in my case that is not a problem. Thank you!

    – DutchFatBoys
    Nov 16 '18 at 11:46





    Yes that problem I've also considered. But in my case that is not a problem. Thank you!

    – DutchFatBoys
    Nov 16 '18 at 11:46













    0














    Count(*) just counts the number of rows.
    This means if have an employee with two vacations one of 10 days and one of 4 days, (i assume that) this are only two rows in rows in your table and count star returns 2.



    You can you the function SUM()



    select employeeid, sum( datediff(day, dateStart, dateEnd) ) as sum
    from time
    where datediff(day, dateStart, dateEnd) >=1
    and year(dateEnd) = 2018
    and timecat= 'vacationdays'
    group by employeeid
    having sum >=13


    I don't know the structure of your table, therefore I'm not quite sure if the query is correct, but here a further helpful links.



    https://www.w3schools.com/sql/sql_count_avg_sum.asp



    Getting the sum of a datediff result






    share|improve this answer



























      0














      Count(*) just counts the number of rows.
      This means if have an employee with two vacations one of 10 days and one of 4 days, (i assume that) this are only two rows in rows in your table and count star returns 2.



      You can you the function SUM()



      select employeeid, sum( datediff(day, dateStart, dateEnd) ) as sum
      from time
      where datediff(day, dateStart, dateEnd) >=1
      and year(dateEnd) = 2018
      and timecat= 'vacationdays'
      group by employeeid
      having sum >=13


      I don't know the structure of your table, therefore I'm not quite sure if the query is correct, but here a further helpful links.



      https://www.w3schools.com/sql/sql_count_avg_sum.asp



      Getting the sum of a datediff result






      share|improve this answer

























        0












        0








        0







        Count(*) just counts the number of rows.
        This means if have an employee with two vacations one of 10 days and one of 4 days, (i assume that) this are only two rows in rows in your table and count star returns 2.



        You can you the function SUM()



        select employeeid, sum( datediff(day, dateStart, dateEnd) ) as sum
        from time
        where datediff(day, dateStart, dateEnd) >=1
        and year(dateEnd) = 2018
        and timecat= 'vacationdays'
        group by employeeid
        having sum >=13


        I don't know the structure of your table, therefore I'm not quite sure if the query is correct, but here a further helpful links.



        https://www.w3schools.com/sql/sql_count_avg_sum.asp



        Getting the sum of a datediff result






        share|improve this answer













        Count(*) just counts the number of rows.
        This means if have an employee with two vacations one of 10 days and one of 4 days, (i assume that) this are only two rows in rows in your table and count star returns 2.



        You can you the function SUM()



        select employeeid, sum( datediff(day, dateStart, dateEnd) ) as sum
        from time
        where datediff(day, dateStart, dateEnd) >=1
        and year(dateEnd) = 2018
        and timecat= 'vacationdays'
        group by employeeid
        having sum >=13


        I don't know the structure of your table, therefore I'm not quite sure if the query is correct, but here a further helpful links.



        https://www.w3schools.com/sql/sql_count_avg_sum.asp



        Getting the sum of a datediff result







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 11:09









        hobitanshobitans

        11




        11



























            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%2f53336413%2fquery-who-looks-if-employee-has-more-than-14-vacation-days%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

            27

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            Category:Rhetoric