求查询表里对应数量汇总合计相减的明细表
MFPOS 表有字段为:
OS_ID, CLS_ID, BIL_TYPE
SO F 01
SO F 01
SO F 02
SO F 01
SO F 02
.....
TFPOS表有字段为:
OS_ID, PRD_NO, QTY
SO CP0245 5000.00
SO CP0245 2000.00
SO CP0245 9000.00
SO CP0241 1000.00
SO CP0241 900.00
.....
我想求出两个表MFPOS.OS_ID=TFPOS.OS_ID= 'SO ' 并 MFPOS.CLS_ID= 'F '
且MFPOS.BIL_TYPE都等于 '02 '对应TFPOS.PRD_NO的TFPOS.QTY总合计减去
MFPOS.BIL_TYPE都等于 '01 '对应TFPOS.PRD_NO的TFPOS.QTY总合计大于0的数值;
另一条件:按TFPOS.PRD_NO可查询;
显示为:
PRD_NO, QTY1, QTY2, QTY3
CP0245 9000 7000 2000
......
注:QTY1为MFPOS.BIL_TYPE都等于 '02 '的PRD_NO总合计
QTY2为MFPOS.BIL_TYPE都等于 '01 '的PRD_NO总合计
QTY3=QTY1-QTY2
上面的CP0241这个货号因为对应 '02 '的合计减去 '01 '的合计少于零(900-1000=-100)所以没有显示出来
谢谢哪大虾帮忙!
------解决方案--------------------貌似有点问题~
------解决方案--------------------create table MFPOS(OS_ID varchar(10),OS_NO varchar(10),CLS_ID varchar(10),BIL_TYPE varchar(10))
insert into MFPOS select 'SO ', 'SO71080001 ', 'F ', '01 '
insert into MFPOS select 'SO ', 'SO71080002 ', 'F ', '01 '
insert into MFPOS select 'SO ', 'SO71080003 ', 'F ', '02 '
insert into MFPOS select 'SO ', 'SO71090001 ', 'F ', '01 '
insert into MFPOS select 'SO ', 'SO71090002 ', 'F ', '02 '
create table TFPOS(OS_ID varchar(10),OS_NO varchar(10),PRD_NO varchar(10),QTY numeric(8,2))
insert into TFPOS select 'SO ', 'SO71080001 ', 'CP0245 ',5000.00
insert into TFPOS select 'SO ', 'SO71080002 ', 'CP0245 ',2000.00
insert into TFPOS select 'SO ', 'SO71080003 ', 'CP0245 ',9000.00
insert into TFPOS select 'SO ', 'SO71090001 ', 'CP0241 ',1000.00
insert into TFPOS select 'SO ', 'SO71090002 ', 'CP0241 ',900.00
go
select
b.PRD_NO,
QTY1=sum(case a.BIL_TYPE when '02 ' then b.QTY else 0 end),
QTY2=sum(case a.BIL_TYPE when '01 ' then b.QTY else 0 end),
QTY3=sum(case a.BIL_TYPE when '02 ' then b.QTY else -b.QTY end)
from
MFPOS a,TFPOS b
where
a.OS_ID=b.OS_ID and a.OS_NO=b.OS_NO and a.CLS_ID= 'F '
group by
b.PRD_NO
having
sum(case a.BIL_TYPE when '02 ' then b.QTY else -b.QTY end)> =0
go
/*
PRD_NO QTY1 QTY2 QTY3
---------- ----------- ----------- -----------
CP0245 9000.00 7000.00 2000.00
*/
drop ta