基本上,我有2个表,第一个表包含每个序列号的原材料数量(QT),第二个表包含在批生产中花费了多少原材料(Qt_Added)。像这样:
表1
+----------+------------+-----+
| Code_Raw | Serial_Raw | Qt |
+----------+------------+-----+
| 1 | 1 | 100 |
| 1 | 2 | 150 |
| 2 | 1 | 80 |
| 1 | 3 | 100 |
+----------+------------+-----+
表2
+------------+----------+------------+----------+--+
| Code_Batch | Code_Raw | Serial_Raw | Qt_Added | |
+------------+----------+------------+----------+--+
| 1 | 1 | 1 | 80 | |
| 2 | 1 | 1 | 10 | |
| 3 | 1 | 2 | 150 | |
| 4 | 1 | 3 | 80 | |
+------------+----------+------------+----------+--+
我试图查询一个特定的Code_Raw
,告诉我每个序列号还剩多少,但是只有在有一个serial_raw
时才能工作。
我的查询:
select *
from
(select
Serial_Raw,
(Select QT From Table_1 where Code_Raw = 1) - Sum(qt_added) as Total_Remaining
from
Table_2
where
Cod_Raw = 1
group by
Serial_Raw) e
where
Total_Remaining > 0
但是会引发此错误
子查询返回了多个值。当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做
我希望:
Serial_Raw Total_Remaining
-------------------------------
1 10
3 20
是否存在结构问题或其他方法?
我正在使用SQL Server 2014
谢谢大家
答案 0 :(得分:1)
尝试一下:
ng build --prod --aot --build-optimizer --vendor-chunk --source-map=false
答案 1 :(得分:1)
如果我没看错,那可能就是你所追求的
declare @tbl1 table (CodeRaw INT, Serial_Raw INT, Qty INT)
declare @tbl2 table (CodeBatch INT, CodeRaw INT, Serial_Raw INT, QtyAdded INT)
insert into @tbl1 values (1,1,100), (1,2,150), (2,1,80), (1,3,100)
insert into @tbl2 values (1,1,1,80), (2,1,1,10), (3,1,2,150), (4,1,3,80)
select t2.Serial_Raw,
t3.Qty - sum(t2.QtyAdded) as Total_Remaining
from @tbl2 t2
inner join ( select t1.Serial_Raw,
t1.CodeRaw,
sum(t1.Qty) as Qty
from @tbl1 t1
group by t1.Serial_Raw, t1.CodeRaw
) t3
on t2.Serial_Raw = t3.Serial_Raw
and t2.CodeRaw = t3.CodeRaw
group by t2.Serial_Raw, t3.Qty
因此,在t2
中,我们得到所有不同的Serial_Raw
值,并从第一张表中求和它们的QtyAdded
。
在t3中,我们从第二张表中获得所有Qty
值。
我们要做的就是将它们连接在一起并减去
此查询的结果是
Serial_Raw Total_Remaining
---------- ---------------
1 10
2 0
3 20