码迷,mamicode.com
首页 > 数据库 > 详细

sql leetcode -Duplicate Emails

时间:2017-08-01 20:39:13      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:group by   table   通过   ges   使用   having   ica   tin   from   

技术分享

第一种解法:

select distinct p1.Email as Email from Person p1, Person p2 where p1.Email=p2.Email and p1.Id>p2.id;

  第二种解法:

select Email from Person group by Email having count(Email)>1;

  sql 中有一系列 聚合函数: sum, count, max, avg, 这些函数作用域多条记录上

select sum(population) from b_table;

而通过group by 子句, 可以让sum和 count 这些函数对于属于一组的数据起作用。 计算population,可以指定region

select region, SUM(population),SUM(area)
from b_table 
group by region

  having 子句可以筛选成组后的数据,where子句在聚合前筛选记录, 也就是作用域group by ,having子句之前,而having 子句对聚合后的记录进行筛选:

select region, sum(population),sum(area)
from b_table
group by region 
having sum(population)>100000

  不能使用where来筛选超过100000的地区,因为表汇总不存在这样的记录。。。

sql leetcode -Duplicate Emails

标签:group by   table   通过   ges   使用   having   ica   tin   from   

原文地址:http://www.cnblogs.com/fanhaha/p/7270150.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!