SQL 五(重复数据、统计)
2012-05-14 11:15
381 查看
转自 http://blog.csdn.net/yangyi22/article/details/7493041
题一:表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
>
题一:表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
>
相关文章推荐
- SQL语句-统计全部数据,过滤掉其中两个字段同时重复的记录
- 查找不重复的统计数据的sql语句的写法
- SQL DISTINCT去掉重复的数据统计方法
- 使用SQL语句对重复记录查询、统计重复次数、删除重复数据
- SQL 删除重复记录,同时统计重复记录某一列的数据 以及 SET XACT_ABORT
- SQL不重复查找统计数据
- 使用SQL语句对重复记录查询、统计重复次数、删除重复数据
- SQL面试题 五(重复数据、统计)
- sql不重复的查找统计数据
- sql不重复的查找统计数据(经典)
- 查找不重复的统计数据的sql语句的写法
- SQL语句-统计全部数据,过滤掉其中两个字段同时重复的记录
- SQL统计相同重复的数据
- sql中统计一列中重复的数据个数
- SQL删除重复记录,只保留一条数据。
- SQL按照日、周、月、年统计数据
- SQL删除重复数据方法
- 上千万或上亿数据(有重复),统计其中出现次数最多的N个数据. C++实现
- SQl不重复的所有数据(重复的只显示一条)