title: Sybase: HAVING operates on rows?
body: <p>I've came across the following SYBASE SQL:</p>
  
  <pre><code>-- Setup first
  create table #t (id int, ts int)
  go
  
  insert into #t values (1, 2)
  insert into #t values (1, 10)
  insert into #t values (1, 20)
  insert into #t values (1, 30)
  
  insert into #t values (2, 5)
  insert into #t values (2, 13) 
  insert into #t values (2, 25)
  go
  
  declare @time int select @time=11
  -- This is the SQL I am asking about
  select * from (select * from #t where ts &lt;= @time) t group by id having ts = max(ts)
  go
  </code></pre>
  
  <p>The results of this SQL are </p>
  
  <pre><code> id          ts          
   ----------- ----------- 
             1          10 
             2           5 
  </code></pre>
  
  <p>This looks like HAVING condition applied to rows rather than groups. Can someone please point me at a place is Sybase 15.5 documentation where this case is described? All I see is "HAVING operates on groups". The closest I see in the docs is:</p>
  
  <blockquote>
    <p>The having clause can include columns or expressions that are not in
    the select list and not in the group by clause.</p>
  </blockquote>
  
  <p>(Quote from <a href="http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.ase_15.0.commands/html/commands/commands60.htm" rel="nofollow">here</a>).</p>
  
  <p>However, they don't exactly explain what happens when you do that.</p>