1

I have data similar to the following in SQL Server. I would like to query the single table for the category, min value from StartDate and max value from EndDate. Hopefully the following mock up of the data will help explain - thanks

DATA:

  Category  StartDate   EndDate
  -------------------------------
       A    1/1/2018    1/11/2018
       A    1/3/2018    1/13/2018
       B    1/1/2018    1/11/2018
       B    1/9/2018    1/19/2018
       A    1/5/2018    1/15/2018
       C    1/4/2018    1/14/2018
       A    1/1/2018    1/11/2018
       C    1/7/2018    1/17/2018

Desired result of query:

  Category  StartDate   EndDate
  --------------------------------
       A    1/1/2018    1/15/2018
       B    1/1/2018    1/19/2018
       C    1/4/2018    1/17/2018

2 Answers 2

4

What's wrong with GROUP BY and MIN() & MAX() ?

select Category, min(startdate) startdate, max(enddate) enddate
from table t
group by Category;
Sign up to request clarification or add additional context in comments.

Comments

3

Use group by

select category, min(startDate), max(endDate)
from my_table  
group by category

1 Comment

Thanks! I thought it would be more involved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.