怎么取得每个分组中的第一条记录

deepbluecs 2006-02-20 10:12:00
请教以下.怎么取得每个分组中的第一条记录.现在是这样一个问题:
序号 值
1 0
1 1
2 1
3 0
3 0

想要的结果是当同序号的记录只有一条时原样输出,当同序号的记录有多条时,其中只要有一个为1,则输出一条值为1的记录(其他相同序号的记录不在输出.).当同序号的记录值都是0时,输出一条值是0的.
我现在的想法是先进行分组排序.则每组中的第一条就是我要找的记录.可不知道怎么写才能找到每个分组的第一条.请大家帮忙看看.如果不用分组有其他办法也行.但前提是只能一个sql写完.
...全文
444 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
citywanderer2005 2006-02-21
  • 打赏
  • 举报
回复
SELECT DISTINCT A.ID, (CASE WHEN (A.ID IN (SELECT ID FROM TEST WHERE VALUE = '有')) THEN '有' ELSE '有' END) VALUE
FROM TEST A
deepbluecs 2006-02-20
  • 打赏
  • 举报
回复
ok .解决了.谢谢各位
-狙击手- 2006-02-20
  • 打赏
  • 举报
回复
declare @a table(aID int,a2 char(2) )
insert @a select 1,'有' union select 1,'无' union select 2,'无'
union all select 3,'无' union all select 3,'无'

select distinct a.*
from @a a
where not exists(select 1 from @a where a.aid = aid
and a2> a.a2)

/*

aID a2
----------- ----
1 有
2 无
3 无

*/
hchile 2006-02-20
  • 打赏
  • 举报
回复
漏了一个名称,查询可用,但程序中不可用
select id,max(ywbz) ywbz from 表 group by id
hchile 2006-02-20
  • 打赏
  • 举报
回复
select id,max(ywbz) from 表 group by id
zjcxc 元老 2006-02-20
  • 打赏
  • 举报
回复
-- 在上述语句中, 通过case when, 可以指定任意顺序


-- 如果还有其他列需要输出, 则在sql 2000中, 主键(或唯一键)来唯一定位1条记录和确定记录的顺序), 否则是无法确定那个是1组的第1条, 那个是第2条
zjcxc 元老 2006-02-20
  • 打赏
  • 举报
回复
-- 如果楼主的表就两个列, 那当然好处理, 因为结果就变成了有实值列='有' , 则输出1条有, 否则输出1条'无'

select distinct
aID,
a2=(select top 1 a2 from 表 where aID=a.aID order by case a2 when '有' then 1 else 2 end)
from 表 a
deepbluecs 2006-02-20
  • 打赏
  • 举报
回复
select top 1 * 只是查最顶的一条记录啊.我要的是每个id的记录都要有
deepbluecs 2006-02-20
  • 打赏
  • 举报
回复
我重新和大家说清楚一点我现在遇到的问题.
像下边这样:
id ywbz
1 '有'
1 '无'
2 '无'
3 '无'
3 '无'

我需要查询得到的结果是下面这样:
id ywbz
1 '有'
2 '无'
3 '无'
也就是单条记录原样输出,有重号的记录ywbz列只要有一个为'有'的输出'有'.全为'无'输出一条为'无'的记录.总之结果中没个id只出现一次.
hchile 2006-02-20
  • 打赏
  • 举报
回复
select top 1 * from tablename a where 序号=a.序号 order by a.值
如果值是有和无,则要看一看结果,将相应desc 增删,
deepbluecs 2006-02-20
  • 打赏
  • 举报
回复
不好意思.有个地方误导大家了.其实值列并不是0和1.而是汉字'有'和'无'.只是为了简便才那么写的.所以没有max(value)这一说.
hchile 2006-02-20
  • 打赏
  • 举报
回复
select top 1 * from tablename a where 序号=a.序号 order by 值 desc
wangdehao 2006-02-20
  • 打赏
  • 举报
回复
select b.id,b.value from (select distinct id,value from tb ) a
right join
( select id,value = max(value) from tb group by id ) b
on a.id=b.id and a.value = b.value
-狙击手- 2006-02-20
  • 打赏
  • 举报
回复
declare @a table(aID int,a2 int )
insert @a select 1,0 union all select 1,1 union all select 2,1
union all select 1,1 union all select 2,1
union all select 1,0 union all select 2,0

select distinct a.*
from @a a
where not exists(select 1 from @a where a.aid = aid
and a2> a.a2)

/*

aID a2
----------- -----------
1 1
2 1
*/
-狙击手- 2006-02-20
  • 打赏
  • 举报
回复
一楼二楼都错了,忘了过虑重复的.就拿2楼吧,测试数据多加几条就不行了,如下:
declare @a table(aID int,a2 int )
insert @a select 1,0 union all select 1,1 union all select 2,1
union all select 1,1 union all select 2,1
union all select 1,0 union all select 2,0

select distinct a.*
from @a a
where not exists(select 1 from @a where a.aid = aid
and a2> a.a2)

/*

aID a2
----------- -----------
1 1
2 1
*/
wangdehao 2006-02-20
  • 打赏
  • 举报
回复

select b.id,b.value from tb a
right join
( select id,value = max(value) from tb group by id ) b
on a.id=b.id and a.value = b.value
wgsasd311 2006-02-20
  • 打赏
  • 举报
回复
一楼二楼都错了,忘了过虑重复的.就拿2楼吧,测试数据多加几条就不行了,如下:
declare @a table(aID int,a2 int )
insert @a select 1,0 union all select 1,1 union all select 2,1
union all select 1,1 union all select 2,1
union all select 1,0 union all select 2,0

select a.*
from @a a
where not exists(select 1 from @a where a.aid = aid
and a2> a.a2)

/*

aID a2
----------- -----------
1 1
2 1
1 1
2 1
*/
zjcxc 元老 2006-02-20
  • 打赏
  • 举报
回复
你的表中没有主键? 如果没有主键, 则无法确定顺序, 也就无法得到第组第1条序号为1的记录.
wgsasd311 2006-02-20
  • 打赏
  • 举报
回复
select distinct 序号,值 from tb a where not exists
(select 1 from tb where 序号=a.序号 and 值>a.值)
-狙击手- 2006-02-20
  • 打赏
  • 举报
回复
declare @a table(aID int,a2 int )
insert @a select 1,0 union select 1,1 union select 2,1

select a.*
from @a a
where not exists(select 1 from @a where a.aid = aid
and a2> a.a2)

/*

aID a2
----------- -----------
1 1
2 1
*/
加载更多回复(2)

34,590

社区成员

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

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