Sql server 从子查询SQL中获取多行

Sql server 从子查询SQL中获取多行,sql-server,sql-server-2014,Sql Server,Sql Server 2014,基本上我有两个表,第一个表是每个序列号的原材料数量(QT),第二个表是批次生产中消耗了多少原材料(添加了QT_)。像这样: 表1 +----------+------------+-----+ | Code_Raw | Serial_Raw | Qt | +----------+------------+-----+ | 1 | 1 | 100 | | 1 | 2 | 150 | | 2 | 1 |

基本上我有两个表,第一个表是每个序列号的原材料数量(QT),第二个表是批次生产中消耗了多少原材料(添加了QT_)。像这样:

表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\u Raw
,显示每个序列号还剩下多少,但只有在有单个
serial\u 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

谢谢大家

试试这个:

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
中,我们得到所有不同的
序列原始值,并将它们的
QtyAdded
从第一个表中求和。
在t3中,我们从第二个表中获得所有
数量
值。
我们所需要做的就是把它们连在一起,然后减去

此查询的结果是

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

我不懂你的数学。你能更好地解释一下这里发生了什么吗?你能详细说明一下你是如何从这两个表中得到预期结果的吗?当然,我为一个特定的“批次”原材料添加了所有花费的原材料。然后,我将与“批次”中初始数量的差异作为总剩余量。希望现在清楚什么是
消耗原材料
?批次中的
初始金额是多少?这些想法对你来说可能很清楚,但对我们来说却不清楚。我们不知道您的数据库和业务您是如何获得串行原始
1
10
的?我认为如果没有
WHERE
子句,这是一个更好的答案,但可能那只是我。稍微解释一下为什么这样做对OP有利会使它成为一个更好的答案,但是因为OP没有提供表设置脚本,所以还是+1。@EricBrandt我添加了一条注释来理解查询,谢谢你的建议。哦,我需要使用JOIN。谢谢你的hepl,你理解的对。再次感谢你的帮助和耐心