求如何实现这样的逻辑

ajdkjalj 2018-10-11 11:28:53
if OBJECT_ID('A') IS NOT NULL
DROP TABLE [A]
CREATE TABLE [A](a varchar(10),b varchar(30),c varchar(20),d int)
insert into [a]
select '001' ,'aa','cc',50 union all
select '001','bb','dd',50 union all
select '003','aa','ff',100 union all
select '003','df','ff',100


查找出a为001,b为aa,d为50的,满足的列显示,不满足的列为空,查出结果是
001 aa 50
001 null 50
null aa null
...全文
122 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
select *
from
(
select
case when a = '001' then '001' end a,
case when b = 'aa' then 'aa' end b,
case when d = '50' then '50' end d
from A
)t
where coalesce(a,b,d) is not null
/*
a b d
001 aa 50
001 NULL 50
NULL aa NULL
*/
卖水果的net 2018-10-12
  • 打赏
  • 举报
回复

select 
 case when a = '001' then '001' end a,
 case when b = 'aa' then 'aa' end b,
 case when d = '50' then '50' end d
from A 
where a = '001' or b = 'aa' or d = '50'
go

(4 行受影响)
a    b    d
---- ---- ----
001  aa   50
001  NULL 50
NULL aa   NULL

(3 行受影响)


27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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