如:
表1
姓名
张三
李四表2
姓名 类别
张三 10
张三 20
张三 30求另存为表3
姓名 类别A 类别B 合计
张三 2(类别<=20) 1(类别>=30) 2*10-1*2
李四 0 0 0*10-0*2ADO+access+adoquery
表1
姓名
张三
李四表2
姓名 类别
张三 10
张三 20
张三 30求另存为表3
姓名 类别A 类别B 合计
张三 2(类别<=20) 1(类别>=30) 2*10-1*2
李四 0 0 0*10-0*2ADO+access+adoquery
select a.姓名 as 姓名, a.类别 as 类别A, b.类别 as 类别B, (a.类别*10 - b.类别*2) as 合计
from (select 姓名, count(类别) as 类别 from 表2 where 类别<=20 group by 姓名) a,
(select 姓名, count(类别) as 类别 from 表2 where 类别>=30 group by 姓名) b
where a.姓名 = b.姓名or
select 姓名, Sum(Case When 类别<=20 Then 1 Else 0 End) as 类别A, Sum(Case When 类别>=30 Then 1 Else 0 End) as 类别B, (Sum(Case When 类别<=20 Then 1 Else 0 End)*10-Sum(Case When 类别>=30 Then 1 Else 0 End)*2) as 合计
from 表2
group by 姓名
from (select 姓名, count(类别) as 类别 from 表2 where 类别<=20 group by 姓名) a,
(select 姓名, count(类别) as 类别 from 表2 where 类别>=30 group by 姓名) b
where a.姓名 = b.姓名
union
select 姓名 , 0 as 类别A, 0 as 类别B, 0 as 合计
from 表1
where 姓名 not in(select distinct 姓名 from 表2)可以不?