--统计每天的累计生产完成情况
DECLARE @dateS DATETIME;
SET @dateS = '2021-06-01';
SELECT a.客户 ,
a.产品编码 ,
a.规格 ,
a.订单数量 ,
a.下单日期 ,
( SELECT SUM(t1.订单数量)
FROM ( SELECT a.F15 AS 产品编码 ,
a.F20 AS 订单数量 ,
b.F3 AS 下单日期 ,
b.F5 AS 客户
FROM JBL.dbo.tabDIYTable3 a
INNER JOIN JBL.dbo.tabDIYTable2 b ON b.ID = a.ID
) AS t1
WHERE t1.产品编码 = a.产品编码
AND t1.下单日期 >= a.下单日期
) AS '累计订单量' ,
( SELECT SUM(t1.生产数量)
FROM ( SELECT a.F2768 AS 产品编码 ,
CAST(a.F2716 AS INT) AS 生产数量 ,
a.F2710 AS 机台 ,
b.F2703 AS 生产日期 ,
b.F2704 AS 生产车间
FROM JBL.dbo.tabDIYTable252 a
INNER JOIN JBL.dbo.tabDIYTable251 b ON b.ID = a.ID
) AS t1
WHERE t1.产品编码 = a.产品编码
AND t1.生产日期 >= a.开始生产日期
AND t1.生产日期 <= @dateS
) AS '总累计产量' ,
( SELECT SUM(t1.生产数量)
FROM ( SELECT a.F2768 AS 产品编码 ,
CAST(a.F2716 AS INT) AS 生产数量 ,
a.F2710 AS 机台 ,
b.F2703 AS 生产日期 ,
b.F2704 AS 生产车间
FROM JBL.dbo.tabDIYTable252 a
INNER JOIN JBL.dbo.tabDIYTable251 b ON b.ID = a.ID
) AS t1
WHERE t1.产品编码 = a.产品编码
AND t1.生产日期 >= a.开始生产日期
AND t1.生产日期 < @dateS --去掉等号表示今天以前
) AS '昨日累计产量' ,
( SELECT SUM(t1.生产数量)
FROM ( SELECT a.F2768 AS 产品编码 ,
CAST(a.F2716 AS INT) AS 生产数量 ,
a.F2710 AS 机台 ,
b.F2703 AS 生产日期 ,
b.F2704 AS 生产车间
FROM JBL.dbo.tabDIYTable252 a
INNER JOIN JBL.dbo.tabDIYTable251 b ON b.ID = a.ID
) AS t1
WHERE t1.产品编码 = a.产品编码
AND t1.生产日期 = @dateS
) AS '当日总产量' ,
( SELECT SUM(t1.生产数量)
FROM ( SELECT a.F2768 AS 产品编码 ,
CAST(a.F2716 AS INT) AS 生产数量 ,
a.F2710 AS 机台 ,
b.F2703 AS 生产日期 ,
b.F2704 AS 生产车间
FROM JBL.dbo.tabDIYTable252 a
INNER JOIN JBL.dbo.tabDIYTable251 b ON b.ID = a.ID
) AS t1
WHERE t1.产品编码 = a.产品编码
AND t1.生产日期 = @dateS
AND t1.生产车间 = '自动车A班'
) AS 'A班当日产量' ,
( SELECT SUM(t1.生产数量)
FROM ( SELECT a.F2768 AS 产品编码 ,
CAST(a.F2716 AS INT) AS 生产数量 ,
a.F2710 AS 机台 ,
b.F2703 AS 生产日期 ,
b.F2704 AS 生产车间
FROM JBL.dbo.tabDIYTable252 a
INNER JOIN JBL.dbo.tabDIYTable251 b ON b.ID = a.ID
) AS t1
WHERE t1.产品编码 = a.产品编码
AND t1.生产日期 = @dateS
AND t1.生产车间 = '自动车B班'
) AS 'B班当日产量' ,
( SELECT SUM(t1.生产数量)
FROM ( SELECT a.F2768 AS 产品编码 ,
CAST(a.F2716 AS INT) AS 生产数量 ,
a.F2710 AS 机台 ,
b.F2703 AS 生产日期 ,
b.F2704 AS 生产车间
FROM JBL.dbo.tabDIYTable252 a
INNER JOIN JBL.dbo.tabDIYTable251 b ON b.ID = a.ID
) AS t1
WHERE t1.产品编码 = a.产品编码
AND t1.生产日期 = @dateS
AND t1.生产车间 = '自动车C班'
) AS 'C班当日产量' ,
( SELECT SUM(t1.生产数量)
FROM ( SELECT a.F2768 AS 产品编码 ,
CAST(a.F2716 AS INT) AS 生产数量 ,
a.F2710 AS 机台 ,
b.F2703 AS 生产日期 ,
b.F2704 AS 生产车间
FROM JBL.dbo.tabDIYTable252 a
INNER JOIN JBL.dbo.tabDIYTable251 b ON b.ID = a.ID
) AS t1
WHERE t1.产品编码 = a.产品编码
AND t1.生产日期 = @dateS
AND t1.生产车间 = '自动车D班'
) AS 'D班当日产量'
FROM dbo.订单计划简表 a