日期:2014-05-17  浏览次数:20324 次

求助,关于SQL的问题
已知有某表结构和示例数据如下
姓名 月份 薪资
张三 一月 8000
张三 二月 9000 
张三 三月 7000
李四 一月 9000 
李四 二月 8800 
王五 一月 8700 
王五 二月 9000
王五 三月 5600
...
写出一条或多条SQL语句,查询所有月份薪资都大于7000的人员姓名的SQL

特别指出,表中的李四因月份缺少一个月所以也要去掉

------解决方案--------------------
select distinct 姓名 from 
(
SELECT * FROM table1 t1
WHERE EXISTS (select 1 from table1 t2 where t1.姓名= t2.姓名 and t2.薪资>7000)
AND t1.NAME IN (SELECT 姓名 from table1 group by 姓名 having COUNT(月份) >=
(SELECT COUNT(月份) FROM table1 GROUP BY 姓名 ) )
  
) table_name
------解决方案--------------------
select distinct 姓名
from TB a
where not exists ( select 1 
from TB a2
where a2.薪资<7000
and a2.姓名=a.姓名

group by 姓名
having count(1)>=3
------解决方案--------------------
SQL code
create table #a 
(姓名 char(4) ,
月份 char(4),
薪资 money )
insert into #a
select '张三','一月',8000
union all
select '张三','二月',9000
union all
select '张三','三月',7000
union all
select '李四','一月',9000
union all
select '李四','二月',8800
union all
select '王五','一月',8700
union all
select '王五','二月',9000
union all
select '王五','三月',5600

select 姓名 from #a a where not exists (select 1 from #a where 薪资<7000 and 姓名=a.姓名)
group by 姓名
having COUNT (月份)>=3