从子查询中获取多行 SQL

Get multiple rows from a subquery SQL

基本上我有 2 Tables,第一个是每个序列号的原始 material 金额 (QT),第二个是原始 material 花费了多少( Qt_Added) 批量生产。像这样:

Table 1

+----------+------------+-----+
| Code_Raw | Serial_Raw | Qt  |
+----------+------------+-----+
|        1 |          1 | 100 |
|        1 |          2 | 150 |
|        2 |          1 |  80 |
|        1 |          3 | 100 |
+----------+------------+-----+

和Table 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

但是它抛出这个错误

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression

我预计:

Serial_Raw     Total_Remaining
-------------------------------
    1                10
    3                20

是否存在结构问题或其他方法?

我正在使用 SQL Server 2014

谢谢大家

试试这个:

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)
    INSERT INTO @tbl1 VALUES(1,2,150)
    INSERT INTO @tbl1 VALUES(2,1,80)
    INSERT INTO @tbl1 VALUES(1,3,100)

    INSERT INTO @tbl2 VALUES(1,1,1,80)
    INSERT INTO @tbl2 VALUES(2,1,1,10)
    INSERT INTO @tbl2 VALUES(3,1,2,150)
    INSERT INTO @tbl2 VALUES(4,1,3,80)

    --Inner table has the summary of the Quantity added with columns CodeRaw and SerialRaw. Outer table make join with inner table and just substruct with the Qty and Sum of Qty Added.  
    SELECT t2.Serial_Raw, t1.Qty - t2.QtyAdded AS Total_Remaining  FROM @tbl1 t1
        INNER JOIN (SELECT CodeRaw, Serial_Raw , SUM(QtyAdded) QtyAdded FROM @tbl2
                    GROUP BY CodeRaw, Serial_Raw) AS t2 ON t2.CodeRaw = t1.CodeRaw AND t1.Serial_Raw = t2.Serial_Raw
    WHERE t1.Qty - t2.QtyAdded > 0   

如果我没理解错的话,这可能就是你想要的

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 值,并从第一个 table.
中求和它们的 QtyAdded 在 t3 中,我们从第二个 table.
中获取所有 Qty 值 我们需要做的就是将它们连接在一起并减去

本次查询的结果是

Serial_Raw  Total_Remaining 
----------  --------------- 
1           10  
2            0  
3           20