A community member has associated this post with a similar question:
Sum of different date by id

Only moderators can edit this content.

Date Duration by ID

Shambhu Rai 1,406 Reputation points
2022-08-15T08:14:13.85+00:00

Hi Expert,

I wanted to calculate max date for prodstatus =5 - min date for prodstatus=4 and max date for prodstatus =6 - min date for prodstatus=5

Create table
CREATE TABLE [dbo].testdata ON [PRIMARY]
GO

Insert
insert into [dbo].[testdata]

values('Prod1000','873','7','2021-06-16', '11-05-2022', '4'),
(' ','873', '18', '2022-05-12',NULL,'7'),
('Prod1000', '873', '19', '2022-05-12', '28-06-2022','5'),
('Prod1000', '1254','4', '2022-06-29', '2022-07-30', '6'),
('Prod1000', '1254','4', '2022-07-30', '2022-08-30', '7'),

expected output

230797-image.png

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