首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

,关于SQL的有关问题

2012-10-17 
求助,关于SQL的问题已知有某表结构和示例数据如下姓名 月份 薪资张三 一月 8000张三 二月 9000张三 三月 7

求助,关于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 #aselect '张三','一月',8000union allselect '张三','二月',9000union allselect '张三','三月',7000union allselect '李四','一月',9000union allselect '李四','二月',8800union allselect '王五','一月',8700union allselect '王五','二月',9000union allselect '王五','三月',5600select 姓名 from #a a where not exists (select 1 from #a where 薪资<7000 and 姓名=a.姓名)group by 姓名having COUNT (月份)>=3 

热点排行