如图所示,这是一张表里边的数据。
业务说明:
按照所有的小类名称(a-g)查询员工编号为YG001的员工对应的(a-g)的所有小类金额
select 小类金额 from 表名 where name IN (a,b,c,d,e,f,g) and 编号=YG001
自己照着吧字段套上去,应该可以
你这个问题最终是求他小类别名称?
如果是查询小类别名称的话,肯定有一个小类别表,这个表里标注着每个小类别对应不同的名称。需要做表关联。
如果说问题写错,是要求小类金额,那么楼上写的,就可以达到你想要的效果。
可以建一张表(命名表B) 两列
小类名称 金额
a 0
... ...
f 0
select B.小类名称,B.小类金额 from 表B B where B.小类名称 not in (select A.小类名称 from 表A A )
UNION
select 小类名称,小类金额 from 表A ;
这样就可以了,只不过要建一张表
select a.a3,
case when b.a1 is null then 'YG001' else b.a1 end,
case when b.a4 is null then 0 else b.a4 end
from
(
select distinct a3
from zTest
) a left join zTest b on a.a3 = b.a3 and b.a1 like 'YG001'
然后对应这字段改改。
select 小类名称,小类金额 from table where 员工编号 = “YG001”order by 小类名称