日期:2014-05-18  浏览次数:20631 次

distinct多列问题
表Job
JobID(自动编号),jobCorporation,JobCount,JobStart,JobBack
1 11 0 2004-4-7 1
2 11 0 2005-4-4 1
3 22 0 2005-4-4 1
4 22 0 2005-4-4 1
5 22 0 2005-4-4 1

我想要的结果:
JobID(自动编号),jobCorporation,JobCount,JobStart,JobBack
1 11 0 2004-4-7 1
3 22 0 2005-4-4 1

消除jobCorporation重复列就行了 我是这样写的
select distinct jobCorporation,JobID,JobCount,JobStart from job where JobBack=1 order by JobCount desc,JobStart desc
可是用distinct取一列可以 但是取多列的时候就不行
请问这个该怎么做呢??


------解决方案--------------------
SQL code
--> 测试数据: #Job
if object_id('tempdb.dbo.#Job') is not null drop table #Job
create table #Job (JobID int,jobCorporation int,JobCount int,JobStart datetime,JobBack int)
insert into #Job
select 1,11,0,'2004-4-7',1 union all
select 2,11,0,'2005-4-4',1 union all
select 3,22,0,'2005-4-4',1 union all
select 4,22,0,'2005-4-4',1 union all
select 5,22,0,'2005-4-4',1

select * from #Job as t where not exists (select 1 from #Job where jobCorporation=t.jobCorporation and JobID<t.JobID)
/*
JobID       jobCorporation JobCount    JobStart                JobBack
----------- -------------- ----------- ----------------------- -----------
1           11             0           2004-04-07 00:00:00.000 1
3           22             0           2005-04-04 00:00:00.000 1
*/

------解决方案--------------------
有两个意义上的重复记录,一是完全重复的记录,也即所有字段均重复的记录,二是部分关键字段重复的记录,比如Name字段重复,而其他字段不一定重复或都重复可以忽略。

1、对于第一种重复,比较容易解决,使用

  select distinct * from tableName

  就可以得到无重复记录的结果集。

  如果该表需要删除重复的记录(重复记录保留1条),可以按以下方法删除

  select distinct * into #Tmp from tableName

  truncate table tableName

  select * into tableName from #Tmp

  drop table #Tmp

  发生这种重复的原因是表设计不周产生的,增加唯一索引列即可解决。

  2、这类重复问题通常要求保留重复记录中的第一条记录,操作方法如下

  假设有重复的字段为Name,Address,要求得到这两个字段唯一的结果集

  select identity(int,1,1) as autoID, * into #Tmp from tableName

  select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoID

  select * from #Tmp where autoID in(select autoID from #tmp2)

  最后一个select即得到了Name,Address不重复的结果集(但多了一个autoID字段,实际写时可以写在select子句中省去此列)

------解决方案--------------------
SQL code
--按某一字段分组取最大(小)值所在行的数据
(爱新觉罗.毓华 2007-10-23于浙江杭州)
/*
数据如下:
name val memo
a    2   a2(a的第二个值)
a    1   a1--a的第一个值
a    3   a3:a的第三个值
b    1   b1--b的第一个值
b    3   b3:b的第三个值
b    2   b2b2b2b2
b    4   b4b4
b    5   b5b5b5b5b5
*/
--创建表并插入数据:
create table tb(name varchar(10),val int,memo varchar(20))
insert into tb values('a',    2,   'a2(a的第二个值)')
insert into tb values('a',    1,   'a1--a的第一个值')
insert into tb values('a',    3,   'a3:a的第三个值')
insert into tb values('b',    1,   'b1--b的第一个值')
insert into tb values('b',    3,   'b3:b的第三个值')
insert into tb values('b',    2,   'b2b2b2b2')
insert into tb values('b',    4,   'b4b4')
insert into tb values('b',    5,   'b5b5b5b5b5')
go

--一、按name分组取val最大的值所在行的数据。
--方法1:
select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
--方法2:
select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
--方法3:
select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
--方法4:
select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
--方法5
select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.n