title: Min() and Max() based on partition in sql server
body: <p>I want to use  min &amp; max function but on certain criteria.</p>
  
  <pre><code>Create Table #Test (Id Int Identity(1,1), Category Varchar(100), DateTimeStamp DateTime)
  
  
  
      Insert into #Test (Category,DateTimeStamp) values ('c1','2019-08-13 01:00:13.503')
          Insert into #Test (Category,DateTimeStamp) values ('c1','2019-08-13 02:00:13.503')
          Insert into #Test (Category,DateTimeStamp) values ('c1','2019-08-13 03:00:13.503')
          Insert into #Test (Category,DateTimeStamp) values ('c1','2019-08-13 04:00:13.503')
          Insert into #Test (Category,DateTimeStamp) values ('c1','2019-08-13 05:00:13.503')
          Insert into #Test (Category,DateTimeStamp) values ('c2','2019-08-13 06:00:13.503')
          Insert into #Test (Category,DateTimeStamp) values ('c2','2019-08-13 07:00:13.503')
          Insert into #Test (Category,DateTimeStamp) values ('c2','2019-08-13 08:00:13.503')
          Insert into #Test (Category,DateTimeStamp) values ('c2','2019-08-13 09:00:13.503')
          Insert into #Test (Category,DateTimeStamp) values ('c2','2019-08-13 10:00:13.503')  
          Insert into #Test (Category,DateTimeStamp) values ('c1','2019-08-13 11:00:13.503')
  </code></pre>
  
  <p>Current Query With Output</p>
  
  <pre><code>select category, min(DateTimeStamp) as minn , max(DateTimeStamp) as maxx from #Test
  group by category
  </code></pre>
  
  <p><strong>Current Output</strong></p>
  
  <p><a href="https://i.stack.imgur.com/sppFY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sppFY.png" alt="Current Output"></a></p>
  
  <p><strong>Expected Output</strong></p>
  
  <p><a href="https://i.stack.imgur.com/jmmGC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jmmGC.png" alt="Expected Output"></a></p>