34,594
社区成员
发帖
与我相关
我的任务
分享
SELECT xmname, fullname, standard,
SUM(CASE newtype WHEN 1 THEN qty ELSE 0 END) AS 入数量,
SUM(CASE newtype WHEN 2 THEN qty ELSE 0 END) AS 出数量,
SUM(CASE newtype WHEN 3 THEN qty ELSE 0 END) AS 退货量,
SUM(CASE newtype WHEN 4 THEN qty ELSE 0 END) AS 返货量,
SUM(CASE newtype WHEN 2 THEN - qty ELSE qty END) AS 库存
FROM
(
SELECT xmname, fullname, standard,qty, newtype = 1 FROM tbS_warehouse_detailed
UNION ALL
SELECT xmname, fullname, standard,qty, newtype = 2 FROM tbS_sell_detailed
UNION ALL
SELECT xmname, fullname, standard,qty, newtype = 3 FROM tbS_rewarehouse_detailed
UNION ALL
SELECT xmname, fullname, standard,qty, newtype = 4 FROM tbS_resell_detailed
) m
GROUP BY xmname, fullname, standard
SELECT xmname, fullname, standard,
SUM(CASE newtype WHEN 1 THEN qty ELSE 0 END) AS 入数量,
SUM(CASE newtype WHEN 2 THEN qty ELSE 0 END) AS 出数量,
SUM(CASE newtype WHEN 3 THEN qty ELSE 0 END) AS 退货量,
SUM(CASE newtype WHEN 4 THEN qty ELSE 0 END) AS 返货量,
SUM(CASE newtype WHEN 2 THEN - qty ELSE qty END) AS 库存
FROM
(
SELECT *, newtype = 1 FROM tbS_warehouse_detailed
UNION ALL
SELECT *, newtype = 2 FROM tbS_sell_detailed
UNION ALL
SELECT *, newtype = 3 FROM tbS_rewarehouse_detailed
UNION ALL
SELECT *, newtype = 4 FROM tbS_resell_detailed
) m
GROUP BY xmname, fullname, standard
select 所属项目,产品名称,规格,
入数量=sum(case flag when 1 then 数量 else 0 end),
出数量=sum(case flag when 2 then 数量 else 0 end),
退货数=sum(case flag when 3 then 数量 else 0 end),
返货数=sum(case flag when 4 then 数量 else 0 end),
库存=sum(case when flag in(1,4) then 数量 else -数量 end)
from (
select *,flag=1 from a
union all
select *,flag=2 from b
union all
select *,flag=3 from c--退货
union all
select *,flag=4 from d--返货
) as t
group by 所属项目,产品名称,规格
如果那些项都要显示 ,这样select m.所属项目,m.产品名称,m.规格,
sum(case type when 1 then 数量 else 0 end) 入数量,
sum(case type when 2 then 数量 else 0 end) 出数量,
sum(case type when 3 then 数量 else 0 end) 退货量,
sum(case type when 4 then 数量 else 0 end) 返货量,
sum(case type when 2 then - 数量 else 数量 end) 库存
from
(
select * , type = 1 from 入库表
union all
select * , type = 2 from 出库表
union all
select * , type = 3 from 退货表
union all
select * , type = 4 from 返货表
) m
group by m.所属项目,m.产品名称,m.规格
select m.所属项目,m.产品名称,m.规格,
sum(case type when 1 then 数量 else 0 end) 入数量,
sum(case type when 2 then 数量 else 0 end) 出数量,
sum(case type when 3 then 数量 else 0 end) 退货量,
sum(case type when 4 then 数量 else 0 end) 返货量,
sum(case when type = 2 or type = 4 then - 数量 else 数量 end) 库存
from
(
select * , type = 1 from 入库表
union all
select * , type = 2 from 出库表
union all
select * , type = 3 from 退货表
union all
select * , type = 4 from 返货表
) m
group by m.所属项目,m.产品名称,m.规格
select 所属项目,产品名称,规格,
入数量=sum(case flag when 1 then 数量 else 0 end),
出数量=sum(case flag when 2 then 数量 else 0 end),
库存=sum(case flag when 1 then 数量 else -数量 end)
from (
select *,flag=1 from a
union all
select *,flag=2 from b
union all
select *,flag=2 from c--退货
union all
select *,flag=1 from d--返货
) as t
group by 所属项目,产品名称,规格
--假设入库,退货,返货对于库存都是+,只有出库是-.则如下:
select m.所属项目,m.产品名称,m.规格,
sum(case type when 1 then 数量 else 0 end) 入数量,
sum(case type when 2 then 数量 else 0 end) 出数量,
sum(case type when 3 then 数量 else 0 end) 退货量,
sum(case type when 4 then 数量 else 0 end) 返货量,
sum(case type when 2 then - 数量 else 数量 end) 库存
from
(
select * , type = 1 from 入库表
union all
select * , type = 2 from 出库表
union all
select * , type = 3 from 退货表
union all
select * , type = 4 from 返货表
) m
group by m.所属项目,m.产品名称,m.规格
select
a.所属项目,
a.产品名称,
a.规格,
a.数量 as 入数量,
isnull(b.数量,0) as 出数量,
isnull(c.数量,0) as 退数量,
a.数量-isnull(b.数量,0)+isnull(c.数量,0) as 库存
from
(
select 所属项目,产品名称,规格,sum(数量) as 数量 from a group by 所属项目,产品名称,规格
) a --入库表
left join
(
select 所属项目,产品名称,规格,sum(数量) as 数量 from b group by 所属项目,产品名称,规格
) b --出库表
on a.所属项目=b.所属项目 and a.产品名称=b.产品名称 and a.规格=b.规格
left join
(
select 所属项目,产品名称,规格,sum(数量) as 数量 from c group by 所属项目,产品名称,规格
) c --退货表
on a.所属项目=c.所属项目 and a.产品名称=c.产品名称 and a.规格=c.规格
select
a.所属项目,
a.产品名称,
a.规格,
a.数量 as 入数量,
isnull(b.数量,0) as 出数量,
a.数量-isnull(b.数量,0) as 库存
from
(
select 所属项目,产品名称,规格,sum(数量) as 数量 from a group by 所属项目,产品名称,规格
) a --入库表
left join
(
select 所属项目,产品名称,规格,sum(数量) as 数量 from b group by 所属项目,产品名称,规格
) b --出库表
on a.所属项目=b.所属项目 and a.产品名称=b.产品名称 and a.规格=b.规格
left join
(
select 所属项目,产品名称,规格,sum(数量) as 数量 from c group by 所属项目,产品名称,规格
) c --退货表
on a.所属项目=c.所属项目 and a.产品名称=c.产品名称 and a.规格=c.规格
select isnull(m.所属项目,n.所属项目) 所属项目,
isnull(m.产品名称,n.产品名称) 产品名称,
isnull(m.规格,n.规格) 规格,
isnull(m.入数量,0) 入数量,
isnull(n.出数量,0) 出数量,
isnull(m.入数量,0) - isnull(n.出数量,0) 库存
from 入库表 m full join 出库表 n
on m.所属项目 = n.所属项目 and m.产品名称 = n.产品名称 and m.规格 = n.规格
select isnull(m.所属项目,n.所属项目) 所属项目,
isnull(m.产品名称,n.产品名称) 产品名称,
isnull(m.规格,n.规格) 规格,
sum(isnull(m.入数量,0)) 入数量,
sum(isnull(n.出数量,0)) 出数量,
sum(isnull(m.入数量,0) - isnull(n.出数量,0)) 库存
from 入库表 m full join 出库表 n
on m.所属项目 = n.所属项目 and m.产品名称 = n.产品名称 and m.规格 = n.规格
group by m.所属项目,m.产品名称,m.规格
--或者
select m.所属项目,m.产品名称,m.规格,
sum(case type when 1 then 入数量 else 0 end) 入数量,
sum(case type when 2 then 出数量 else 0 end) 出数量,
sum(case type when 1 then 入数量 else - 出数量 end) 库存
from
(
select * , type = 1 from 入库表
union all
select * , type = 2 from 出库表
) m
group by m.所属项目,m.产品名称,m.规格
select isnull(m.所属项目,n.所属项目) 所属项目,
isnull(m.产品名称,n.产品名称) 产品名称,
isnull(m.规格,n.规格) 规格,
isnull(m.入数量,0) 入数量,
isnull(n.出数量,0) 出数量,
isnull(m.入数量,0) - isnull(n.出数量,0) 库存
from 入库表 m full join 出库表 n
on m.所属项目 = n.所属项目 and m.产品名称 = n.产品名称 and m.规格 = n.规格
--假设你的连接条件为上述三个,如果不是,自己更改之。