Display next event date

前端 未结 5 644
离开以前
离开以前 2020-12-02 18:13

The UI design for storing event and event meta data is

\"enter

<
5条回答
  •  再見小時候
    2020-12-02 18:22

    This will do it!

    WITH mycte AS
    (
        SELECT A.ID, A.Name, A.StartDate, A.StartDate AS [IntervalDate], A.Interval, A.[repeat_startID], A.[repeat_intervalID]
        FROM (
                -- this is your provided query
                -- can you run this derived table only and make sure it return what you expect?
            SELECT
                 EV.*
                ,EM1.id AS [repeat_startID]
                ,EM2.id AS [repeat_intervalID]
                -- I convert to datetime becuase Im more familiar with datatype=time manipulations
                ,DATEADD(SECOND,EM1.meta_value,'1970-01-01') AS [StartDate]
                ,EM2.meta_value AS [Interval]
            FROM [EVENTS] AS EV
                -- I used inner joins, please modify that as needed.
                INNER JOIN [EVENTS_META] AS EM1 ON EM1.meta_key = 'repeat_start' AND EM1.event_id = EV.id
                INNER JOIN [EVENTS_META] AS EM2 ON EM2.meta_key = 'repeat_interval_'+ CAST(EM1.id as Varchar(100))
        ) AS A
        UNION ALL
        SELECT  ID, Name, StartDate, DATEADD(SECOND,Interval,[IntervalDate]), Interval, [repeat_startID], [repeat_intervalID]
        FROM    mycte   
        WHERE   DATEADD(SECOND,1,[IntervalDate]) < '2014-01-30 00:00:00.000' -- this is your epoch timestamp
    )
    SELECT * FROM mycte 
    -- it is unclear if the "cutoff" date is for the Last Interval's Start Date or the next one
    -- examining the results shows there are 2 records after your"cutoff" date
    -- add a WHERE statement to fix this if needed?
    -- WHERE [IntervalDate] < '2014-01-30 00:00:00.000' -- this is your epoch timestamp
    ORDER BY [repeat_startID], StartDate;
    
    -- produces: (Column #4 is what you are interested in)
    1   Billy Visit 2014-01-03 10:00:00.000 2014-01-03 10:00:00.000 604800  1   2
    1   Billy Visit 2014-01-03 10:00:00.000 2014-01-10 10:00:00.000 604800  1   2
    1   Billy Visit 2014-01-03 10:00:00.000 2014-01-17 10:00:00.000 604800  1   2
    1   Billy Visit 2014-01-03 10:00:00.000 2014-01-24 10:00:00.000 604800  1   2
    1   Billy Visit 2014-01-03 10:00:00.000 2014-01-31 10:00:00.000 604800  1   2 -- occurs after '2014-01-30 00:00:00.000'
    1   Billy Visit 2014-01-04 18:00:00.000 2014-01-04 18:00:00.000 604800  3   4
    1   Billy Visit 2014-01-04 18:00:00.000 2014-01-11 18:00:00.000 604800  3   4
    1   Billy Visit 2014-01-04 18:00:00.000 2014-01-18 18:00:00.000 604800  3   4
    1   Billy Visit 2014-01-04 18:00:00.000 2014-01-25 18:00:00.000 604800  3   4
    1   Billy Visit 2014-01-04 18:00:00.000 2014-02-01 18:00:00.000 604800  3   4 -- occurs after '2014-01-30 00:00:00.000'
    

提交回复
热议问题