从子查询SQL中获取多行

问题描述 投票:2回答:2

基本上我有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

但它抛出了这个错误

子查询返回的值超过1。当子查询遵循=,!=,<,<=,>,> =或子查询用作表达式时,不允许这样做

我希望:

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

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

我正在使用SQL Server 2014

多谢你们

sql-server sql-server-2014
2个回答
2
投票

试试这个:

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   

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  
© www.soinside.com 2019 - 2024. All rights reserved.