首页 新闻 赞助 找找看

SQL筛选单个字段重复数据group by

0
悬赏园豆:20 [已关闭问题]

如:

C_ID  C_UID  C_Title  C_UTime

1        232  test1    2009-10-27 08:38:00

2        232  test2    2009-10-27 08:38:11

3        324  test3    2009-10-27 08:38:22

4        354  test4    2009-10-27 08:38:34

5        256  test5    2009-10-27 08:38:55

......

想的到

 

C_ID  C_UID  C_Title  C_UTime

5        256  test5    2009-10-27 08:38:55

2        232  test2    2009-10-27 08:38:11

4        354  test4    2009-10-27 08:38:34

......

 

过滤C_UID重复用户ID。 

用 GROUP BY 语句应该怎么写?

 

问题补充: 死神的背影 那需要怎么处理呢?请赐教!
淘吧淘吧的主页 淘吧淘吧 | 初学一级 | 园豆:180
提问于:2009-12-30 14:31
< >
分享
其他回答(3)
0

如果你是想4个字段都搜出来,那么你的需求逻辑就有问题了,group by 语句使用之后,没有被group by 的字段一定要有聚合函数去约束的,也就是说要有特殊的处理方法,单单group by 一个字段,另三个字段不做操作还想输出的话是要报错的

死神的背影 | 园豆:667 (小虾三级) | 2009-12-30 16:09
1

你的数据是不是有问题,是不是这样的

代码
declare @t table( C_ID int,C_UID int,C_Title varchar(10),C_UTime datetime)
insert @t


select 1,232,'test1','2009-10-27 08:38:00'
union all
select 2,232,'test2','2009-10-27 08:38:11'
union all
select 3,324,'test3','2009-10-27 08:38:22'
union all
select 4,324,'test4','2009-10-27 08:38:34'
union all
select 5,256,'test5','2009-10-27 08:38:55'



select * from @t a where not exists (select 1 from @t where C_UID=a.C_UID and C_UTime>a.C_UTime)

 

清海扬波 | 园豆:825 (小虾三级) | 2009-12-30 19:51
刚才忘记写cuid了。。应该是这样: (select c_uid,count(c_uid) as c_uid_count from tableC group by c_uid,c_uid_count) 这样就能找到重复C_UID超过1条数据的 然后,在这个结果上,用select * from tableC where C_UID in(select c_uid from (select c_uid,count(c_uid) as c_uid_count from tableC group by c_uid,c_uid_count) as tableTmp))这样做的效率有点低。看看别的人怎么说的。
支持(0) 反对(0) gxpotato | 园豆:44 (初学一级) | 2009-12-31 13:15
0

select max(C_ID),  C_UID, max(C_Title) , max(C_UTime) from 表名 group by C_UID

只好这样了。

liming1019 | 园豆:210 (菜鸟二级) | 2009-12-31 21:48
0

我觉得 select c_ID,c_UID,c_Title,c_UTime from DB group by c_UID ;

々孤星☆泪♂ | 园豆:13 (初学一级) | 2010-01-02 00:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册