再提
有表A:
字段1 字段2
01 A
01 B
01 D
02 C
02 D
02 D
03 B
03 B
.............
假定有1万条记录(如何写程序)
要求得表B:
XH A B C D 总计
01 1 1 0 1 3
02 0 0 1 2 3
03 0 2 0 0 2
小计 1 3 1 3 8
[解决办法]
select xh,sum(iif(f1= 'A ',1,0)) AS A,;
sum(iif(f1= 'B ',1,0)) AS B,;
sum(iif(f1= 'C ',1,0)) AS C,;
sum(iif(f1= 'D ',1,0)) AS D,;
COUNT(*) AS 总计;
FROM QQ ;
GROUP BY XH;
UNION;
SELECT '小计 ',sum(iif(f1= 'A ',1,0)) AS A,;
sum(iif(f1= 'B ',1,0)) AS B,;
sum(iif(f1= 'C ',1,0)) AS C,;
sum(iif(f1= 'D ',1,0)) AS D,;
COUNT(*);
FROM QQ
假设只有A、B、C、D,否则用动态SQL语句
[解决办法]
你的前一个帖中 syecho 已经给出方法了。