日期:2014-05-17 浏览次数:21001 次
select (select [系数] from [表2] where [系数关键字] = [表1].[系数关键字]) as [系数], * from [表1] where [是否统计]='是'
--drop table table1
create table table1(
id int,
xm varchar(100),
tj varchar(100),
gjz varchar(100)
)
--drop table table2
create table table2(
gjz varchar(100),
xs numeric(12,2)
)
insert into table1
select 1, '张三','是','北京' union
select 2, '李四','否','上海' union
select 3, '张三','是','上海' union
select 4, '王五','是','广州'
insert into table2
select '北京',0.5 union
select '上海',1.0 union
select '广州',1.5
select xm,sum(xs) "合计" from (select b.xs,a.* from table1 a,table2 b where a.gjz=b.gjz) T group by xm
create table tb1
(
代号 varchar(10),
姓名 varchar(10),
是否统计 varchar(2),
系数关键字 varchar(10)
)
create table tb2
(
系数关键字 varchar(10),
系数 float(8)
)
insert into tb1
select '1111','aaa','是','北京'
union all
select '1111','aaa','是','上海'
union all
select '2222','bbb','否','广州'
insert into tb2
select '北京',0.5
union all
select '上海',1.0
select 代号,姓名,
sum(case when 是否统计='是' then 系数 else 0 end) as 系数
from tb1 inner join tb2 on tb1.系数关键字=tb2.系数关键字 group by 代号,姓名
----------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-05-15 11:18:34