check date range for previous year within same table

  • hi,

    I have following sample data set:

    SaleID    SaleMarkdown    StartDate(yyyymmdd)    EndDate(yyyymmdd)  SaleInPreviousYear    previousyearsaleID
    1            20%                               20160101        20160301
    2            30%                               20170501        20170630
    3            40%                               20170101        20170201
    4            10%                               20180101        20180301                                            
    5            15%                               20180401        20180430

    Last two columns are empty at the moment.

    I would like to find out if there was a sale +-30 days in the previous year for a given sale.

    For example for saleid 4 the sale date range is between 20180101 and 20180301. Was there a sale in the previous year during this time period? if so update SaleInPreviousYear column to yes and update previousyearsaleID column with the saleid of the previous year.  In this case saleid 3 falls within +-30 days of the sale start or sale end date.

    SaleID    SaleMarkdown    StartDate(yyyymmdd)    EndDate(yyyymmdd)  SaleInPreviousYear    previousyearsaleID
    1            20%                                  20160101        20160301
    2            30%                                  20170501        20170630
    3            40%                                  20170101        20170201
    4            10%                                  20180101        20180301                        yes                                      3
    5            15%                                  20180401        20180430

    Sale 5 is another example where sale 2 occured in the +-30 range of start and end date in the previous year.

    How can I accomplish this in TSQL?

    Thanks

  • One way is to create a calendar table (either permanent or on-the-fly) and join to that.  You'll get much better and answers much quicker if you post table DDL and sample data in consumable format (ie CREATE TABLE and INSERT statements).

    John

  • sqlstar2011 - Monday, October 22, 2018 9:03 AM

    hi,

    I have following sample data set:

    SaleID    SaleMarkdown    StartDate(yyyymmdd)    EndDate(yyyymmdd)  SaleInPreviousYear    previousyearsaleID
    1            20%                               20160101        20160301
    2            30%                               20170501        20170630
    3            40%                               20170101        20170201
    4            10%                               20180101        20180301                                            
    5            15%                               20180401        20180430

    Last two columns are empty at the moment.

    I would like to find out if there was a sale +-30 days in the previous year for a given sale.

    For example for saleid 4 the sale date range is between 20180101 and 20180301. Was there a sale in the previous year during this time period? if so update SaleInPreviousYear column to yes and update previousyearsaleID column with the saleid of the previous year.  In this case saleid 3 falls within +-30 days of the sale start or sale end date.

    SaleID    SaleMarkdown    StartDate(yyyymmdd)    EndDate(yyyymmdd)  SaleInPreviousYear    previousyearsaleID
    1            20%                                  20160101        20160301
    2            30%                                  20170501        20170630
    3            40%                                  20170101        20170201
    4            10%                                  20180101        20180301                        yes                                      3
    5            15%                                  20180401        20180430

    Sale 5 is another example where sale 2 occured in the +-30 range of start and end date in the previous year.

    How can I accomplish this in TSQL?

    Thanks

    Could you post the code you currently have written?

  • Here is the DDL and insert statements for the sample data:

    create table dateRangeTest(
        saleid int,
        salemarkdown decimal(3,2),
        startdate date,
        enddate date,
        saleinpreviousyear varchar(5),
        previousyearsaleID int
    )

    insert into dateRangeTest values(1, .20, '01/01/2016', '03/01/2016',NULL, NULL)
    insert into dateRangeTest values(2, .30, '05/01/2017', '06/30/2017',NULL, NULL)
    insert into dateRangeTest values(3, .40, '01/01/2017', '02/01/2017',NULL, NULL)
    insert into dateRangeTest values(4, .10, '01/01/2018', '03/01/2018',NULL, NULL)
    insert into dateRangeTest values(5, .15, '04/01/2018', '04/30/2018',NULL, NULL)

    thanks

  • sqlstar2011 - Monday, October 22, 2018 9:39 AM

    Here is the DDL and insert statements for the sample data:

    create table dateRangeTest(
        saleid int,
        salemarkdown decimal(3,2),
        startdate date,
        enddate date,
        saleinpreviousyear varchar(5),
        previousyearsaleID int
    )

    insert into dateRangeTest values(1, .20, '01/01/2016', '03/01/2016',NULL, NULL)
    insert into dateRangeTest values(2, .30, '05/01/2017', '06/30/2017',NULL, NULL)
    insert into dateRangeTest values(3, .40, '01/01/2017', '02/01/2017',NULL, NULL)
    insert into dateRangeTest values(4, .10, '01/01/2018', '03/01/2018',NULL, NULL)
    insert into dateRangeTest values(5, .15, '04/01/2018', '04/30/2018',NULL, NULL)

    thanks

    Well, that looks like is shows the results you want.  What I asked was for the code you are running that actually generates the results.

  • hi Lynn,

    I don't have anything at this point. As I am struggling with how to actually write SQL for this.

    I was looking for some example that I can work with.

    Thanks

  • I dont think creating a calendar table from the base table will work.

    I created a temp table from the base table:
    select distinct startdate, enddate
    into #calendar
    from dateRangeTest

    but something like this:
    SELECT a.saleid, a.salemarkdown, a.startdate, a.enddate, c.startdate,c.enddate
    from dateRangeTest a
    INNER JOIN #calendar c
     ON a.startDate BETWEEN c.startdate AND c.enddate

    is going to return everything.

    How can I filter out records.
    For example the record with saleid 1 - shoud not show up since there is no record for it in the previous dates.

  • sqlstar2011 - Monday, October 22, 2018 9:03 AM

    hi,

    I have following sample data set:

    SaleID    SaleMarkdown    StartDate(yyyymmdd)    EndDate(yyyymmdd)  SaleInPreviousYear    previousyearsaleID
    1            20%                               20160101        20160301
    2            30%                               20170501        20170630
    3            40%                               20170101        20170201
    4            10%                               20180101        20180301                                            
    5            15%                               20180401        20180430

    Last two columns are empty at the moment.

    I would like to find out if there was a sale +-30 days in the previous year for a given sale.

    For example for saleid 4 the sale date range is between 20180101 and 20180301. Was there a sale in the previous year during this time period? if so update SaleInPreviousYear column to yes and update previousyearsaleID column with the saleid of the previous year.  In this case saleid 3 falls within +-30 days of the sale start or sale end date.

    SaleID    SaleMarkdown    StartDate(yyyymmdd)    EndDate(yyyymmdd)  SaleInPreviousYear    previousyearsaleID
    1            20%                                  20160101        20160301
    2            30%                                  20170501        20170630
    3            40%                                  20170101        20170201
    4            10%                                  20180101        20180301                        yes                                      3
    5            15%                                  20180401        20180430

    Sale 5 is another example where sale 2 occured in the +-30 range of start and end date in the previous year.

    How can I accomplish this in TSQL?

    Thanks

    SELECT d1.saleid,
           d1.salemarkdown,
           d1.startdate,
           d1.enddate,
           IIF(SalesMadeLastYear.SalesIds IS NULL,'No','Yes') saleinpreviousyear,
           ISNULL(SalesMadeLastYear.SalesIds,'') previousyearsaleIDs
    FROM dateRangeTest d1
    OUTER APPLY(SELECT STUFF((SELECT ',' + CONVERT(varchar,d2.saleid) AS [text()]
                   FROM dateRangeTest d2
                  WHERE d2.startdate >= dateadd(yy,-1,d1.startdate)
                    AND d2.enddate <= dateadd(yy,-1,d1.enddate)
                    FOR XML PATH('')), 1, 1, '' )) SalesMadeLastYear(SalesIds)

  • Jonathan AC Roberts - Monday, October 22, 2018 10:57 AM

    sqlstar2011 - Monday, October 22, 2018 9:03 AM

    hi,

    I have following sample data set:

    SaleID    SaleMarkdown    StartDate(yyyymmdd)    EndDate(yyyymmdd)  SaleInPreviousYear    previousyearsaleID
    1            20%                               20160101        20160301
    2            30%                               20170501        20170630
    3            40%                               20170101        20170201
    4            10%                               20180101        20180301                                            
    5            15%                               20180401        20180430

    Last two columns are empty at the moment.

    I would like to find out if there was a sale +-30 days in the previous year for a given sale.

    For example for saleid 4 the sale date range is between 20180101 and 20180301. Was there a sale in the previous year during this time period? if so update SaleInPreviousYear column to yes and update previousyearsaleID column with the saleid of the previous year.  In this case saleid 3 falls within +-30 days of the sale start or sale end date.

    SaleID    SaleMarkdown    StartDate(yyyymmdd)    EndDate(yyyymmdd)  SaleInPreviousYear    previousyearsaleID
    1            20%                                  20160101        20160301
    2            30%                                  20170501        20170630
    3            40%                                  20170101        20170201
    4            10%                                  20180101        20180301                        yes                                      3
    5            15%                                  20180401        20180430

    Sale 5 is another example where sale 2 occured in the +-30 range of start and end date in the previous year.

    How can I accomplish this in TSQL?

    Thanks

    SELECT d1.saleid,
           d1.salemarkdown,
           d1.startdate,
           d1.enddate,
           IIF(SalesMadeLastYear.SalesIds IS NULL,'No','Yes') saleinpreviousyear,
           ISNULL(SalesMadeLastYear.SalesIds,'') previousyearsaleIDs
    FROM dateRangeTest d1
    OUTER APPLY(SELECT STUFF((SELECT ',' + CONVERT(varchar,d2.saleid) AS [text()]
                   FROM dateRangeTest d2
                  WHERE d2.startdate >= dateadd(yy,-1,d1.startdate)
                    AND d2.enddate <= dateadd(yy,-1,d1.enddate)
                    FOR XML PATH('')), 1, 1, '' )) SalesMadeLastYear(SalesIds)

    Thanks - can you please explain your solution. What is the purpose of XML PATH?

  • sqlstar2011 - Monday, October 22, 2018 11:44 AM

    Jonathan AC Roberts - Monday, October 22, 2018 10:57 AM

    sqlstar2011 - Monday, October 22, 2018 9:03 AM

    hi,

    I have following sample data set:

    SaleID    SaleMarkdown    StartDate(yyyymmdd)    EndDate(yyyymmdd)  SaleInPreviousYear    previousyearsaleID
    1            20%                               20160101        20160301
    2            30%                               20170501        20170630
    3            40%                               20170101        20170201
    4            10%                               20180101        20180301                                            
    5            15%                               20180401        20180430

    Last two columns are empty at the moment.

    I would like to find out if there was a sale +-30 days in the previous year for a given sale.

    For example for saleid 4 the sale date range is between 20180101 and 20180301. Was there a sale in the previous year during this time period? if so update SaleInPreviousYear column to yes and update previousyearsaleID column with the saleid of the previous year.  In this case saleid 3 falls within +-30 days of the sale start or sale end date.

    SaleID    SaleMarkdown    StartDate(yyyymmdd)    EndDate(yyyymmdd)  SaleInPreviousYear    previousyearsaleID
    1            20%                                  20160101        20160301
    2            30%                                  20170501        20170630
    3            40%                                  20170101        20170201
    4            10%                                  20180101        20180301                        yes                                      3
    5            15%                                  20180401        20180430

    Sale 5 is another example where sale 2 occured in the +-30 range of start and end date in the previous year.

    How can I accomplish this in TSQL?

    Thanks

    SELECT d1.saleid,
           d1.salemarkdown,
           d1.startdate,
           d1.enddate,
           IIF(SalesMadeLastYear.SalesIds IS NULL,'No','Yes') saleinpreviousyear,
           ISNULL(SalesMadeLastYear.SalesIds,'') previousyearsaleIDs
    FROM dateRangeTest d1
    OUTER APPLY(SELECT STUFF((SELECT ',' + CONVERT(varchar,d2.saleid) AS [text()]
                   FROM dateRangeTest d2
                  WHERE d2.startdate >= dateadd(yy,-1,d1.startdate)
                    AND d2.enddate <= dateadd(yy,-1,d1.enddate)
                    FOR XML PATH('')), 1, 1, '' )) SalesMadeLastYear(SalesIds)

    Thanks - can you please explain your solution. What is the purpose of XML PATH?

    Also how does the join happen here? In other examples that I have seen for outerapply there is some sort of reference to the common column like this example:


    SELECT * FROM Department D OUTER APPLY  (  SELECT * FROM Employee E  WHERE E.DepartmentID = D.DepartmentID  ) A GO

    Sort of lost.
    Also if you can expand on the STUFF function that will be helpful

    thanks. 🙂

  • sqlstar2011 - Monday, October 22, 2018 11:54 AM

    sqlstar2011 - Monday, October 22, 2018 11:44 AM

    Jonathan AC Roberts - Monday, October 22, 2018 10:57 AM

    sqlstar2011 - Monday, October 22, 2018 9:03 AM

    hi,

    I have following sample data set:

    SaleID    SaleMarkdown    StartDate(yyyymmdd)    EndDate(yyyymmdd)  SaleInPreviousYear    previousyearsaleID
    1            20%                               20160101        20160301
    2            30%                               20170501        20170630
    3            40%                               20170101        20170201
    4            10%                               20180101        20180301                                            
    5            15%                               20180401        20180430

    Last two columns are empty at the moment.

    I would like to find out if there was a sale +-30 days in the previous year for a given sale.

    For example for saleid 4 the sale date range is between 20180101 and 20180301. Was there a sale in the previous year during this time period? if so update SaleInPreviousYear column to yes and update previousyearsaleID column with the saleid of the previous year.  In this case saleid 3 falls within +-30 days of the sale start or sale end date.

    SaleID    SaleMarkdown    StartDate(yyyymmdd)    EndDate(yyyymmdd)  SaleInPreviousYear    previousyearsaleID
    1            20%                                  20160101        20160301
    2            30%                                  20170501        20170630
    3            40%                                  20170101        20170201
    4            10%                                  20180101        20180301                        yes                                      3
    5            15%                                  20180401        20180430

    Sale 5 is another example where sale 2 occured in the +-30 range of start and end date in the previous year.

    How can I accomplish this in TSQL?

    Thanks

    SELECT d1.saleid,
           d1.salemarkdown,
           d1.startdate,
           d1.enddate,
           IIF(SalesMadeLastYear.SalesIds IS NULL,'No','Yes') saleinpreviousyear,
           ISNULL(SalesMadeLastYear.SalesIds,'') previousyearsaleIDs
    FROM dateRangeTest d1
    OUTER APPLY(SELECT STUFF((SELECT ',' + CONVERT(varchar,d2.saleid) AS [text()]
                   FROM dateRangeTest d2
                  WHERE d2.startdate >= dateadd(yy,-1,d1.startdate)
                    AND d2.enddate <= dateadd(yy,-1,d1.enddate)
                    FOR XML PATH('')), 1, 1, '' )) SalesMadeLastYear(SalesIds)

    Thanks - can you please explain your solution. What is the purpose of XML PATH?

    Also how does the join happen here? In other examples that I have seen for outerapply there is some sort of reference to the common column like this example:


    SELECT * FROM Department D OUTER APPLY  (  SELECT * FROM Employee E  WHERE E.DepartmentID = D.DepartmentID  ) A GO

    Sort of lost.
    Also if you can expand on the STUFF function that will be helpful

    thanks. 🙂

    "Also how does the join happen here?"
    WHERE d2.startdate >= dateadd(yy,-1,d1.startdate)
                    AND d2.enddate <= dateadd(yy,-1,d1.enddate)

    "Also if you can expand on the STUFF function that will be helpful"
    I put the XML in there so if you had more than one order for a for a given date range then it would list all the salesIds in a csv.
    Read this for more details: https://sqlwhisper.wordpress.com/2013/03/24/stuff-and-for-xml-path-for-string-concatenation/

  • sqlstar2011 - Monday, October 22, 2018 9:03 AM

    hi,

    I have following sample data set:

    SaleID    SaleMarkdown    StartDate(yyyymmdd)    EndDate(yyyymmdd)  SaleInPreviousYear    previousyearsaleID
    1            20%                               20160101        20160301
    2            30%                               20170501        20170630
    3            40%                               20170101        20170201
    4            10%                               20180101        20180301                                            
    5            15%                               20180401        20180430

    Last two columns are empty at the moment.

    I would like to find out if there was a sale +-30 days in the previous year for a given sale.

    For example for saleid 4 the sale date range is between 20180101 and 20180301. Was there a sale in the previous year during this time period? if so update SaleInPreviousYear column to yes and update previousyearsaleID column with the saleid of the previous year.  In this case saleid 3 falls within +-30 days of the sale start or sale end date.

    SaleID    SaleMarkdown    StartDate(yyyymmdd)    EndDate(yyyymmdd)  SaleInPreviousYear    previousyearsaleID
    1            20%                                  20160101        20160301
    2            30%                                  20170501        20170630
    3            40%                                  20170101        20170201
    4            10%                                  20180101        20180301                        yes                                      3
    5            15%                                  20180401        20180430

    Sale 5 is another example where sale 2 occured in the +-30 range of start and end date in the previous year.

    How can I accomplish this in TSQL?

    Thanks

    I bel

    Try this
    SELECT B.saleid
      , A.salemarkdown
      , A.startdate
      , A.enddate
      --, DATEADD(dd,-30,DATEADD(yy,-1, A.startdate)) [previous_year_start]
      --, DATEADD(dd,30,DATEADD(yy,-1, A.enddate)) [previous_year_end]
      , CASE WHEN DATEADD(dd,-30,DATEADD(yy,-1, A.startdate)) <= A.startdate THEN 'yes' END saleinpreviousyear
      , A.saleid [previousyearsaleID]
    FROM dateRangeTest A
    INNER JOIN dateRangeTest B ON A.startdate >= DATEADD(dd,-30,DATEADD(yy,-1, B.startdate))
     AND A.enddate <= DATEADD(dd,30,DATEADD(yy,-1, B.enddate))

    [font="Verdana"]Sal Young[/font]
    [font="Verdana"]MCITP Database Administrator[/font]

Viewing 13 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply