菜鸟求知:找不同行

wxshzf 2003-05-09 10:47:40
我有一张表如下:
telNo xcode money
83562387 1 34
83562388 2 54
83562389 1 37
83562390 3 44
83562391 1 47
83562392 2 34
83562393 1 56
83562394 3 12
83562395 1 36
我想得到按xcode为1,2,3的不同行,如果用select distinct xcode,仅能得到
xcode
1
2
3
但我要得到的一整行,如
83562387 1 34
83562388 2 54
83562390 3 44
...全文
33 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
sasacat 2003-05-09
select top 1 * from table where xcode in(select distinct xcode form table)
回复
米多 2003-05-09
不知这个是否可以:
select max(telno) as telNo,xcode,max(money) as money
from table
group by xcode
回复
select * from yourtable as A
where telNo = (select min(telNo) from yourtable where xcode = A.xcode)
回复
wxshzf 2003-05-09
如果xcode的值有多个呢?总不能用union写多个语句吧?我只关心xcode唯一,而telno与money则仅带着,其值大小及是否唯一并不重要
回复
psxfghost 2003-05-09
更保险一点的:
select top 1 * from table where xcode =1 order by telNo
union
select top 1 * from table where xcode =2 order by telNo
union
select top 1 * from table where xcode =3 order by telNo

^_^
回复
psxfghost 2003-05-09
这是你要实现的吗???
select telNo,xcode,min(money) as money from table where xcode=1 union
select telNo,xcode,max(money) as money from table where xcode=2 union
select telNo,xcode,max(money) as money from table where xcode=3
回复
sasacat 2003-05-09
select top 1 * from table where xcode =1
union
select top 1 * from table where xcode =2
union
select top 1 * from table where xcode =3
回复
相关推荐
发帖
MS-SQL Server
创建于2007-09-28

3.3w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
帖子事件
创建了帖子
2003-05-09 10:47
社区公告
暂无公告