Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

  -- "find the maximum value of 'views',
  -- where 'sales' is greater than its mean, per 'id'".

  select max(views), id  -- "find the maximum value of   'views',
  from example_table as et 
  where exists 
  (
    SELECT *
      FROM 
    ( 
      SELECT id, avg(sales) as mean_sales 
      FROM example_table
      GROUP by id 
    ) as f --  
    where et.sales  > f.mean_sales -- where 'sales' is greater than its mean
    and et.id = f.id 
  )
  group by id; -- per 'id'".


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: