这样的sql怎么写??
table1
客户.code 制品.code p_name
table2(入库表)
客户.code 制品.code number date
c-1 0004 232 2007-8-20
c-2 0006 4333 2007-8-23
c-1 0004 2320 2007-8-20
table3(出库表)
客户.code 制品.code number date
c-3 0004 32 2007-8-21
c-1 0004 24 2007-8-20
想要查询出
一段时间内,制品(每天)的入库数,和出库数明细
------解决方案--------------------select isnull(m.code , n.code) code, isnull(m.date,n.date) date,
isnull(m.number , 0) 入库数, isnull(n.number,0) 出库数 from
(
select convert(varchar(10),date,120) date , code , sum(number) number from t2 group by convert(varchar(10),date,120) , code
) m
full join
(
select convert(varchar(10),date,120) date , code , sum(number) number from t3 group by convert(varchar(10),date,120) , code
) n
on m.code = n.code and m.date = n.date
------解决方案--------------------create table t2(code varchar(10),number int,date datetime)
insert into t2 values( '0004 ', 232, '2007-8-20 ')
insert into t2 values( '0006 ', 4333, '2007-8-23 ')
insert into t2 values( '0004 ', 2320, '2007-8-20 ')
create table t3(code varchar(10),number int,date datetime)
insert into t3 values( '0004 ', 32, '2007-8-21 ')
insert into t3 values( '0004 ', 24, '2007-8-20 ')
go
select isnull(m.code , n.code) code, isnull(m.date,n.date) date,
isnull(m.number , 0) 入库数, isnull(n.number,0) 出库数 from
(
select convert(varchar(10),date,120) date , code , sum(number) number from t2 group by convert(varchar(10),date,120) , code
) m
full join
(
select convert(varchar(10),date,120) date , code , sum(number) number from t3 group by convert(varchar(10),date,120) , code
) n
on m.code = n.code and m.date = n.date
drop table t2,t3
/*
code date 入库数 出库数
---------- ---------- ----------- -----------
0004 2007-08-20 2552 24
0004 2007-08-21 0 32
0006 2007-08-23 4333 0
(所影响的行数为 3 行)
*/