解决方案 »
- 存储过程中datetime类型的变量作为查询条件时怎么处理
- sqlserver 能实现一张表同时有两个字段自增1吗
- SqlServer的外连接问题(查询中包含不允许的外联接请求)
- !!!高分求教一个简单的数据库(c/s)问题!!
- 日历关联查询视图
- 求SQL2000问题 ?恐怖!!!!!!恐怖!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- 一个修改所有字段中的值是问题
- 麻烦大家帮我想一个查询SQL啊
- 安装SQL 2008 Express sp1时出现registry properties are not valid under this context
- 有一张表,我想得出以下效果,比如与前一天相比销售额,与上一周相比销售额,与同期上一个月相比销售额
- 数据库 采番的作用 在线等
- sql语句如何去重保留一条记录啊
再去重前提是 你一个班里不会出现两个 相同姓名 年龄 性别的人
if OBJECT_ID('student') is not null drop table student
create table student(ID int identity(1,1),姓名 nvarchar(20),性别 int,年龄 int,班级 int)
go
insert into student(姓名,性别,年龄,班级)
select '张三',1,18,1 union all
select '李四',1,18,1 union all
select '赵武',1,19,1 union all
select '小明',1,17,1 union all
select '刘涛',1,18,2 union all
select '小红',2,17,2 union all
select '小芳',2,18,2 union all
select '小红',2,18,2 union all
select '张三',1,18,1 union all
select '李四',1,17,1
--查询
select 姓名,性别,年龄 From student GROUP BY 班级,姓名,性别,年龄
union all
select 姓名,性别,年龄 From student GROUP BY 班级,姓名,性别,年龄 having COUNT(1)>1/**
姓名 性别 年龄
-------------------- ----------- -----------
李四 1 17
李四 1 18
小明 1 17
张三 1 18
赵武 1 19
刘涛 1 18
小芳 2 18
小红 2 17
小红 2 18
张三 1 18(10 行受影响)
**/
----------------------------------------------------------------
-- Author :TravyLee(物是人非事事休,欲语泪先流!)
-- Date :2012-11-27 12:19:06
-- Version:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[test]
if object_id('[test]') is not null
drop table [test]
go
create table [test]
(
[ID] int,
[姓名] varchar(4),
[性别] int,
[年龄] int,
[班级] int
)
insert [test]
select 1,'张三',1,18,1 union all
select 2,'李四',1,18,1 union all
select 3,'赵武',1,19,1 union all
select 4,'小明',1,17,1 union all
select 5,'刘涛',1,18,2 union all
select 6,'小红',2,17,2 union all
select 7,'小芳',2,18,2 union all
select 8,'小红',2,18,2 union all
select 9,'张三',1,18,1 union all
select 10,'李四',1,17,1
goselect
*
from
test a
where
a.ID=(
select
min(ID)
from
test b
where
a.姓名=b.姓名
and a.性别=b.性别
and a.年龄=b.年龄
)
order by
id
/*
ID 姓名 性别 年龄 班级
----------- ---- ----------- ----------- -----------
1 张三 1 18 1
2 李四 1 18 1
3 赵武 1 19 1
4 小明 1 17 1
5 刘涛 1 18 2
6 小红 2 17 2
7 小芳 2 18 2
8 小红 2 18 2
10 李四 1 17 1(9 行受影响)
*/--按照你的意思就是这个结果 但是估计你不是这个意思 给出你期待的结果