MYSQL select query return list of months as string from between start/end date

Divyesh K

There are two main fields Startdate and Enddate in Contract table I want to give result as extra column called description to represent month come between Contract start and end. see bellow table and result which I require

Table : Contracts
------------------------------
ID | START      | END        |
------------------------------
1  | 2016-05-01 | 2016-07-31 |
2  | 2016-04-01 | 2016-08-31 |
3  | 2016-01-22 | 2016-02-25 |
------------------------------

Here I need result as per bellow formate, one extra field which represent range/list of months between startdate and enddate of contract using SELECT query.

Result (as per give format)
----------------------------------------------------------------------------------------
ID | START      | END        | Description      
----------------------------------------------------------------------------------------
1  | 2016-05-01 | 2016-07-31 | May-2016, Jun-2016, July-2016
2  | 2016-04-01 | 2016-07-31 | April-2016, May-2016, Jun-2016, July-2016
3  | 2016-01-22 | 2016-02-25 | January-2016, February-2016
----------------------------------------------------------------------------------------

Example first row of above table startdate is 2016-05-01 (2016-May-01) and end date is 2016-07-31 (2016-July-31) so here it gives list of months and year between May-01 to July-31 so description will be May-2016, Jun-2016, July-2016.

I tried many queries still I fail to get the exact SQL query.

Don't know how to do exactly and get same result,

Any suggestions please

Thanks in Advance

KP.

The below query should do the trick.

Your data in input and output are different for 2nd row, I have run it for the output data

select id, DATE_FORMAT(start_Date, '%Y-%c-%d') as Start_Date,
       DATE_FORMAT(end_date,'%Y-%c-%d') as END_Date,
       group_concat( distinct(DATE_FORMAT(aDate, '%Y %M '))) as Descp
from (
     select ss.end_date - interval (a.a ) month as aDate from
     (select 0 as a union all select 1 union all select 2 union all select 3
     union all select 4 union all select 5 union all select 6 union all
     select 7 union all select 8 union all select 9) a, Contracts ss 
     ) mon, Contracts sa
where aDate between sa.start_date and sa.end_date
group by id;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MYSQL select query return list of months as string from between start/end date

From Dev

MySQL query to return a list of months

From Dev

PHP MYSQL Return last 3 months from a date from the DB - Mysql Query

From Dev

How to return part of a string from a mysql table filed by select query?

From Dev

linq query to return a string from a list

From Dev

linq query to return a string from a list

From Dev

Mysql query fetching record between two months

From Dev

MySQL: Select and return articles where date between two weeks

From Dev

MySQL query to return rows between two date ranges

From Dev

Select count distinct months between two dates from many date registries?

From Dev

Trying to select distinct list of months from date where year = xxxx - linq to entities

From Dev

Calculate previous and next months from date string

From Dev

mysql select distinct months (abbreviation) from table

From Dev

Select data from each previous months - MySQL

From Dev

Selecting between the MIN date and MIN date + 11 months - MySQL

From Dev

Return All Months & Years Between Date Range - SQL

From Dev

Slow Select Query with DATE Between

From Dev

Slow Select Query with DATE Between

From Dev

Return Result from Select Query in stored procedure to a List

From Dev

If select query returns null then return lastest entry from db in mysql

From Dev

MySQL return no results from select * where varchar="" query

From Dev

Linq query to select single string from multiple List<string>

From Dev

Resolve Mysql query to Select unique from id and order by date acceding

From Dev

php - select from mysql table column between date range

From Dev

php - select from mysql table column between date range

From Dev

Return records for last 6 months if no date supplied or last 6 months from date supplied

From Dev

MYSQL query select column string date compared to date formatted current date

From Dev

MySQL Select distinct date query

From Dev

Mysql Date between query not working

Related Related

  1. 1

    MYSQL select query return list of months as string from between start/end date

  2. 2

    MySQL query to return a list of months

  3. 3

    PHP MYSQL Return last 3 months from a date from the DB - Mysql Query

  4. 4

    How to return part of a string from a mysql table filed by select query?

  5. 5

    linq query to return a string from a list

  6. 6

    linq query to return a string from a list

  7. 7

    Mysql query fetching record between two months

  8. 8

    MySQL: Select and return articles where date between two weeks

  9. 9

    MySQL query to return rows between two date ranges

  10. 10

    Select count distinct months between two dates from many date registries?

  11. 11

    Trying to select distinct list of months from date where year = xxxx - linq to entities

  12. 12

    Calculate previous and next months from date string

  13. 13

    mysql select distinct months (abbreviation) from table

  14. 14

    Select data from each previous months - MySQL

  15. 15

    Selecting between the MIN date and MIN date + 11 months - MySQL

  16. 16

    Return All Months & Years Between Date Range - SQL

  17. 17

    Slow Select Query with DATE Between

  18. 18

    Slow Select Query with DATE Between

  19. 19

    Return Result from Select Query in stored procedure to a List

  20. 20

    If select query returns null then return lastest entry from db in mysql

  21. 21

    MySQL return no results from select * where varchar="" query

  22. 22

    Linq query to select single string from multiple List<string>

  23. 23

    Resolve Mysql query to Select unique from id and order by date acceding

  24. 24

    php - select from mysql table column between date range

  25. 25

    php - select from mysql table column between date range

  26. 26

    Return records for last 6 months if no date supplied or last 6 months from date supplied

  27. 27

    MYSQL query select column string date compared to date formatted current date

  28. 28

    MySQL Select distinct date query

  29. 29

    Mysql Date between query not working

HotTag

Archive