continues missing 2 months in sql server

balakrishna k 21 Reputation points
2021-07-14T18:27:53.18+00:00

how to get which empid not get salary continus 2 months(subsequence) .

create table tempc( eid int, paiddate varchar(25),salry money)
insert into tempc values(1,'2021-JAN-12',10),
(1,'2020-FEB-12',20),
(1,'2020-MAR-12',20),
(1,'2020-MAY-12',20),
(1,'2020-JUN-12',20),
(1,'2020-JUL-12',20),
(1,'2020-OCT-12',20),
(2,'2021-JAN-12',20),
(2,'2021-FEB-12',20),
(2,'2021-MAR-12',20),
(3,'2021-JAN-12',20),
(3,'2021-APR-12',20),
(3,'2021-MAY-12',20)
bassed on above data expected outputlike below

eid | paiddate | salary
1 |2020-Aug-12 |0
1 |2020-Sep-12 |0
3 |2021-Feb-12 |0
3 |2021-Mar-12 |0

I tried like below
declare @mindate date = (select min(convert (varchar(10), cast(paiddate as date),121 ))mindate from tempc);
declare @maxdate date = (select max(convert (varchar(10), cast(paiddate as date),121 ))mindate from tempc);

with dt as
(select @mindate as dte
union all
select dateadd(mm,1,dte) from dt where dte < @maxdate)
select eid,convert (varchar(10), cast(paiddate as date),121 )paiddate ,
dte from dt
outer apply (select * from tempc t where month(dt.dte)=month(convert (varchar(10), cast(paiddate as date),121 ))
and year(dt.dte)=year(convert (varchar(10), cast(paiddate as date),121 ))
)t
order by eid

above query not give expected reulst .could you please tell me how to write a query to achive this task in sql server .

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,812 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,638 questions
{count} votes

Accepted answer
  1. EchoLiu-MSFT 14,581 Reputation points
    2021-07-15T02:46:48.167+00:00

    Hi @balakrishna k ,

    Please also check:

    ;with cte  
    as(select eid,paiddate,  
      row_number() over(partition by eid order by eid) rr,  
      substring(cast(cast(paiddate as date) as varchar),6,2) mm  
      from tempc)  
    ,cte2 as  
      (select c.*  
       from cte c  
       left join cte c2 on c.rr=c2.rr-1 and c.eid=c2.eid  
       where cast(c2.mm as int)-cast(c.mm as int)=3)  
      
    select eid,upper(format(dateadd(MM,1,paiddate),'yyyy"-"MMM"-"dd')) paiddate,0 salry   
    from cte2  
    union   
    select eid,upper(format(dateadd(MM,2,paiddate),'yyyy"-"MMM"-"dd')),0   
    from cte2  
    

    Output:
    114776-image.png

    If you have any question, please feel free to let me know.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Viorel 117.3K Reputation points
    2021-07-14T19:26:24.737+00:00

    Check a query:

    ; with Q1 as
    (
        select *, 
            datediff(month, d, lead(d) over(partition by eid order by d)) df
        from tempc
        cross apply (values (cast(paiddate as date))) D(d)
    ),
    Q2 as
    (
        select eid, dateadd(month, 1, paiddate) as paiddate, df
        from Q1
        where df > 2
        union all
        select eid, dateadd(month, 1, paiddate), df - 1
        from Q2
        where df > 2
    )
    select eid, upper(format(paiddate, 'yyyy"-"MMM"-"dd')) as paiddate, 0 as salary
    from Q2
    order by eid, paiddate
    

    Note that the first year in your sample data probably should be 2020 instead of 2021.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.