稍微复杂的去掉重复记录问题,解决马上给分!!!!!!在线等!!!再问!!!!

chenyushen 2006-12-25 06:06:45
之前http://community.csdn.net/Expert/topic/5254/5254854.xml?temp=.4921381的问题已经由 hellowork(一两清风) 解决,非常感谢,但是如果把原记录改为:

a b c
-----------------
1 1 a
1 2 b
1 3 c
2 4 d
1 5 e
2 6 f
2 7 g
3 8 h

要求结果为:
a b c
-----------------
1 1 a
2 b
3 c
5 d
2 4 e
6 f
7 g
3 8 h

则无法解决,再问!!!

...全文
1704 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
zz_zzl2008 2007-02-13
  • 打赏
  • 举报
回复
结果为下面的该怎么写?
a b c
-----------------
1 5 d
2 7 g
3 8 h
Ghost360 2007-01-15
  • 打赏
  • 举报
回复
Mark
superman_yc 2006-12-27
  • 打赏
  • 举报
回复
mark
hanbinyang 2006-12-26
  • 打赏
  • 举报
回复
mark!
loveeqing 2006-12-26
  • 打赏
  • 举报
回复
up
avril1986swj 2006-12-26
  • 打赏
  • 举报
回复
加 distinct 可以吗?
laowang2 2006-12-26
  • 打赏
  • 举报
回复
mark
chenyushen 2006-12-25
  • 打赏
  • 举报
回复
再次感谢Hahahahahaha(哈,哈哈,哈哈哈...冒牌主人)、hellowork(一两清风),两位都是高手,分数不太好分啊,hellowork(一两清风)上次已经拿了100了,这次少拿点吧,呵呵


给个msn啊!!??
hellowork 2006-12-25
  • 打赏
  • 举报
回复
这样的sql不是循环也不是嵌套,只是一种相关查询,体现在下面这行代码中:
when b = (select top 1 b from @t where a = x.a order by b)
即源表中每扫描一行都判断"b = (select top 1 b from @t where a = x.a order by b)"这个条件是否成立,关联之处在于子查询中的a = x.a
hellowork 2006-12-25
  • 打赏
  • 举报
回复
declare @t table(a int,b int,c varchar(20))
insert @t
select 1, 1, 'a' union all
select 1, 2, 'b' union all
select 1, 3, 'c' union all
select 2, 4, 'd' union all
select 1, 5, 'e' union all
select 2, 6, 'f' union all
select 2, 7, 'g' union all
select 3, 8, 'h'

----方法1
select
d = case /*取个新列名,防止与a同名,以使a能用于排序*/
when b = (select top 1 b from @t where a = x.a order by b)
then cast(a as varchar(10))
else '' end,b,c
from @t as x
order by a,b /*按照a列原值排序,再按b列原值排序*/

----方法2:使用排序后的子查询作为查询的数据源(效率要低些)
select
a = case
when b = (select top 1 b from @t where a = x.a order by b)
then cast(a as varchar(10))
else '' end,b,c
from (select top 100 percent * from @t order by a,b) as x /*子查询表示对源数据进行排序*/
chenyushen 2006-12-25
  • 打赏
  • 举报
回复
Hahahahahaha(哈,哈哈,哈哈哈...冒牌主人):
“就是指明先按a字段排序,加了运算(*1)表示必须选择字段”的意思是不是说加了(*1)就不按结果集中a字段排序,而是按数据库中实际记录的a字段值排序???


刚才有两个字打错了!
chenyushen 2006-12-25
  • 打赏
  • 举报
回复
Hahahahahaha(哈,哈哈,哈哈哈...冒牌主人):
“就是指明先按a字段排序,加了运算(*1)表示必须选择字段”的意思是不是说加了(*1)就不按结果集中a字段排序,而是按时据数据库中记录的a字段值排序???
chenyushen 2006-12-25
  • 打赏
  • 举报
回复
Hahahahahaha(哈,哈哈,哈哈哈...冒牌主人) 太感谢了,不过sorry,还得问你
“就是指明先按a字段排序,加了运算(*1)表示必须选择字段”,这句不太明白,麻烦在给说说,谢谢,另外,留个msn可以么?

等你30分钟,不管怎样,30分钟之后给分,再次感谢!!!!
Hahahahahaha 2006-12-25
  • 打赏
  • 举报
回复
这种情况只是个特例,没有研究价值
Hahahahahaha 2006-12-25
  • 打赏
  • 举报
回复
如果
order by a,b

sql的优化器会认为按照结果的第一个字段排序,这样所有空的都跑到最前面去了
order by a*1,b
就是指明先按a字段排序,加了运算(*1)表示必须选择字段
也可以:
declare @t table(a int,b int,c varchar(20))
insert @t
select 1, 1, 'a' union all
select 1, 2, 'b' union all
select 1, 3, 'c' union all
select 2, 4, 'd' union all
select 1, 5, 'e' union all
select 2, 6, 'f' union all
select 2, 7, 'g' union all
select 3, 8, 'h'


----查询
select
a1 = case
when b = (select top 1 b from @t where a = x.a order by b)
then cast(a as varchar(10))
else '' end,b,c
from @t as x
order by a,b

不过前端也许就不方便了

chenyushen 2006-12-25
  • 打赏
  • 举报
回复
Hahahahahaha(哈,哈哈,哈哈哈...冒牌主人),感谢!!!强!!
呵呵,不过想问一下,a*1有什么作用,另外这段sql是不是相当于一个嵌套,告诉我给你分,当然不告诉也会给,呵呵
Hahahahahaha 2006-12-25
  • 打赏
  • 举报
回复
declare @t table(a int,b int,c varchar(20))
insert @t
select 1, 1, 'a' union all
select 1, 2, 'b' union all
select 1, 3, 'c' union all
select 2, 4, 'd' union all
select 1, 5, 'e' union all
select 2, 6, 'f' union all
select 2, 7, 'g' union all
select 3, 8, 'h'


----查询
select
a = case
when b = (select top 1 b from @t where a = x.a order by b)
then cast(a as varchar(10))
else '' end,b,c
from @t as x
order by a*1,b

---结果
a b c
---------- ----------- --------------------
1 1 a
2 b
3 c
5 e
2 4 d
6 f
7 g
3 8 h

(所影响的行数为 8 行)

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧