简体   繁体   中英

How to calculate days between two dates,

Is it possible in MySQL to easily to get the number of days between two dates, group by months and then disperse those days between the months, so that when I group by month, I get to see how many days inbetween two those two dates fall into each month.

Example: creation 05.05.2014 to expiration 06.06.2014 returns 32 days. I want to display this as:

May: 26 days June: 5 days

So far I've got:

SELECT MONTHNAME(date) as month_name,
SUM(DATEDIFF(expiration, date)) AS num_days
FROM reservations
GROUP BY month_name

But it doesn't disperse the days corretly by month, just drop them all into the group-by -start-date-month, obviously. Any ideas would be highly appriciated.

I would build a comprehensive date temp table (or CTE) first.

From there you can select what you'd like, and group it however suits your needs.

    IF OBJECT_ID('TEMPDB..#Dates') IS NOT NULL DROP TABLE #Dates

    DECLARE @StartDate DATETIME, @EndDate DATETIME

        SET @StartDate = '05/05/2014'
        SET @EndDate = '06/06/2014'

    ;WITH Dates AS
        (
         SELECT CAST(CONVERT(VARCHAR(8), @StartDate,112) AS INT) AS DateId
              , CAST(@StartDate AS DATE) AS Date
              , CAST(@StartDate AS DATETIME) AS DateTime
              , DATEPART(QUARTER, @StartDate) AS QuarterId
              , 'Q' + CAST(DATEPART(QUARTER,@StartDate) AS CHAR(1)) AS Quarter
              , DATEPART(MONTH, @StartDate) AS MonthId
              , DATENAME(MONTH, @StartDate) AS Month
              , DATEPART(DAY, @StartDate) AS Day
              , DATEPART(YEAR, @StartDate) AS Year
              , DATENAME(DW,@StartDate) AS DayOfWeek
              , DATEPART(DAYOFYEAR, @StartDate) AS DayOfYear

          UNION ALL

         SELECT CAST(CONVERT(VARCHAR(8),DATEADD(DAY, 1, D.DateTime), 112) AS INT) ASDateId
              , CAST(DATEADD(DAY, 1, D.DateTime) AS DATE) AS Date
              , CAST(DATEADD(DAY, 1, D.DateTime) AS DATETIME) AS DateTime
              , DATEPART(QUARTER,DATEADD(DAY,1,D.DateTime)) AS QuarterId
              ,'Q'+CAST(DATEPART(QUARTER,DATEADD(DAY, 1, D.DateTime)) AS CHAR(1)) AS Quarter
              , DATEPART(MONTH,DATEADD(DAY, 1, D.DateTime)) AS MonthId
              , DATENAME(MONTH,DATEADD(DAY, 1,D.DateTime)) AS Month
              , DATEPART(DAY,DATEADD(DAY, 1, D.DateTime)) AS Day
              , DATEPART(YEAR,DATEADD(DAY, 1, D.DateTime)) AS Year
              , DATENAME(DW,DATEADD(DAY,1, D.DateTime)) AS DayOfWeek
              , DATEPART(DAYOFYEAR,DATEADD(DAY,1, D.DateTime)) AS DayOfYear
           FROM Dates AS D
          WHERE D.DateTime < @EndDate
        )

    SELECT DateId AS DateId
         , Date AS Date
         , DateTime AS DateTime
         , QuarterId AS QuarterId
         , Quarter AS Quarter
         , MonthId AS MonthId
         , Month AS Month
         , Day AS Day
         , Year AS Year
         , DayOfWeek AS DayOfWeek
         , DayOfYear AS DayOfYear
         , ROW_NUMBER() OVER(ORDER BY DateId)-1 AS DayCount
      INTO #Dates
      FROM Dates AS D
     ORDER BY D.DateId
    OPTION (MAXRECURSION 32767)

    -- SELECT * FROM #Dates

SELECT COUNT(Day) AS CountOfDays, Month
  FROM #Dates
  GROUP BY Month

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM