我可以声明 TOP 以从另一个 table 中提取值吗

Can i declare TOP to pull the value from another table

我希望从库存 table 中收集 TOP diff,其中 newcount <> 数量。 基本上,如果我的库存数量与应有的数量不同,我想搜索我的剩余库存以查找缺失零件的 TOP #。这会给我返回一个库存 ID,以确保我首先移除最旧的部件。 我可以有 900 个,所以我正在寻找一个 sql 命令,该命令 returns partsremainingfifo 的结果基于库存中的 diff 和 partnumber。

select 
    PartNumber, 
    Quantity, 
    NewCount, 
    diff
from Inventory
where NewCount <> Quantity

+------------+----------+----------+------+
| PartNumber | Quantity | NewCount | diff |
+------------+----------+----------+------+
|    2871451 |        1 |        0 |    1 |
|    4932615 |        6 |        1 |    5 |
+------------+----------+----------+------+


select top 1 
    id, PartNumber, 
    PartDescription, 
    Quantity, 
    TotalPrice,
    Brand, 
    Location, 
    Account
from PARTSREMAININGFIFO
where PartNumber = '2871451'


+------+------------+-------------------+----------+------------+---------+----------+----------+
|  id  | PartNumber |  PartDescription  | Quantity | TotalPrice |  Brand  | Location | Account  |
+------+------------+-------------------+----------+------------+---------+----------+----------+
| 9183 |    2871451 | AFM DEVICE GASKET |        1 |  19.815225 | CUMMINS | A1       | 6015-Z   |
+------+------------+-------------------+----------+------------+---------+----------+----------+




select top 5 
    id, 
    PartNumber, 
    PartDescription, 
    Quantity, 
    TotalPrice,
    Brand, 
    Location, 
     Account
from PARTSREMAININGFIFO
where PartNumber = '4932615'


+------+------------+-----------------+----------+------------+---------+----------+---------+
|  id  | PartNumber | PartDescription | Quantity | TotalPrice |  Brand  | Location | Account |
+------+------------+-----------------+----------+------------+---------+----------+---------+
| 3264 |    4932615 | GASKET          |        1 |   2.907144 | CUMMINS | A1       | 6015-Z  |
| 9780 |    4932615 | GASKET          |        1 |   5.053475 | CUMMINS | A1       | 6015-Z  |
| 9781 |    4932615 | GASKET          |        1 |   5.053475 | CUMMINS | A1       | 6015-Z  |
| 9782 |    4932615 | GASKET          |        1 |   5.053475 | CUMMINS | A1       | 6015-Z  |
| 9783 |    4932615 | GASKET          |        1 |   5.053475 | CUMMINS | A1       | 6015-Z  |
+------+------------+-----------------+----------+------------+---------+----------+---------+

我现在明白你想要什么了,我想你需要一个光标。重要说明,您 必须 在使用 TOP 时指定 order by 除非您不关心 什么 行背部。 Read this article.

以下是您的实现方式:

--create some sample data that you gave

declare @inventory table (  PartNumber int, 
                            Quantity int, 
                            NewCount int, 
                            diff int)

insert into @inventory
values
(2871451,1,0,1),
(4932615,6,1,5)

declare @PARTSREMAININGFIFO table ( id int, 
                                    PartNumber int, 
                                    PartDescription varchar(64), 
                                    Quantity int, 
                                    TotalPrice decimal (8,6), 
                                    brand varchar(64), 
                                    Location varchar(16), 
                                    Account varchar(64))

insert into @PARTSREMAININGFIFO
values

(9183,2871451,'AFM DEVICE GASKET',1,19.815225,'CUMMINS','A1','6015-Z'),
(9183,2871451,'AFM DEVICE GASKET',1,19.815225,'CUMMINS','A2','6015-Z'), --notice the extra (2nd) row here for part 2871451
(9183,2871451,'AFM DEVICE GASKET',1,19.815225,'CUMMINS','A3','6015-Z'), --notice the extra (3nd) row here for part 2871451

(3264,4932615,'GASKET',1,2.907144,'CUMMINS','A1','6015-Z'),
(9780,4932615,'GASKET',1,5.053475,'CUMMINS','A1','6015-Z'),
(9781,4932615,'GASKET',1,5.053475,'CUMMINS','A1','6015-Z'),
(9782,4932615,'GASKET',1,5.053475,'CUMMINS','A1','6015-Z'),
(9783,4932615,'GASKET',1,5.053475,'CUMMINS','A1','6015-Z'),
(9783,4932615,'GASKET',1,5.053475,'CUMMINS','A6','6015-Z')  --notice the 6th (extra) row here for part 4932615


--staging table for your results

declare @tempResults table (        id int, 
                                    PartNumber int, 
                                    PartDescription varchar(64), 
                                    Quantity int, 
                                    TotalPrice decimal (8,6), 
                                    brand varchar(64), 
                                    Location varchar(16), 
                                    Account varchar(64))



declare cur cursor local fast_forward for 
select distinct 
    PartNumber,
    diff 
from (select 
            PartNumber, 
            Quantity, 
            NewCount, 
            diff
        from @inventory
        where NewCount <> Quantity) x

declare @partNumber int
declare @diff int

open cur

fetch next from cur into @partNumber, @diff 

while @@FETCH_STATUS = 0
begin
    insert into @tempResults
    select top (@diff) 
        id, 
        PartNumber, 
        PartDescription, 
        Quantity, 
        TotalPrice,
        Brand, 
        Location, 
        Account
    from @PARTSREMAININGFIFO
    where PartNumber = @partNumber
    order by Quantity   --note you need to specify WHAT you want to order by 

    fetch next from cur into @partNumber, @diff 
end


select * 
from @tempResults
order by PartNumber

close cur
deallocate cur