group by year on multiple date columns mysql Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Add a column with a default value to an existing table in SQL ServerCan I concatenate multiple MySQL rows into one field?Should I use the datetime or timestamp data type in MySQL?MySQL Query GROUP BY day / month / yearGroup By Multiple ColumnsRetrieving the last record in each group - MySQLUsing group by on multiple columnsSelect first row in each GROUP BY group?How to reset AUTO_INCREMENT in MySQL?How to import an SQL file using the command line in MySQL?

What do you call a floor made of glass so you can see through the floor?

Why are the trig functions versine, haversine, exsecant, etc, rarely used in modern mathematics?

Does classifying an integer as a discrete log require it be part of a multiplicative group?

Why wasn't DOSKEY integrated with COMMAND.COM?

What is homebrew?

Do I really need to have a message in a novel to appeal to readers?

Compare a given version number in the form major.minor.build.patch and see if one is less than the other

Is there any way for the UK Prime Minister to make a motion directly dependent on Government confidence?

First console to have temporary backward compatibility

Can anything be seen from the center of the Boötes void? How dark would it be?

Wu formula for manifolds with boundary

Can an alien society believe that their star system is the universe?

How to react to hostile behavior from a senior developer?

How to Make a Beautiful Stacked 3D Plot

Has negative voting ever been officially implemented in elections, or seriously proposed, or even studied?

Closed form of recurrent arithmetic series summation

Why are both D and D# fitting into my E minor key?

Fundamental Solution of the Pell Equation

Is this homebrew Lady of Pain warlock patron balanced?

Why aren't air breathing engines used as small first stages

2001: A Space Odyssey's use of the song "Daisy Bell" (Bicycle Built for Two); life imitates art or vice-versa?

Delete nth line from bottom

What does the "x" in "x86" represent?

Irreducible of finite Krull dimension implies quasi-compact?



group by year on multiple date columns mysql



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Add a column with a default value to an existing table in SQL ServerCan I concatenate multiple MySQL rows into one field?Should I use the datetime or timestamp data type in MySQL?MySQL Query GROUP BY day / month / yearGroup By Multiple ColumnsRetrieving the last record in each group - MySQLUsing group by on multiple columnsSelect first row in each GROUP BY group?How to reset AUTO_INCREMENT in MySQL?How to import an SQL file using the command line in MySQL?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I have table as following:



hours | ... | task_assigned | task_deadline | task_completion
----------------------------------------------------------------
123 | ... | 2019-08-01 | - | -
234 | ... | - | 2018-08-01 | 2019-08-01
145 | ... | 2017-08-01 | 2017-08-01 | 2018-01-01


I want to calculate total hours for each year, i.e. grouping by year.



Currently I'm only taking into account task_completion field.



If there's no value in task_completion field, the record is not included in SUM calculation.



To elaborate further, say for year 2019, row 1 and 1 both should be considered. Hence the total hours should be 123 + 234 = 357.



And for year 2018, row 2 and 3.



Similarly, for year 2017, row 3.



SELECT YEAR(task_completion) as year, ROUND(SUM(total_hours), 2) as hours 
FROM task
GROUP BY year
HAVING year BETWEEN '$year_from' AND '$year_to'


The resultset:



year | hours
--------------------
2017 | <somevalue>
2018 | <somevalue>
2019 | <somevalue>


How can I include other two date fields too?










share|improve this question



















  • 2





    How should your result set look like?

    – Pavel Smirnov
    Mar 22 at 9:38











  • @PavelSmirnov I've updated the post. please check.

    – Azima
    Mar 22 at 9:42











  • Do you want your resultset to include the other two date columns?

    – helloworld
    Mar 22 at 9:43











  • @helloworld .. yes.. currently if there's no value for task_completion field, then that record is not used for sum calculation.

    – Azima
    Mar 22 at 9:44

















1















I have table as following:



hours | ... | task_assigned | task_deadline | task_completion
----------------------------------------------------------------
123 | ... | 2019-08-01 | - | -
234 | ... | - | 2018-08-01 | 2019-08-01
145 | ... | 2017-08-01 | 2017-08-01 | 2018-01-01


I want to calculate total hours for each year, i.e. grouping by year.



Currently I'm only taking into account task_completion field.



If there's no value in task_completion field, the record is not included in SUM calculation.



To elaborate further, say for year 2019, row 1 and 1 both should be considered. Hence the total hours should be 123 + 234 = 357.



And for year 2018, row 2 and 3.



Similarly, for year 2017, row 3.



SELECT YEAR(task_completion) as year, ROUND(SUM(total_hours), 2) as hours 
FROM task
GROUP BY year
HAVING year BETWEEN '$year_from' AND '$year_to'


The resultset:



year | hours
--------------------
2017 | <somevalue>
2018 | <somevalue>
2019 | <somevalue>


How can I include other two date fields too?










share|improve this question



















  • 2





    How should your result set look like?

    – Pavel Smirnov
    Mar 22 at 9:38











  • @PavelSmirnov I've updated the post. please check.

    – Azima
    Mar 22 at 9:42











  • Do you want your resultset to include the other two date columns?

    – helloworld
    Mar 22 at 9:43











  • @helloworld .. yes.. currently if there's no value for task_completion field, then that record is not used for sum calculation.

    – Azima
    Mar 22 at 9:44













1












1








1








I have table as following:



hours | ... | task_assigned | task_deadline | task_completion
----------------------------------------------------------------
123 | ... | 2019-08-01 | - | -
234 | ... | - | 2018-08-01 | 2019-08-01
145 | ... | 2017-08-01 | 2017-08-01 | 2018-01-01


I want to calculate total hours for each year, i.e. grouping by year.



Currently I'm only taking into account task_completion field.



If there's no value in task_completion field, the record is not included in SUM calculation.



To elaborate further, say for year 2019, row 1 and 1 both should be considered. Hence the total hours should be 123 + 234 = 357.



And for year 2018, row 2 and 3.



Similarly, for year 2017, row 3.



SELECT YEAR(task_completion) as year, ROUND(SUM(total_hours), 2) as hours 
FROM task
GROUP BY year
HAVING year BETWEEN '$year_from' AND '$year_to'


The resultset:



year | hours
--------------------
2017 | <somevalue>
2018 | <somevalue>
2019 | <somevalue>


How can I include other two date fields too?










share|improve this question
















I have table as following:



hours | ... | task_assigned | task_deadline | task_completion
----------------------------------------------------------------
123 | ... | 2019-08-01 | - | -
234 | ... | - | 2018-08-01 | 2019-08-01
145 | ... | 2017-08-01 | 2017-08-01 | 2018-01-01


I want to calculate total hours for each year, i.e. grouping by year.



Currently I'm only taking into account task_completion field.



If there's no value in task_completion field, the record is not included in SUM calculation.



To elaborate further, say for year 2019, row 1 and 1 both should be considered. Hence the total hours should be 123 + 234 = 357.



And for year 2018, row 2 and 3.



Similarly, for year 2017, row 3.



SELECT YEAR(task_completion) as year, ROUND(SUM(total_hours), 2) as hours 
FROM task
GROUP BY year
HAVING year BETWEEN '$year_from' AND '$year_to'


The resultset:



year | hours
--------------------
2017 | <somevalue>
2018 | <somevalue>
2019 | <somevalue>


How can I include other two date fields too?







mysql sql group-by






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 9:51







Azima

















asked Mar 22 at 9:37









AzimaAzima

846823




846823







  • 2





    How should your result set look like?

    – Pavel Smirnov
    Mar 22 at 9:38











  • @PavelSmirnov I've updated the post. please check.

    – Azima
    Mar 22 at 9:42











  • Do you want your resultset to include the other two date columns?

    – helloworld
    Mar 22 at 9:43











  • @helloworld .. yes.. currently if there's no value for task_completion field, then that record is not used for sum calculation.

    – Azima
    Mar 22 at 9:44












  • 2





    How should your result set look like?

    – Pavel Smirnov
    Mar 22 at 9:38











  • @PavelSmirnov I've updated the post. please check.

    – Azima
    Mar 22 at 9:42











  • Do you want your resultset to include the other two date columns?

    – helloworld
    Mar 22 at 9:43











  • @helloworld .. yes.. currently if there's no value for task_completion field, then that record is not used for sum calculation.

    – Azima
    Mar 22 at 9:44







2




2





How should your result set look like?

– Pavel Smirnov
Mar 22 at 9:38





How should your result set look like?

– Pavel Smirnov
Mar 22 at 9:38













@PavelSmirnov I've updated the post. please check.

– Azima
Mar 22 at 9:42





@PavelSmirnov I've updated the post. please check.

– Azima
Mar 22 at 9:42













Do you want your resultset to include the other two date columns?

– helloworld
Mar 22 at 9:43





Do you want your resultset to include the other two date columns?

– helloworld
Mar 22 at 9:43













@helloworld .. yes.. currently if there's no value for task_completion field, then that record is not used for sum calculation.

– Azima
Mar 22 at 9:44





@helloworld .. yes.. currently if there's no value for task_completion field, then that record is not used for sum calculation.

– Azima
Mar 22 at 9:44












3 Answers
3






active

oldest

votes


















2














You want to consider each row once for each of its years. Use UNION to get these years:



select year, round(sum(total_hours), 2) as hours
from
(
select year(task_assigned) as year, total_hours from task
union
select year(task_deadline) as year, total_hours from task
union
select year(task_completion) as year, total_hours from task
) years_and_hours
group by year
having year between $year_from and $year_to
order by year;


If you want to consider a row with one year twice or thrice also as often in the sum, then change UNION to UNION ALL.






share|improve this answer






























    1














    Basically, you want to unpivot the data. I will assume that the - represents a NULL value and your dates are real dates.



    select year(dte) as year, sum(total_hours) as hours
    from ((select task_assigned as dte, total_hours
    from task
    ) union all
    (select task_deadline, total_hours
    from task
    ) union all
    (select task_completion, total_hours
    from task
    )
    ) d
    where dte is not null
    group by year(dte)
    order by year(dte);


    Based on your sample data, the round() is not necessary so I removed it.



    If you want to filter for particular years, the filtering should be in a where clause -- so it filters the data before aggregation.



    Change the where to:



    where year(dte) >= ? and year(dte) <= ?


    or:



    where dte >= ? and dte <= ?


    to pass in the dates.



    The ? are for parameter placeholders. Learn how to use parameters rather than munging query strings.






    share|improve this answer























    • passing date parameters will only concern task_assigned field?

      – Azima
      Mar 22 at 12:14











    • @Azima . . . I don't understand your comment. There is no such reference in the question.

      – Gordon Linoff
      Mar 22 at 12:32











    • dte field is task_assigned right?

      – Azima
      Mar 22 at 13:23











    • @Azima . . . You accepted an answer; why do you continue to ask questions? If you have another question, then ask as a question. dte is all three columns combined into a single column, so it is sometimes task_assigned.

      – Gordon Linoff
      Mar 22 at 14:03


















    0














    This answer is no langer valid with the updated request.




    If I understand correctly, you want to use task_assigned if the task_completion is still null. Use COALEASCE for this.



    SELECT
    YEAR(COALESCE(task_completion, task_assigned)) as year,
    ROUND(SUM(total_hours), 2) as hours
    FROM task
    GROUP BY year
    HAVING year BETWEEN $year_from AND $year_to
    ORDER BY year;


    (I don't think you actually want to use task_deadline, too, for how could a task get completed before getting assigned first? If such can occur, then include it in the COALESCE expression. Probably: COALESCE(task_completion, task_assigned, task_deadline)` then.)






    share|improve this answer

























    • I've updated the post.. hope it will clear the confusion.

      – Azima
      Mar 22 at 9:53











    • but say, a row has task_completion value as 2019-09-09 and task_assigned as 2018-09-21. And I'm looking SUM(hours) for 2018 and hence this row should also be considered. But with COALESCE(), since task_completion is not null, it will take 2019 as year and hence ignore the row.

      – Azima
      Mar 22 at 10:02











    • Sorry, I was wrong. I'll write another answer.

      – Thorsten Kettner
      Mar 22 at 10:03











    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%2f55296674%2fgroup-by-year-on-multiple-date-columns-mysql%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You want to consider each row once for each of its years. Use UNION to get these years:



    select year, round(sum(total_hours), 2) as hours
    from
    (
    select year(task_assigned) as year, total_hours from task
    union
    select year(task_deadline) as year, total_hours from task
    union
    select year(task_completion) as year, total_hours from task
    ) years_and_hours
    group by year
    having year between $year_from and $year_to
    order by year;


    If you want to consider a row with one year twice or thrice also as often in the sum, then change UNION to UNION ALL.






    share|improve this answer



























      2














      You want to consider each row once for each of its years. Use UNION to get these years:



      select year, round(sum(total_hours), 2) as hours
      from
      (
      select year(task_assigned) as year, total_hours from task
      union
      select year(task_deadline) as year, total_hours from task
      union
      select year(task_completion) as year, total_hours from task
      ) years_and_hours
      group by year
      having year between $year_from and $year_to
      order by year;


      If you want to consider a row with one year twice or thrice also as often in the sum, then change UNION to UNION ALL.






      share|improve this answer

























        2












        2








        2







        You want to consider each row once for each of its years. Use UNION to get these years:



        select year, round(sum(total_hours), 2) as hours
        from
        (
        select year(task_assigned) as year, total_hours from task
        union
        select year(task_deadline) as year, total_hours from task
        union
        select year(task_completion) as year, total_hours from task
        ) years_and_hours
        group by year
        having year between $year_from and $year_to
        order by year;


        If you want to consider a row with one year twice or thrice also as often in the sum, then change UNION to UNION ALL.






        share|improve this answer













        You want to consider each row once for each of its years. Use UNION to get these years:



        select year, round(sum(total_hours), 2) as hours
        from
        (
        select year(task_assigned) as year, total_hours from task
        union
        select year(task_deadline) as year, total_hours from task
        union
        select year(task_completion) as year, total_hours from task
        ) years_and_hours
        group by year
        having year between $year_from and $year_to
        order by year;


        If you want to consider a row with one year twice or thrice also as often in the sum, then change UNION to UNION ALL.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 22 at 10:07









        Thorsten KettnerThorsten Kettner

        53.1k32744




        53.1k32744























            1














            Basically, you want to unpivot the data. I will assume that the - represents a NULL value and your dates are real dates.



            select year(dte) as year, sum(total_hours) as hours
            from ((select task_assigned as dte, total_hours
            from task
            ) union all
            (select task_deadline, total_hours
            from task
            ) union all
            (select task_completion, total_hours
            from task
            )
            ) d
            where dte is not null
            group by year(dte)
            order by year(dte);


            Based on your sample data, the round() is not necessary so I removed it.



            If you want to filter for particular years, the filtering should be in a where clause -- so it filters the data before aggregation.



            Change the where to:



            where year(dte) >= ? and year(dte) <= ?


            or:



            where dte >= ? and dte <= ?


            to pass in the dates.



            The ? are for parameter placeholders. Learn how to use parameters rather than munging query strings.






            share|improve this answer























            • passing date parameters will only concern task_assigned field?

              – Azima
              Mar 22 at 12:14











            • @Azima . . . I don't understand your comment. There is no such reference in the question.

              – Gordon Linoff
              Mar 22 at 12:32











            • dte field is task_assigned right?

              – Azima
              Mar 22 at 13:23











            • @Azima . . . You accepted an answer; why do you continue to ask questions? If you have another question, then ask as a question. dte is all three columns combined into a single column, so it is sometimes task_assigned.

              – Gordon Linoff
              Mar 22 at 14:03















            1














            Basically, you want to unpivot the data. I will assume that the - represents a NULL value and your dates are real dates.



            select year(dte) as year, sum(total_hours) as hours
            from ((select task_assigned as dte, total_hours
            from task
            ) union all
            (select task_deadline, total_hours
            from task
            ) union all
            (select task_completion, total_hours
            from task
            )
            ) d
            where dte is not null
            group by year(dte)
            order by year(dte);


            Based on your sample data, the round() is not necessary so I removed it.



            If you want to filter for particular years, the filtering should be in a where clause -- so it filters the data before aggregation.



            Change the where to:



            where year(dte) >= ? and year(dte) <= ?


            or:



            where dte >= ? and dte <= ?


            to pass in the dates.



            The ? are for parameter placeholders. Learn how to use parameters rather than munging query strings.






            share|improve this answer























            • passing date parameters will only concern task_assigned field?

              – Azima
              Mar 22 at 12:14











            • @Azima . . . I don't understand your comment. There is no such reference in the question.

              – Gordon Linoff
              Mar 22 at 12:32











            • dte field is task_assigned right?

              – Azima
              Mar 22 at 13:23











            • @Azima . . . You accepted an answer; why do you continue to ask questions? If you have another question, then ask as a question. dte is all three columns combined into a single column, so it is sometimes task_assigned.

              – Gordon Linoff
              Mar 22 at 14:03













            1












            1








            1







            Basically, you want to unpivot the data. I will assume that the - represents a NULL value and your dates are real dates.



            select year(dte) as year, sum(total_hours) as hours
            from ((select task_assigned as dte, total_hours
            from task
            ) union all
            (select task_deadline, total_hours
            from task
            ) union all
            (select task_completion, total_hours
            from task
            )
            ) d
            where dte is not null
            group by year(dte)
            order by year(dte);


            Based on your sample data, the round() is not necessary so I removed it.



            If you want to filter for particular years, the filtering should be in a where clause -- so it filters the data before aggregation.



            Change the where to:



            where year(dte) >= ? and year(dte) <= ?


            or:



            where dte >= ? and dte <= ?


            to pass in the dates.



            The ? are for parameter placeholders. Learn how to use parameters rather than munging query strings.






            share|improve this answer













            Basically, you want to unpivot the data. I will assume that the - represents a NULL value and your dates are real dates.



            select year(dte) as year, sum(total_hours) as hours
            from ((select task_assigned as dte, total_hours
            from task
            ) union all
            (select task_deadline, total_hours
            from task
            ) union all
            (select task_completion, total_hours
            from task
            )
            ) d
            where dte is not null
            group by year(dte)
            order by year(dte);


            Based on your sample data, the round() is not necessary so I removed it.



            If you want to filter for particular years, the filtering should be in a where clause -- so it filters the data before aggregation.



            Change the where to:



            where year(dte) >= ? and year(dte) <= ?


            or:



            where dte >= ? and dte <= ?


            to pass in the dates.



            The ? are for parameter placeholders. Learn how to use parameters rather than munging query strings.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 22 at 10:59









            Gordon LinoffGordon Linoff

            799k37320426




            799k37320426












            • passing date parameters will only concern task_assigned field?

              – Azima
              Mar 22 at 12:14











            • @Azima . . . I don't understand your comment. There is no such reference in the question.

              – Gordon Linoff
              Mar 22 at 12:32











            • dte field is task_assigned right?

              – Azima
              Mar 22 at 13:23











            • @Azima . . . You accepted an answer; why do you continue to ask questions? If you have another question, then ask as a question. dte is all three columns combined into a single column, so it is sometimes task_assigned.

              – Gordon Linoff
              Mar 22 at 14:03

















            • passing date parameters will only concern task_assigned field?

              – Azima
              Mar 22 at 12:14











            • @Azima . . . I don't understand your comment. There is no such reference in the question.

              – Gordon Linoff
              Mar 22 at 12:32











            • dte field is task_assigned right?

              – Azima
              Mar 22 at 13:23











            • @Azima . . . You accepted an answer; why do you continue to ask questions? If you have another question, then ask as a question. dte is all three columns combined into a single column, so it is sometimes task_assigned.

              – Gordon Linoff
              Mar 22 at 14:03
















            passing date parameters will only concern task_assigned field?

            – Azima
            Mar 22 at 12:14





            passing date parameters will only concern task_assigned field?

            – Azima
            Mar 22 at 12:14













            @Azima . . . I don't understand your comment. There is no such reference in the question.

            – Gordon Linoff
            Mar 22 at 12:32





            @Azima . . . I don't understand your comment. There is no such reference in the question.

            – Gordon Linoff
            Mar 22 at 12:32













            dte field is task_assigned right?

            – Azima
            Mar 22 at 13:23





            dte field is task_assigned right?

            – Azima
            Mar 22 at 13:23













            @Azima . . . You accepted an answer; why do you continue to ask questions? If you have another question, then ask as a question. dte is all three columns combined into a single column, so it is sometimes task_assigned.

            – Gordon Linoff
            Mar 22 at 14:03





            @Azima . . . You accepted an answer; why do you continue to ask questions? If you have another question, then ask as a question. dte is all three columns combined into a single column, so it is sometimes task_assigned.

            – Gordon Linoff
            Mar 22 at 14:03











            0














            This answer is no langer valid with the updated request.




            If I understand correctly, you want to use task_assigned if the task_completion is still null. Use COALEASCE for this.



            SELECT
            YEAR(COALESCE(task_completion, task_assigned)) as year,
            ROUND(SUM(total_hours), 2) as hours
            FROM task
            GROUP BY year
            HAVING year BETWEEN $year_from AND $year_to
            ORDER BY year;


            (I don't think you actually want to use task_deadline, too, for how could a task get completed before getting assigned first? If such can occur, then include it in the COALESCE expression. Probably: COALESCE(task_completion, task_assigned, task_deadline)` then.)






            share|improve this answer

























            • I've updated the post.. hope it will clear the confusion.

              – Azima
              Mar 22 at 9:53











            • but say, a row has task_completion value as 2019-09-09 and task_assigned as 2018-09-21. And I'm looking SUM(hours) for 2018 and hence this row should also be considered. But with COALESCE(), since task_completion is not null, it will take 2019 as year and hence ignore the row.

              – Azima
              Mar 22 at 10:02











            • Sorry, I was wrong. I'll write another answer.

              – Thorsten Kettner
              Mar 22 at 10:03















            0














            This answer is no langer valid with the updated request.




            If I understand correctly, you want to use task_assigned if the task_completion is still null. Use COALEASCE for this.



            SELECT
            YEAR(COALESCE(task_completion, task_assigned)) as year,
            ROUND(SUM(total_hours), 2) as hours
            FROM task
            GROUP BY year
            HAVING year BETWEEN $year_from AND $year_to
            ORDER BY year;


            (I don't think you actually want to use task_deadline, too, for how could a task get completed before getting assigned first? If such can occur, then include it in the COALESCE expression. Probably: COALESCE(task_completion, task_assigned, task_deadline)` then.)






            share|improve this answer

























            • I've updated the post.. hope it will clear the confusion.

              – Azima
              Mar 22 at 9:53











            • but say, a row has task_completion value as 2019-09-09 and task_assigned as 2018-09-21. And I'm looking SUM(hours) for 2018 and hence this row should also be considered. But with COALESCE(), since task_completion is not null, it will take 2019 as year and hence ignore the row.

              – Azima
              Mar 22 at 10:02











            • Sorry, I was wrong. I'll write another answer.

              – Thorsten Kettner
              Mar 22 at 10:03













            0












            0








            0







            This answer is no langer valid with the updated request.




            If I understand correctly, you want to use task_assigned if the task_completion is still null. Use COALEASCE for this.



            SELECT
            YEAR(COALESCE(task_completion, task_assigned)) as year,
            ROUND(SUM(total_hours), 2) as hours
            FROM task
            GROUP BY year
            HAVING year BETWEEN $year_from AND $year_to
            ORDER BY year;


            (I don't think you actually want to use task_deadline, too, for how could a task get completed before getting assigned first? If such can occur, then include it in the COALESCE expression. Probably: COALESCE(task_completion, task_assigned, task_deadline)` then.)






            share|improve this answer















            This answer is no langer valid with the updated request.




            If I understand correctly, you want to use task_assigned if the task_completion is still null. Use COALEASCE for this.



            SELECT
            YEAR(COALESCE(task_completion, task_assigned)) as year,
            ROUND(SUM(total_hours), 2) as hours
            FROM task
            GROUP BY year
            HAVING year BETWEEN $year_from AND $year_to
            ORDER BY year;


            (I don't think you actually want to use task_deadline, too, for how could a task get completed before getting assigned first? If such can occur, then include it in the COALESCE expression. Probably: COALESCE(task_completion, task_assigned, task_deadline)` then.)







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 22 at 10:09

























            answered Mar 22 at 9:50









            Thorsten KettnerThorsten Kettner

            53.1k32744




            53.1k32744












            • I've updated the post.. hope it will clear the confusion.

              – Azima
              Mar 22 at 9:53











            • but say, a row has task_completion value as 2019-09-09 and task_assigned as 2018-09-21. And I'm looking SUM(hours) for 2018 and hence this row should also be considered. But with COALESCE(), since task_completion is not null, it will take 2019 as year and hence ignore the row.

              – Azima
              Mar 22 at 10:02











            • Sorry, I was wrong. I'll write another answer.

              – Thorsten Kettner
              Mar 22 at 10:03

















            • I've updated the post.. hope it will clear the confusion.

              – Azima
              Mar 22 at 9:53











            • but say, a row has task_completion value as 2019-09-09 and task_assigned as 2018-09-21. And I'm looking SUM(hours) for 2018 and hence this row should also be considered. But with COALESCE(), since task_completion is not null, it will take 2019 as year and hence ignore the row.

              – Azima
              Mar 22 at 10:02











            • Sorry, I was wrong. I'll write another answer.

              – Thorsten Kettner
              Mar 22 at 10:03
















            I've updated the post.. hope it will clear the confusion.

            – Azima
            Mar 22 at 9:53





            I've updated the post.. hope it will clear the confusion.

            – Azima
            Mar 22 at 9:53













            but say, a row has task_completion value as 2019-09-09 and task_assigned as 2018-09-21. And I'm looking SUM(hours) for 2018 and hence this row should also be considered. But with COALESCE(), since task_completion is not null, it will take 2019 as year and hence ignore the row.

            – Azima
            Mar 22 at 10:02





            but say, a row has task_completion value as 2019-09-09 and task_assigned as 2018-09-21. And I'm looking SUM(hours) for 2018 and hence this row should also be considered. But with COALESCE(), since task_completion is not null, it will take 2019 as year and hence ignore the row.

            – Azima
            Mar 22 at 10:02













            Sorry, I was wrong. I'll write another answer.

            – Thorsten Kettner
            Mar 22 at 10:03





            Sorry, I was wrong. I'll write another answer.

            – Thorsten Kettner
            Mar 22 at 10:03

















            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%2f55296674%2fgroup-by-year-on-multiple-date-columns-mysql%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

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript