求查询语句一条。
表1
入库时间 品名 数量 人员代码 单位 状态
2007-01-01 AAAA 100 111 aaa 0
2007-01-01 AAAA 20 111 bbb 0
2007-01-01 BBBB 100 111 aaa 0
2007-01-01 AAAA 100 222 aaa 1
表2
入库时间 品名 数量 人员代码 单位 状态
2007-01-01 AAAA 100 111 aaa 0
2007-01-01 AAAA 20 111 bbb 0
2007-01-01 BBBB 100 111 aaa 0
2007-01-01 AAAA 100 222 aaa 1
表3
人员代码 姓名
111 张三
222 李四
汇总表1与表2中品名为AAAA,单位为aaa,状态是0的数量,并且显示姓名。
[解决办法]
create table A(入库时间 datetime, 品名 varchar(10), 数量 int, 人员代码 int, 单位 varchar(10), 状态 bit)
insert A select '2007-01-01 ', 'AAAA ', 100, 111, 'aaa ', 0
union all select '2007-01-01 ', 'AAAA ', 20, 111, 'bbb ', 0
union all select '2007-01-01 ', 'BBBB ', 100, 111, 'aaa ', 0
union all select '2007-01-01 ', 'AAAA ', 100, 222, 'aaa ', 1
create table B(入库时间 datetime, 品名 varchar(10), 数量 int, 人员代码 int, 单位 varchar(10), 状态 bit)
insert B select '2007-01-01 ', 'AAAA ', 100, 111, 'aaa ', 0
union all select '2007-01-01 ', 'AAAA ', 20, 111, 'bbb ', 0
union all select '2007-01-01 ', 'BBBB ', 100, 111, 'aaa ', 0
union all select '2007-01-01 ', 'AAAA ', 100, 222, 'aaa ', 1
create table C(人员代码 int, 姓名 varchar(10))
insert C select 111, '张三 '
union all select 222, '李四 '
select C.人员代码, C.姓名, 数量=sum(数量)
from
(
select * from A where 品名= 'AAAA ' and 人员代码=111 and 单位= 'aaa ' and 状态=0
union all
select * from B where 品名= 'AAAA ' and 人员代码=111 and 单位= 'aaa ' and 状态=0
)tmp inner join C on tmp.人员代码=C.人员代码
group by C.人员代码, C.姓名
--result
人员代码 姓名 数量
----------- ---------- -----------
111 张三 200
(1 row(s) affected)
[解决办法]
if object_id( 'pubs..表1 ') is not null
drop table 表1
go
create table 表1(
入库时间 varchar(10),
品名 varchar(10),
数量 int,
人员代码 varchar(10),
单位 varchar(10),
状态 varchar(10))
insert into 表1(入库时间,品名,数量,人员代码,单位,状态) values( '2007-01-01 ', 'AAAA ', 100, '111 ', 'aaa ', '0 ')
insert into 表1(入库时间,品名,数量,人员代码,单位,状态) values( '2007-01-01 ', 'AAAA ', 20 , '111 ', 'bbb ', '0 ')
insert into 表1(入库时间,品名,数量,人员代码,单位,状态) values( '2007-01-01 ', 'BBBB ', 100, '111 ', 'aaa ', '0 ')
insert into 表1(入库时间,品名,数量,人员代码,单位,状态) values( '2007-01-01 ', 'AAAA ', 100, '222 ', 'aaa ', '1 ')
if object_id( 'pubs..表2 ') is not null
drop table 表2
go
create table 表2(
入库时间 varchar(10),
品名 varchar(10),
数量 int,
人员代码 varchar(10),
单位 varchar(10),
状态 varchar(10))
insert into 表2(入库时间,品名,数量,人员代码,单位,状态) values( '2007-01-01 ', 'AAAA ', 100, '111 ', 'aaa ', '0 ')
insert into 表2(入库时间,品名,数量,人员代码,单位,状态) values( '2007-01-01 ', 'AAAA ', 20 , '111 ', 'bbb ', '0 ')
insert into 表2(入库时间,品名,数量,人员代码,单位,状态) values( '2007-01-01 ', 'BBBB ', 100, '111 ', 'aaa ', '0 ')
insert into 表2(入库时间,品名,数量,人员代码,单位,状态) values( '2007-01-01 ', 'AAAA ', 100, '222 ', 'aaa ', '1 ')
if object_id( 'pubs..表3 ') is not null
drop table 表3
go
create table 表3(
人员代码 varchar(10),
姓名 varchar(10))
insert into 表3(人员代码,姓名) values( '111 ', '张三 ')
insert into 表3(人员代码,姓名) values( '222 ', '李四 ')
select n.人员代码 , 表3.姓名 , n.数量 from
(
select 人员代码 , sum(数量) as 数量 from
(
select * from 表1 where 品名= 'AAAA ' and 单位 = 'aaa ' and 状态 = '0 '
union all
select * from 表2 where 品名= 'AAAA ' and 单位 = 'aaa ' and 状态 = '0 '
) m
group by 人员代码
) n,表3
where n.人员代码 = 表3.人员代码
drop table 表1,表2,表3
人员代码 姓名 数量
---------- ---------- -----------
111 张三 200
(所影响的行数为 1 行)
[解决办法]
declare @ta table (入库时间 datetime, 品名 varchar(10), 数量 int, 人员代码 int, 单位 varchar(10), 状态 bit)
insert @ta select '2007-01-01 ', 'AAAA ', 100, 111, 'aaa ', 0
union all select '2007-01-01 ', 'AAAA ', 20, 111, 'bbb ', 0
union all select '2007-01-01 ', 'BBBB ', 100, 111, 'aaa ', 0
union all select '2007-01-01 ', 'AAAA ', 100, 222, 'aaa ', 1
declare @tb table (入库时间 datetime, 品名 varchar(10), 数量 int, 人员代码 int, 单位 varchar(10), 状态 bit)
insert @tb select '2007-01-01 ', 'AAAA ', 100, 111, 'aaa ', 0
union all select '2007-01-01 ', 'AAAA ', 20, 111, 'bbb ', 0
union all select '2007-01-01 ', 'BBBB ', 100, 111, 'aaa ', 0
union all select '2007-01-01 ', 'AAAA ', 100, 222, 'aaa ', 1
declare @tc table (人员代码 int, 姓名 varchar(10))
insert @tc select 111, '张三 '
union all select 222, '李四 '
select c.人员代码,c.姓名,数量=sum(数量)
from
(select * from @ta a
where exists(select * from @ta where 人员代码=a.人员代码 and 品名= 'AAAA ' and 人员代码=111 and 单位= 'aaa ' and 状态=0)
union all
select * from @tb a
where exists(select * from @ta where 人员代码=a.人员代码 and 品名= 'AAAA ' and 人员代码=111 and 单位= 'aaa ' and 状态=0))
test join @tc c on c.人员代码=test.人员代码 group by c.人员代码,c.姓名
(4 行受影响)
(4 行受影响)
(2 行受影响)
人员代码 姓名 数量
----------- ---------- -----------
111 张三 440
(1 行受影响)
[解决办法]
create table A(入库时间 datetime, 品名 varchar(10), 数量 int, 人员代码 int, 单位 varchar(10), 状态 bit)
insert A select '2007-01-01 ', 'AAAA ', 100, 111, 'aaa ', 0
union all select '2007-01-01 ', 'AAAA ', 20, 111, 'bbb ', 0
union all select '2007-01-01 ', 'BBBB ', 100, 111, 'aaa ', 0
union all select '2007-01-01 ', 'AAAA ', 100, 222, 'aaa ', 1
create table B(入库时间 datetime, 品名 varchar(10), 数量 int, 人员代码 int, 单位 varchar(10), 状态 bit)
insert B select '2007-01-01 ', 'AAAA ', 100, 111, 'aaa ', 0
union all select '2007-01-01 ', 'AAAA ', 20, 111, 'bbb ', 0
union all select '2007-01-01 ', 'BBBB ', 100, 111, 'aaa ', 0
union all select '2007-01-01 ', 'AAAA ', 100, 222, 'aaa ', 1
create table C(人员代码 int, 姓名 varchar(10))
insert C select 111, '张三 '
union all select 222, '李四 '
select 'AAAA '品名, 'aaa '单位,(select 姓名 from c where c.人员代码=a.人员代码),sum(数量)
from(select * from a where 品名= 'AAAA ' and 单位= 'aaa ' and 状态= '0 '
union all select * from a where 品名= 'AAAA ' and 单位= 'aaa ' and 状态= '0 ')a
group by 人员代码
[解决办法]
select *,
((select count(*) from 表1 where 品名= 'AAAA ' and 单位= 'aaa 'and 状态= '0 'and 表1.人员代码=表3.人员代码) +(select count(*) from 表1 where 品名= 'AAAA ' and 单位= 'aaa 'and 状态= '0 'and 表2.人员代码=表3.人员代码)) as 数量
from 表3