Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在表格中找到多行的完美匹配?_Sql_Sql Server_Tsql - Fatal编程技术网

Sql 如何在表格中找到多行的完美匹配?

Sql 如何在表格中找到多行的完美匹配?,sql,sql-server,tsql,Sql,Sql Server,Tsql,我正在寻找一种方法来找出#订单中的哪一行集合与#托盘最匹配 下面的示例使用@palletId=1000作为查询的输入,结果应该只能与'Order2'(100%匹配)和'Order4'(75%匹配)匹配。在这种情况下,我想要的结果是“Order2” Input@palletId=4000应与“Order4”100%匹配,且无其他匹配项 DECLARE @paletId bigint = 1000 CREATE TABLE #Pallets ([PalletId] bigint, [Item] n

我正在寻找一种方法来找出
#订单
中的哪一行集合与
#托盘
最匹配

下面的示例使用
@palletId=1000
作为查询的输入,结果应该只能与'Order2'(100%匹配)'Order4'(75%匹配)匹配。在这种情况下,我想要的结果是“Order2”

Input@palletId=4000应与“Order4”100%匹配,且无其他匹配项

DECLARE @paletId bigint = 1000

CREATE TABLE #Pallets ([PalletId] bigint, [Item] nvarchar(16), [Quantity] int)
CREATE TABLE #Orders ([OrderId] nvarchar(16), [Item] nvarchar(16), [Quantity] int)

INSERT INTO #Pallets ([PalletId], [Item], [Quantity]) VALUES 
(1000, 'item1', 10), 
(1000, 'item2', 10), 
(1000, 'item3', 10),
(4000, 'item1', 10), 
(4000, 'item2', 10), 
(4000, 'item3', 10),
(4000, 'item4', 10)

INSERT INTO #Orders ([OrderId], [Item], [Quantity]) VALUES 
('Order2', 'item1', 10), 
('Order2', 'item2', 10), 
('Order2', 'item3', 10),
('Order1', 'item1', 10),
('Order1', 'item2', 10),
('Order1', 'item3', 5),
('Order3', 'item2', 5),
('Order3', 'item3', 10),
('Order4', 'item1', 10), 
('Order4', 'item2', 10), 
('Order4', 'item3', 10),
('Order4', 'item4', 10),
('Order5', 'item1', 5), 
('Order5', 'item2', 5), 
('Order5', 'item3', 5),

DROP TABLE #ItemTable
DROP TABLE #LocationTable
DROP TABLE #BookingTable 
DROP TABLE #OrderTable
我一直试图以下面的例子为基础来解决这个问题,但没有得到我想要的结果


提前谢谢。

你可以试试像

declare @cnt int
select @cnt = count(1) 
from #Pallets
where PalletId = @paletId 


select  top 1 OrderId 
from #Orders o
join #Pallets p on 
    o.Item = p.Item and 
    o.Quantity = p.Quantity and 
    p.PalletId = @paletId 
group by OrderId
order by abs(@cnt - count(PalletId))
我只是利用这两者之间的差异来找出哪一个接近100%
abs(@cnt-count(PalletId))
只有在100%匹配时才会返回零

请尝试以下查询:

declare @palletId int = 1000;
select OrderId,
       count(p.Item) * 1.0 / count(*) matchLevel
from #Orders o
left join (
    select Item, Quantity
    from #Pallets
    where palletId = @palletId
) p on o.Item = p.Item and o.Quantity = p.Quantity
group by OrderId
返回:


然后用
top1
orderbymatchleveldesc

将其包装在查询中就足够了,以确保
Order2
包含
item1
item2
item3
,这意味着它与
托盘上的
item1/2/3
的两个实例都相关?您可能还想看一看类似的问题。@ZoharPeled,并且数量与正确的项目完全匹配。这就是问题的主要原因。因为order5在其他情况下也是匹配的,但数量仅为所需数量的50%。我必须做一些测试,看看是否存在无法正常工作的情况。但乍一看,这个答案是有潜力的。这也非常有效。但我接受了其他人的回答,因为这是第一次。