SQL面试题 五(重复数据、统计)
2012-04-24 11:47
776 查看
题一:表stu
+----+------+-----+-------+
| id | sno | cno | score |
+----+------+-----+-------+
| 1 | 1001 | 1 | 89 |
| 3 | 1001 | 1 | 89 |
| 2 | 1002 | 1 | 87 |
| 4 | 1002 | 1 | 90 |
| 5 | 1003 | 1 | 86 |
+----+------+-----+-------+
1、查询出sno,cno重复的数据:
> select * from rs where (sno,cno) in (select sno,cno from rs group by sno,cno having count(*)>1);
2、查询sno,cno,score都重复的数据,且除id最小的一条数据外:
> select * from rs where (sno,cno,score) in(select sno,cno,score from rs group by sno,cno,score having count(*)>1)and
id not in (select min(id) from rs group by sno,cno,score having count(*)>1);
3、删除sno,cno,score都重复的数据,保留id最小的一条数据:
>(SQL Server未验证)delete from rs where (sno,cno,score) in(select sno,cno,score from rs group by sno,cno,score having count(*)>1)and id not in (select
min(id) from rs group by sno,cno,score having count(*)>1);
>(MySQL)
create table temp as select * from rs where (sno,cno,score) in(select sno,cno,score from rs group by sno,cno,score
having count(*)>1)and id not in (select min(id) from rs group by sno,cno,score having count(*)>1);
delete from rs where id in(select id from temp);
drop table temp;
题二:有以下三张表
商品表:tp
+-------+-------+
| pcode | pname |
+-------+-------+
| 1 | cpu |
| 2 | 内存 |
| 3 | 硬盘 |
城市表:tc
+-----+-------+
| cno | cname |
+-----+-------+
| 101 | 广州 |
| 102 | 深圳 |
| 103 | 上海 |
+-----+-------+
销售表:ts
+-------+-----+-------+
| pcode | cno | count |
+-------+-----+-------+
| 1 | 101 | 10000 |
| 1 | 102 | 500 |
| 1 | 103 | 20000 |
| 2 | 101 | 40000 |
| 2 | 103 | 30000 |
| 3 | 102 | 90000 |
+-------+-----+-------+
问题:需要得到以下结构的统计结果,写出SQL语句:
广州 深圳 上海 总计
CPU 10000 5000 20000 35000
内存 40000 0 30000 70000
硬盘 0 90000 0 90000
总计 50000 95000 50000 195000
>
+----+------+-----+-------+
| id | sno | cno | score |
+----+------+-----+-------+
| 1 | 1001 | 1 | 89 |
| 3 | 1001 | 1 | 89 |
| 2 | 1002 | 1 | 87 |
| 4 | 1002 | 1 | 90 |
| 5 | 1003 | 1 | 86 |
+----+------+-----+-------+
1、查询出sno,cno重复的数据:
> select * from rs where (sno,cno) in (select sno,cno from rs group by sno,cno having count(*)>1);
2、查询sno,cno,score都重复的数据,且除id最小的一条数据外:
> select * from rs where (sno,cno,score) in(select sno,cno,score from rs group by sno,cno,score having count(*)>1)and
id not in (select min(id) from rs group by sno,cno,score having count(*)>1);
3、删除sno,cno,score都重复的数据,保留id最小的一条数据:
>(SQL Server未验证)delete from rs where (sno,cno,score) in(select sno,cno,score from rs group by sno,cno,score having count(*)>1)and id not in (select
min(id) from rs group by sno,cno,score having count(*)>1);
>(MySQL)
create table temp as select * from rs where (sno,cno,score) in(select sno,cno,score from rs group by sno,cno,score
having count(*)>1)and id not in (select min(id) from rs group by sno,cno,score having count(*)>1);
delete from rs where id in(select id from temp);
drop table temp;
题二:有以下三张表
商品表:tp
+-------+-------+
| pcode | pname |
+-------+-------+
| 1 | cpu |
| 2 | 内存 |
| 3 | 硬盘 |
城市表:tc
+-----+-------+
| cno | cname |
+-----+-------+
| 101 | 广州 |
| 102 | 深圳 |
| 103 | 上海 |
+-----+-------+
销售表:ts
+-------+-----+-------+
| pcode | cno | count |
+-------+-----+-------+
| 1 | 101 | 10000 |
| 1 | 102 | 500 |
| 1 | 103 | 20000 |
| 2 | 101 | 40000 |
| 2 | 103 | 30000 |
| 3 | 102 | 90000 |
+-------+-----+-------+
问题:需要得到以下结构的统计结果,写出SQL语句:
广州 深圳 上海 总计
CPU 10000 5000 20000 35000
内存 40000 0 30000 70000
硬盘 0 90000 0 90000
总计 50000 95000 50000 195000
>
相关文章推荐
- java面试题,不用sql的sum统计数据
- 查找不重复的统计数据的sql语句的写法
- SQL统计相同重复的数据
- 使用SQL语句对重复记录查询、统计重复次数、删除重复数据
- 使用SQL语句对重复记录查询、统计重复次数、删除重复数据
- 查找不重复的统计数据的sql语句的写法
- 面试题中经常遇到的SQL题:删除重复数据,保留其中一条
- sql中统计一列中重复的数据个数
- SQL 五(重复数据、统计)
- sql不重复的查找统计数据
- 面试题(一)-sql查询重复数据
- sql不重复的查找统计数据(经典)
- SQL 删除重复记录,同时统计重复记录某一列的数据 以及 SET XACT_ABORT
- SQL DISTINCT去掉重复的数据统计方法
- SQL语句-统计全部数据,过滤掉其中两个字段同时重复的记录
- SQL语句-统计全部数据,过滤掉其中两个字段同时重复的记录
- SQL不重复查找统计数据
- 一条SQL语句插入大量数据 和查看某个表的字段是否有重复值
- Pandas学习笔记之重复数据统计