日期:2014-05-17 浏览次数:20492 次
with tb (a,b)as(
select 'A',null union all
select 'B','A' union all
select 'C','A' union all
select 'D','A' union all
select 'M','B' union all
select 'N','B' union all
select 'R','C' union all
select 'P','C' union all
select 'Q','C'
),
t as (
select A,b=a from tb
union all
select t.a,b=tb.b from t inner join tb on t.b=tb.a
),
t1 as (
select b,COUNT(b)countb from t where b is not null and a!=b
group by b)
select a.a,2000 b,isnull(countb,0)*2000 c from tb a left join t1 b on a.a=b.b
with t as (
select name,jsr=name from dbo.emp1
union all
select t.name,b=emp1.jsr from t inner join emp1 on t.jsr=emp1.name
)
select code,qnt,ISNULL(b,0)*qnt as b from dbo.sale1 a left join (
select jsr,COUNT(jsr)b from t where jsr !='' and name!=jsr group by jsr)b
on a.code=b.jsr