從titles表獲取按照title進(jìn)行分組,每組個(gè)數(shù)大于等于2,給出title以及對(duì)應(yīng)的數(shù)目t。
注意對(duì)于重復(fù)的emp_no進(jìn)行忽略。
CREATE TABLE IF NOT EXISTS "titles" (
`emp_no` int(11) NOT NULL,
`title` varchar(50) NOT NULL,
`from_date` date NOT NULL,
`to_date` date DEFAULT NULL);
解析:由于要對(duì)emp_no 去重,所以考慮先對(duì)emp_no去重,之后再對(duì)title分組
解法一:
select title, count(title) as t
from(
select distinct emp_no,title
from titles
)
group by title
having t >= 2;
解法二:
select title, count(distinct emp_no) as t
from titles
group by title
having t >= 2;