Hi Expert,
I am expecting enddate column from startdate -1
CREATE TABLE [dbo].[Table3](
[Testid] [nchar](10) NULL,
[Status] [nchar](10) NULL,
[Startdate] [date] NULL
) ON [PRIMARY]
GO
insert into table3
values
('1','Progress', '20210203'),
('1', 'Closed', '20220403'),
('2', 'Closed', '20220703'),
('2', 'Progress', '20220503')
I wanted to get end date column from startdate -1 from start where count(testid) >1
I tried
select Testid, Status ,count(testid) as count,
Startdate, DATEADD(day,-1,Startdate) as enddate from table3 group by Testid,Status,Startdate
expected output
Please refer above output and not from attachment as i am unable to remove attachement
March 15, 2022 at 1:36 pm
Your expected output is inconsistent.
That said, I believe that you need to look at the LAG function.
March 15, 2022 at 1:43 pm
select Testid, Status ,count(testid) as count,
Startdate, lead(DATEADD(day,-1,Startdate)) over (order by testid,Startdate) from table3 group by Testid,Status,Startdate
I tried above lead function but unable to get above output
March 15, 2022 at 2:02 pm
You are on the right track.
SELECT t.Testid
, t.Status
, t.Startdate
, EndDate = DATEADD(dd, -1, LEAD(t.Startdate) OVER (PARTITION BY t.Testid ORDER BY t.Startdate))
FROM Table3 AS t
March 15, 2022 at 2:51 pm
I have repeated and loaded the insert statement
insert into table3
values
('1','Progress', '20210203'),
('1', 'Closed', '20220403'),
('2', 'Closed', '20220703'),
('2', 'Progress', '20220503')
it is showing wrong output
if the date is repeated then it should show the same and not the before date ..now it is showing 1 day minus from start date
March 15, 2022 at 3:18 pm
i tried
SELECT TestID, Status, StartDate, case when Lead(StartDate) OVER (PARTITION BY TestID ORDER BY StartDate) =LEAD(StartDate) OVER (PARTITION BY TestID ORDER BY StartDate) then Lag(StartDate) OVER (PARTITION BY TestID ORDER BY StartDate) else DATEADD(dd, -1, LEAD(StartDate) OVER (PARTITION BY TestID ORDER BY StartDate)) end
FROM Table3
ORDER BY Testid, Startdate
but getting wrong output
March 15, 2022 at 3:24 pm
I have repeated and loaded the insert statement
insert into table3 values ('1','Progress', '20210203'), ('1', 'Closed', '20220403'), ('2', 'Closed', '20220703'), ('2', 'Progress', '20220503')
it is showing wrong output
if the date is repeated then it should show the same and not the before date ..now it is showing 1 day minus from start date
The solution that I provided was correct for what you requested. Now you have changed the requirements, but not provided the expected results
March 15, 2022 at 3:35 pm
So, taking a guess at what you are looking for ....
WITH cteData AS (
SELECT t.Testid
, t.Status
, t.Startdate
, EndDate = DATEADD(dd, -1, LEAD(t.Startdate) OVER (PARTITION BY t.Testid ORDER BY t.Startdate))
FROM Table3 AS t
GROUP BY t.Testid, t.Status, t.Startdate
)
SELECT t.Testid
, t.Status
, t.Startdate
, cte.EndDate
FROM Table3 AS t
INNER JOIN cteData AS cte
ON t.Testid = cte.Testid
AND t.Status = cte.Status
AND t.Startdate = cte.Startdate
ORDER BY cte.Testid, cte.Startdate;
March 15, 2022 at 3:35 pm
Hi Expert,
I am so sorry
here is the expected output
The following code satisfies your requirement
SELECT t.Testid
, t.Status
, t.Startdate
, EndDate = CASE WHEN t.Startdate = LEAD(t.Startdate) OVER (PARTITION BY t.Testid ORDER BY t.Startdate)
THEN t.Startdate
ELSE DATEADD(dd, -1, LEAD(t.Startdate) OVER (PARTITION BY t.Testid ORDER BY t.Startdate))
END
FROM #Table3 AS t
March 15, 2022 at 3:48 pm
Hi Expert,
it is showing incorrect output. Am attaching the expected output again
March 15, 2022 at 6:05 pm
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply