要返回这样的结果集,如何组合sql语句?

oneofcsdn 2003-10-17 10:45:26
table.a 列名: a b
列值为: 1 1
2 0
2 1
3 0
返回:a b
1 1
2 1
3 0
除了返回列a中值相同但b=1的行外,还要返回其他所有行。


...全文
78 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 2003-10-17
  • 打赏
  • 举报
回复
--我理解错了楼主的意思.应该是:

select * from tablea aa
where (select sum(1) from tablea where a=aa.a)>1 and b=1) or (select sum(1) from tablea where a=aa.a)=1
zjcxc 2003-10-17
  • 打赏
  • 举报
回复
用临时表:

select id=identity(int,1,1),* into #tb from tablea
select a,b from #tb aa where id=(select min(id) from #tb where a=aa.a)
drop table #tb
oneofcsdn 2003-10-17
  • 打赏
  • 举报
回复
楼上的老大:
可能我表述的不太清楚,上面的数值是我图方便写的,其实每个数值其实是个字符串,
我想返回这样记录集:表中有两个列a,b;列a中可能有值相同,这时我取b为指定值的的行,其他行不管b为何值,全部取出。
yujohny 2003-10-17
  • 打赏
  • 举报
回复
select a,max(b) from 表 group by a
sdhdy 2003-10-17
  • 打赏
  • 举报
回复
select * from tablename where a in (select a from tablename group by a having count(b)=1)
union all
select * from tablename where a in (select a from tablename group by a having count(b)>1) and b=1

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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