select a.所属项目,a.产品名称,a.a.规格, 入数量=a.数量,出数量=ISNULL(b.数量,0),库存=a.数量-ISNULL(b.数量,0) from a left join b on a.所属项目=b.所属项目 and a.产品名称=b.产品名称 and a.规格=b.规格
select 所属项目,产品名称,规格, 入数量=sum(case flag when 1 then 数量 else 0 end), 出数量=sum(case flag when 2 then 数量 else 0 end), 库存=sum(数量) from ( select *,flag=1 from a union all select *,flag=2 from b ) as t group by 所属项目,产品名称,规格
SELECT A.所属项目,A.产品名称,A.规格,A.数量 AS 入数量, B.数量 AS 出数量,A.数量-B.数量 AS 库存 FROM (SELECT 所属项目,产品名称,规格,SUM(数量) AS 数量 FROM 入库表 GROUP BY 所属项目,产品名称,规格) AS A JOIN (SELECT 所属项目,产品名称,规格,SUM(数量) AS 数量 FROM 出库表 GROUP BY 所属项目,产品名称,规格) ON A.所属项目=B.所属项目 AND A.产品名称 = B.产吕名称 AND A.规格 = B.规格;
应该是这样 select a.所属项目,a.产品名称,a.a.规格, 入数量=sum(a.数量),出数量=ISNULL(sum(b.数量),0),库存=sum(a.数量)-ISNULL(sum(b.数量),0) from a left join b on a.所属项目=b.所属项目 and a.产品名称=b.产品名称 and a.规格=b.规格 group by a.所属项目,a.产品名称,a.a.规格
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 ) as t group by 所属项目,产品名称,规格汗,太快了,失误,修正。
select 所属项目, 产品名称, 规格, 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.规格
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.规格) 规格, 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.规格 --假设你的连接条件为上述三个,如果不是,自己更改之。--如果存在多笔,则需要使用group by 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 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 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 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 所属项目,产品名称,规格, 入数量=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 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 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 所属项目,产品名称,规格如果那些项都要显示 ,这样
tbS_warehouse_detailed 入库 a tbS_rewarehouse_detailed 退货 c tbS_sell_detailed 出库 b tbS_resell_detailed 返货 d SELECT xmname, fullname, standard, SUM(CASE type WHEN 1 THEN qty ELSE 0 END) AS 入数量, SUM(CASE type WHEN 2 THEN qty ELSE 0 END) AS 出数量, SUM(CASE type WHEN 3 THEN qty ELSE 0 END) AS 退货量, SUM(CASE type WHEN 4 THEN qty ELSE 0 END) AS 返货量, SUM(CASE type WHEN 2 THEN - qty ELSE qty END) AS 库存 FROM (SELECT *, type = 1 FROM tbS_warehouse_detailed UNION ALL SELECT *, type = 2 FROM tbS_sell_detailed UNION ALL SELECT *, type = 3 FROM tbS_rewarehouse_detailed UNION ALL SELECT *, type = 4 FROM tbS_resell_detailed) m GROUP BY xmname, fullname, standard 这样写错误提示 多次为M指定了TYPE列 什么意思啊???
你的数据表可能本身存在type列,你换个名字再试试. 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
错误 odbc sql server drvier包含UNION运算符查询表达式中的所有查询都必须在选择列表中包含同样数目的表达式
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
楼上大哥 我要想在 SUM(CASE newtype WHEN 1 THEN qty ELSE 0 END) AS 入数量, 加个条件呢?比如要查询 2个时间段之间的入库 如何写?
表A: 入库表 表B: 出库表 A B 的字段相同 为: 所属项目 产品名称 规格 数量 时间 我要查询的结果为: 所属项目 产品名称 规格 入数量 出数量 库存 (入-出)select a.所属项目,distinct a.产品名称,a.规格,(select sum(isnull(a.数量,0)) from a where a.产品名称=tbla.产品名称) as 入库量,(select sum(isnull(b.数量,0)) from b where b.产品名称=tbla.产品名称)as 出库量,(入库量-出库量) as 库存 from a as tbla left join b as tblb on tbla.产品名称=tblb.产品名称
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
楼上大哥 我要想在 SUM(CASE newtype WHEN 1 THEN qty ELSE 0 END) AS 入数量, 加个条件呢?比如要查询 2个时间段之间的入库 如何写?
入数量=a.数量,出数量=ISNULL(b.数量,0),库存=a.数量-ISNULL(b.数量,0)
from a left join b on a.所属项目=b.所属项目
and a.产品名称=b.产品名称 and a.规格=b.规格
入数量=sum(case flag when 1 then 数量 else 0 end),
出数量=sum(case flag when 2 then 数量 else 0 end),
库存=sum(数量)
from (
select *,flag=1 from a
union all
select *,flag=2 from b
) as t
group by 所属项目,产品名称,规格
B.数量 AS 出数量,A.数量-B.数量 AS 库存
FROM (SELECT 所属项目,产品名称,规格,SUM(数量) AS 数量
FROM 入库表 GROUP BY 所属项目,产品名称,规格) AS A
JOIN (SELECT 所属项目,产品名称,规格,SUM(数量) AS 数量
FROM 出库表 GROUP BY 所属项目,产品名称,规格)
ON A.所属项目=B.所属项目 AND A.产品名称 = B.产吕名称
AND A.规格 = B.规格;
select a.所属项目,a.产品名称,a.a.规格,
入数量=sum(a.数量),出数量=ISNULL(sum(b.数量),0),库存=sum(a.数量)-ISNULL(sum(b.数量),0)
from a left join b on a.所属项目=b.所属项目
and a.产品名称=b.产品名称 and a.规格=b.规格
group by a.所属项目,a.产品名称,a.a.规格
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
) as t
group by 所属项目,产品名称,规格汗,太快了,失误,修正。
所属项目,
产品名称,
规格,
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.规格
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.规格--假设你的连接条件为上述三个,如果不是,自己更改之。
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.规格
--假设你的连接条件为上述三个,如果不是,自己更改之。--如果存在多笔,则需要使用group by
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
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.规格
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 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.规格
入数量=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 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.规格
入数量=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 所属项目,产品名称,规格如果那些项都要显示 ,这样
tbS_rewarehouse_detailed 退货 c tbS_sell_detailed 出库 b
tbS_resell_detailed 返货 d
SELECT xmname, fullname, standard, SUM(CASE type WHEN 1 THEN qty ELSE 0 END)
AS 入数量, SUM(CASE type WHEN 2 THEN qty ELSE 0 END) AS 出数量,
SUM(CASE type WHEN 3 THEN qty ELSE 0 END) AS 退货量,
SUM(CASE type WHEN 4 THEN qty ELSE 0 END) AS 返货量,
SUM(CASE type WHEN 2 THEN - qty ELSE qty END) AS 库存
FROM (SELECT *, type = 1
FROM tbS_warehouse_detailed
UNION ALL
SELECT *, type = 2
FROM tbS_sell_detailed
UNION ALL
SELECT *, type = 3
FROM tbS_rewarehouse_detailed
UNION ALL
SELECT *, type = 4
FROM tbS_resell_detailed) m
GROUP BY xmname, fullname, standard
这样写错误提示 多次为M指定了TYPE列 什么意思啊???
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
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
表B: 出库表
A B 的字段相同 为:
所属项目
产品名称
规格
数量
时间
我要查询的结果为:
所属项目 产品名称 规格 入数量 出数量 库存 (入-出)select a.所属项目,distinct a.产品名称,a.规格,(select sum(isnull(a.数量,0)) from a where a.产品名称=tbla.产品名称) as 入库量,(select sum(isnull(b.数量,0)) from b where b.产品名称=tbla.产品名称)as 出库量,(入库量-出库量) as 库存
from a as tbla left join b as tblb on tbla.产品名称=tblb.产品名称
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
楼上大哥 我要想在 SUM(CASE newtype WHEN 1 THEN qty ELSE 0 END) AS 入数量, 加个条件呢?比如要查询 2个时间段之间的入库 如何写?