OpenJSON未检测到重复的值
我需要将一些销售记录导入和解析到数据库中。
一些客户有多个产品,因此有几个订单具有不同的产品,但是 json
array
中的运输地址相同。
当我尝试将地址保存在地址
表:
中时会出现问题
显然,我只需要每个地址的一个记录,并且要实现此目标,我计算了地址字段的 hash
,我将其与表中已经存在的哈希进行了比较:
看来(嗯,我当然)查询仅第一次检查 hash
已经存在,如果不存在,它将添加与订单计数
的行一样多的行:我可以想象这是 openjson
功能,因为 JSON的解析
有效负载似乎是由内部循环进行的,因此我认为我必须使用其他方法。...但是我没有想法使用什么。
在这里, JSON
有效负载
declare @json nvarchar(max)=N'[
{
"id": 21660,
"currency": "USD",
"total": "15.00",
"shipping": {
"first_name": "Charles",
"last_name": "Leuschke",
"address_1": "3121 W Olive Ave",
"city": "Burbank",
"state": "CA",
"postcode": "91505",
"country": "US"
},
"line_items": [
{
"id": 1052
}
]
},
{
"id": 21659,
"currency": "USD",
"total": "38.00",
"shipping": {
"first_name": "Charles",
"last_name": "Leuschke",
"address_1": "3121 W Olive Ave",
"city": "Burbank",
"state": "CA",
"postcode": "91505",
"country": "US"
},
"line_items": [
{
"id": 1050
}
]
},
{
"id": 21658,
"currency": "USD",
"total": "38.00",
"shipping": {
"first_name": "Charles",
"last_name": "Leuschke",
"address_1": "3121 W Olive Ave",
"city": "Burbank",
"state": "CA",
"postcode": "91505",
"country": "US"
},
"line_items": [
{
"id": 1048
}
]
}
]'
和(简化) QUERY
Insert Into @Addresses
(
orderId,
fullName,
addressLine1,
city,
stateOrProvince,
postalCode,
countryCode,
addressCode
)
SELECT
o.orderId,
concat(s.firstName,' ',s.lastName),
s.addressLine1,
s.city,
s.stateOrProvince,
s.postalCode,
s.countryCode,
convert(nvarchar(64),hashbytes('SHA1',concat(s.firstName, ' ', s.lastName, s.addressLine1, s.city, s.stateOrProvince, s.postalCode, s.countryCode)),2)
FROM OPENJSON(@json)
WITH (
orderId nvarchar(64) '$.id',
shipping nvarchar(max) '$.shipping' AS JSON
) o
CROSS APPLY OPENJSON(shipping)
WITH (
firstName nvarchar(128) '$.first_name',
lastName nvarchar(128) '$.last_name',
addressLine1 nvarchar(128) '$.address_1',
city nvarchar(128) '$.city',
stateOrProvince nvarchar(64) '$.state',
postalCode nvarchar(64) '$.postcode',
countryCode nvarchar(4) '$.country'
) s
left join @Addresses a on a.addressCode=convert(nvarchar(64),hashbytes('SHA1',concat(s.firstName,' ', s.lastName, s.addressLine1, s.city, s.stateOrProvince, s.postalCode, s.countryCode)),2)
where a.addressCode is null
我还准备了一个SQLFIDDLE,您可以看到它可以返回3行,而目标只需获得一个
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用标准
row_number()
重复数据删除方法,例如注意:如果使用
交叉应用
来计算哈希码,则避免多次计算它。Just use the standard
ROW_NUMBER()
deduplication method e.g.Note: If you use
CROSS APPLY
to calculate the hashcode one avoids calculating it multiple times.