select 产品名称,日期,收发类型, 入库数=case 收发类型 when '入库 ' then 数量 end, 出库数=case 收发类型 when '出库 ' then 数量 end, [结存]=(select sum(case 收发类型 when '入库 ' then 数量 else -数量 end) from test where 日期 <=a.日期) from (select * from test union all select 盘点日期, 产品名称, '期初结存 ', 盘点数量, from 盘点表 ) a
select a.出入库日期,a.产品名称,b.入库数量,b.出库数量,(b.入库数量+b.出库数量+c.盘点数量)as 结存数量 from A a left join ( select 产品名称 ,sum(case when 收发类型= '入库 ' then 数量 else 0 end)as 入库数量, sum(case when 收发类型= '出库 ' then -数量 else 0 end)as 出库数量 from A --出入明细表 group by 产品名称 )B b on a.产品名称=b.产品名称 left join ( select 产品名称 , isnull(盘点数量,0)as 盘点数量 from B --盘点表 )c on a.产品名称=c.产品名称
select 产品名称,日期,收发类型, 入库数=case 收发类型 when '入库 ' then 数量 end, 出库数=case 收发类型 when '出库 ' then 数量 end, [结存]=(select sum(case 收发类型 when '入库 ' or '期初结存 ' then 数量+ else -数量 end) from test where 日期 <=a.日期) from (select * from test union all select 盘点日期, 产品名称, '期初结存 ', 盘点数量, from 盘点表 ) a
select 产品名称,日期,收发类型, 入库数=case 收发类型 when '入库 ' then 数量 end, 出库数=case 收发类型 when '出库 ' then 数量 end, [结存]=(select sum(case 收发类型 when '入库 ' or '期初结存 ' then 数量+ else -数量 end) from test where 日期 <=a.日期) from (select * from test union all select 盘点日期, 产品名称, '期初结存 ', 盘点数量, from 盘点表 ) a 通不过,提示 or 错误
insert 盘点表 select '2007-4-28 ', 'AAAA ',2000 union all select '2007-5-29 ', 'bbbb ',3000
insert 明细表 select '2007-5-29 ', 'AAAA ', '入库 ',1000 union all select '2007-5-29 ', 'bbbb ', '入库 ',2000 union all select '2009-5-30 ', 'AAAA ', '出库 ',50
select 日期,产品名称,收发类型,入库数,出库数,结存数量 from ( select top 100 percent 日期=null,产品名称,收发类型= '期初结存 ',入库数=null,出库数=null,结存数量=盘点数量,bh=1 from 盘点表 union all select top 100 percent 出入库日期,产品名称,收发类型,入库数=case 收发类型 when '入库 ' then 数量 end, 出库数=case 收发类型 when '出库 ' then 数量 end, 结存数量=(select sum(case 收发类型 when '入库 ' then 数量 else -数量 end) from 明细表 where 出入库日期 <=a.出入库日期),bh=2 from 明细表 a order by 产品名称,bh )b order by 产品名称,b.bh
SELECT CheckDate,ProductID,ProductName,ProductSpec, '期初结存 ',SUM(PassQty) FROM tblCheckZCP WHERE CheckDate BETWEEN @QCStartDate AND @QCEndDate GROUP BY CheckDate,ProductID,ProductName,ProductSpec
UNION ALL SELECT RptDate,ProductID, ProductName, ProductSpec,ProjectClass,SUM(Qty) FROM tblProjectDayRpt WHERE ProjectClass = '入库单 ' AND RptDate BETWEEN @DyStartDate AND @DyEndDate GROUP BY RptDate,ProductID, ProductName, ProductSpec,ProjectClass
UNION ALL SELECT RptDate,ProductID, ProductName, ProductSpec, '出库单 ',SUM(Qty) FROM tblProjectDayRpt WHERE ProjectClass = '出库单 ' OR ProjectClass = '不良单 ' AND RptDate BETWEEN @DyStartDate AND @DyEndDate GROUP BY RptDate,ProductID, ProductName, ProductSpec 麻烦了...