请教一个SQL怎么写

tyrival 2015-11-02 08:32:15
表T中,字段F可能会有2个值A或者B。
如果存在F=A的记录,取出F=A的记录,忽略F=B;
如果不存在F=A的记录,取出F=B的记录,也就是全部记录。
这个sql怎么写
...全文
62 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
zbdzjx 2015-11-02
  • 打赏
  • 举报
回复
with table1 as 
(
select 1 id, 'A' col1 union all
select 1 id, 'B' col1 union all
select 1 id, 'B' col1
)
select * from table1 
where col1 in (case when (select count(col1) from table1 where col1='A')>0 then 'A' else 'B' end)

22,209

社区成员

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

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