检查项目的尺寸是不是大于给定值
Posted
技术标签:
【中文标题】检查项目的尺寸是不是大于给定值【英文标题】:check that the dimensions of item are greater than given values检查项目的尺寸是否大于给定值 【发布时间】:2021-12-25 02:03:00 【问题描述】:我正在尝试编写一个 SQL 查询,其中将提取包含所有项目及其尺寸的数据,并且必须满足以下条件:长度 >=20 或宽度 >=14 或高度 >=10(所有项目不适合 20x14x10 的容器)。问题是高度可能大于长度(想象在空间中翻转项目)。我写了一个代码,但我认为它不会涵盖所有情况。下面是我试过的代码:
select
items.bin_id,
items.bin_type_name,
items.bin_usage_name,
items.isbn,
items.boo,
items.quantity,
ma.pkg_height,
ma.pkg_width,
ma.pkg_length,
ma.pkg_weight,
items.owner
from items
left join BOOKER.D_MP_PHYSICAL_ITEMS ma
on items.boo = ma.asin
where ((ma.pkg_length >= 20 AND ma.pkg_width >= 14 AND ma.pkg_height >= 10) OR
(ma.pkg_length >= 14 AND ma.pkg_width >= 10 AND ma.pkg_height >= 20) OR
(ma.pkg_length >= 10 AND ma.pkg_width >= 20 AND ma.pkg_height >= 14) OR
((ma.pkg_length >= 20 AND ma.pkg_width >= 10 AND ma.pkg_height >= 14) OR
(ma.pkg_length >= 14 AND ma.pkg_width >= 20 AND ma.pkg_height >= 10) OR
(ma.pkg_length >= 10 AND ma.pkg_width >= 14 AND ma.pkg_height >= 20))
我的另一个想法是从项目的尺寸中选择最大值并检查它是否大于 20,然后选择最小值并查看它是否大于 10 并检查最后一个数字是否大于 14(以防万一其中两个第一个值给出 FALSE),但我不能把它放在 SQL 中
【问题讨论】:
【参考方案1】:我想下面的逻辑会起作用(基本上我已经尝试为您在问题的后面部分中提到的逻辑编写查询):
with temp_cte as (
select
items.*, ma.*,
Select dim from
(SELECT dim, row_number() over (order by dim desc) as rn
FROM (VALUES (ma.pkg_length), (ma.pkg_height), (ma.pkg_width)) AS value(dim)) where rn = 1) as length,
Select dim from
(SELECT dim, row_number() over (order by dim desc) as rn
FROM (VALUES (ma.pkg_length), (ma.pkg_height), (ma.pkg_width)) AS value(dim)) where rn = 2) as height,
Select dim from
(SELECT dim, row_number() over (order by dim desc) as rn
FROM (VALUES (ma.pkg_length), (ma.pkg_height), (ma.pkg_width)) AS value(dim)) where rn = 3) as width
from items
left join BOOKER.D_MP_PHYSICAL_ITEMS ma
on items.boo = ma.asin
where
length >= 20 or height >= 14 or width >= 10
)
Select
bin_id,
bin_type_name,
bin_usage_name,
isbn,
boo,
quantity,
pkg_height,
pkg_width,
pkg_length,
pkg_weight,
owner
from temp_cte;
【讨论】:
以上是关于检查项目的尺寸是不是大于给定值的主要内容,如果未能解决你的问题,请参考以下文章