select hyml,sum(case when qylx=qylx1 then nbxh else 0 end),
sum(case when qylx=qylx2 then nbxh else 0 end),
sum(case when qylx=qylx3 then nbxh else 0 end),
sum(case when qylx=qylx4 then nbxh else 0 end)
into table2
from table1
group by hyml
where sj>='2000-1-1' and sj<='2000.12.31'
sum(case when qylx=qylx2 then nbxh else 0 end),
sum(case when qylx=qylx3 then nbxh else 0 end),
sum(case when qylx=qylx4 then nbxh else 0 end)
into table2
from table1
group by hyml
where sj>='2000-1-1' and sj<='2000.12.31'
select hyml,sum(case when qylx=qylx1 then nbxh else 0 end),
sum(case when qylx=qylx2 then nbxh else 0 end),
sum(case when qylx=qylx3 then nbxh else 0 end),
sum(nbxh )
into table2
from table1
group by hyml
where sj>='2000-1-1' and sj<='2000.12.31'
select hyml,sum(case when qylx='有限公司' then 1 else 0 end),
sum(case when qylx='分公司' then 1 else 0 end),
sum(case when qylx='个体户' then 1 else 0 end),
count(*)
from table1
group by hyml
where sj>='2000-1-1' and sj<='2000.12.31'
举个例子吧!
源表table1有字段hyml(行业门类),qylx(企业类型),sj(时间),qymc(企业名称),nbxh(序号)
数据如下:
农林业 有限公司 2000.1.2 A 1
农林业 有限公司 2000.1.6 B 2
建筑业 有限公司 2000.1.3 C 3
制造业 个体户 2000.1.13 D 4
建筑业 有限公司 2000.5.3 E 5
建筑业 分公司 2000.7.3 F 6现在我想利用原表table1按行业门类作统计,并把统计结果插入到表table2中。
table2表的结构如下:
hyml(行业门类),qylx1(有限公司),qylx2(分公司),qylx3(个体户),total(合计)
结果类似于:2000.1.1-----2000.12.31期间的分布情况农林业 2 0 0 2
制造业 0 0 1 1
建筑业 2 1 0 3
能达到如上结果吗?
insert table2(hyml,qylx1,qylx2,qylx3,total)
select hyml,sum(case when qylx=N'有限公司' then 1 else 0 end),
sum(case when qylx=N'分公司' then 1 else 0 end),
sum(case when qylx=N'个体户' then 1 else 0 end),
count(*)
from table1
group by hyml
where sj>='2000-1-1' and sj<='2000.12.31'
select hyml,sum(case when qylx='有限公司' then 1 else 0 end),
sum(case when qylx='分公司' then 1 else 0 end),
sum(case when qylx='个体户' then 1 else 0 end),
count(hyml)
from table1
group by hyml
where sj>='2000-1-1' and sj<='2000.12.31'