Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I work on sql server 2012 i face issue i need to make select statment get Partid from last month until current month

based on last date exist per partid

and on same time if there are any gaps between dates then file it based on last date

so

first case if i found partid with last date 2022-01-08 then i will add 08-02-2022 and 08-03-2022 as partid 6070

second case if partid with date on month 7 and month 10 and no date per part id on month 8 and 9 then it must display this gap

according to last month as partid 1234 have gap

both cases must applied for all data based on partid

date used below on formate yyyy-mm-dd

create table Parts
 (
    
 PartId int,
 CreatedDate date
 )
 insert into Parts(PartId,CreatedDate)
 values
 (1234,'2021-07-03'),
 (1234,'2021-10-05'),
 (1234,'2021-11-05'),
 (5981,'2021-11-15'),
 (5981,'2021-12-03'),
 (6070,'2021-12-12'),
 (6070,'2022-01-08')


i need to make select statment display parts as expected result

green rows only for more clear that these parts must added

Expected result

PartId	CreatedDate
1234	2021-07-03
1234	2021-08-03
1234	2021-09-03
1234	2021-10-05
1234	2021-11-05
1234	2021-12-05
1234	2022-01-05
1234	2022-02-05
1234	2022-03-05
5981	2021-11-15
5981	2021-12-03
5981	2022-01-03
5981	2022-02-03
5981	2022-03-03
6070	2021-12-12
6070	2022-01-08
6070	2022-02-08
6070	2022-03-08


Notes
update post i get part from task above
1-get gapes between dates as code below
;with cte as (
      select partid, createddate,
             dateadd(month, -1,
                     coalesce(lead(createddate) over (partition by partid order by createddate),
                              max(createddate) over ()
                             )
                    ) as end_month
      from Parts
      union all
      select partid, dateadd(month, 1, createddate), end_month
      from cte
      where createddate <end_month
     )
select *
from cte
order by partid, createddate

remaining one thing
how to get dates until current date please

What I have tried:

what i try

with cte as (
      select partid, month(CreatedDate),
             dateadd(month, -1,
                     coalesce(lead(month(CreatedDate)) over (partition by partid order by month(CreatedDate)),
                              max(month(CreatedDate)) over ()
                             )
                    ) as end_month
      from Parts
      union all
      select partid, dateadd(month, 1, month(CreatedDate)) as monthes, end_month
      from cte
      where monthes < end_month
     )
select *
from cte
order by partid, month;
Posted
Updated 15-Mar-22 22:49pm
v2

1 solution

I would create a a Calendar table with a single Primary key column, containing all the date between the earliest date you expect (2000-01-01 perhaps) and some point in the future (2099-12-31 maybe) - that's less than 40,000 rows, and simple to construct and both small and performant:
SQL
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate = GETDATE()
SET @EndDate = DATEADD(d, 365, @StartDate)

WHILE @StartDate <= @EndDate
      BEGIN
             INSERT INTO [Calendar]
             (
                   CalendarDate
             )
             SELECT
                   @StartDate

             SET @StartDate = DATEADD(dd, 1, @StartDate)
      END


Then it's a simple matter of a JOIN to generate the dates you need:
SQL
SELECT c.CalendarDate, bd.ID FROM Calendar c
JOIN BetweenDates bd
  ON c.CalendarDate BETWEEN bd.StartDate AND bd.EndDate
For this data:
BetweenDates
ID  StartDate               EndDate
1	2022-02-20 00:00:00.000	2022-02-24 00:00:00.000
2	2022-03-01 00:00:00.000	2022-03-03 00:00:00.000
You get these results:
Results
CalendarDate            ID
2022-02-20 00:00:00.000	1
2022-02-21 00:00:00.000	1
2022-02-22 00:00:00.000	1
2022-02-23 00:00:00.000	1
2022-02-24 00:00:00.000	1
2022-03-01 00:00:00.000	2
2022-03-02 00:00:00.000	2
2022-03-03 00:00:00.000	2
 
Share this answer
 
Comments
ahmed_sa 16-Mar-22 4:45am    
can any one help me
i do part from what i need as below
1-i get gap between dates

;with cte as (
select partid, createddate,
dateadd(month, -1,
coalesce(lead(createddate) over (partition by partid order by createddate),
max(createddate) over ()
)
) as end_month
from Parts
union all
select partid, dateadd(month, 1, createddate), end_month
from cte
where createddate
ahmed_sa 16-Mar-22 4:47am    
but remain to get dates until current date so how to do it please
ahmed_sa 16-Mar-22 4:50am    
can you check update original post

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900