可以简化 SQL 吗?
Possible to simplify SQL?
此查询的目的是计算 "same" 类型商品的四种不同变体的每个唯一 ID (LUID_NO)。
它按预期工作。
现在我的问题是,这个查询是否可以简化或以其他方式编写?
当我看到它时,我无法摆脱这样一种感觉,即可能可以用另一种更简单的方式来编写它。
SELECT
(SELECT COUNT(DISTINCT ICL.LUID_NO)
FROM L2000.ITEM_CARRIER IC
INNER JOIN L2000.ITEM_UNITS_DEFINITION IUD ON IUD.PV_ID=IC.PV_ID AND IUD.STORER_ID=IC.STORER_ID AND IUD.ITEM_ID=IC.ITEM_ID
INNER JOIN L2000.ITEM_CARRIER_LUID ICL ON ICL.ITEM_CARRIER_NO=IC.ITEM_CARRIER_NO
INNER JOIN L2000.ITEM_PROPERTY IP ON IC.PROPERTY_INDEX=IP.PROPERTY_INDEX
INNER JOIN L2000.ITEM I ON I.ITEM_ID = IC.ITEM_ID AND I.STORER_ID = IC.STORER_ID
WHERE IC.PV_ID = 'B2'
AND IP.BLOCK_CODE IS NULL
AND IUD.UOM_QTY = ICL.PHYSIC_QTY
AND IUD.UNIT_DEF_ID = 'LU'
AND IC.STORER_ID = 'PG'
AND ICL.PHYSIC_QTY > 0
AND I.ITEM_DESC LIKE '#%'
AND I.ITEM_GROUP_ID IN ('BABYCARE','FEMCARE')
)AS B2_PALLETS,
(SELECT COUNT(DISTINCT ICL.LUID_NO)
FROM L2000.ITEM_CARRIER IC
INNER JOIN L2000.ITEM_UNITS_DEFINITION IUD ON IUD.PV_ID=IC.PV_ID AND IUD.STORER_ID=IC.STORER_ID AND IUD.ITEM_ID=IC.ITEM_ID
INNER JOIN L2000.ITEM_CARRIER_LUID ICL ON ICL.ITEM_CARRIER_NO=IC.ITEM_CARRIER_NO
INNER JOIN L2000.ITEM_PROPERTY IP ON IC.PROPERTY_INDEX=IP.PROPERTY_INDEX
INNER JOIN L2000.ITEM I ON I.ITEM_ID = IC.ITEM_ID AND I.STORER_ID = IC.STORER_ID
WHERE IC.PV_ID = 'B3'
AND IP.BLOCK_CODE IS NULL
AND IUD.UOM_QTY = ICL.PHYSIC_QTY
AND IUD.UNIT_DEF_ID = 'LU'
AND IC.STORER_ID = 'PG'
AND ICL.PHYSIC_QTY > 0
AND I.ITEM_DESC LIKE '#%'
AND I.ITEM_GROUP_ID IN ('BABYCARE','FEMCARE')
)AS B3_PALLETS,
(SELECT COUNT(DISTINCT ICL.LUID_NO)
FROM L2000.ITEM_CARRIER IC
INNER JOIN L2000.ITEM_UNITS_DEFINITION IUD ON IUD.PV_ID=IC.PV_ID AND IUD.STORER_ID=IC.STORER_ID AND IUD.ITEM_ID=IC.ITEM_ID
INNER JOIN L2000.ITEM_CARRIER_LUID ICL ON ICL.ITEM_CARRIER_NO=IC.ITEM_CARRIER_NO
INNER JOIN L2000.ITEM_PROPERTY IP ON IC.PROPERTY_INDEX=IP.PROPERTY_INDEX
INNER JOIN L2000.ITEM I ON I.ITEM_ID = IC.ITEM_ID AND I.STORER_ID = IC.STORER_ID
WHERE IC.PV_ID = 'B2'
AND IP.BLOCK_CODE IS NOT NULL
AND IUD.UNIT_DEF_ID = 'LU'
AND IC.STORER_ID = 'PG'
AND ICL.PHYSIC_QTY > 0
AND I.ITEM_DESC LIKE '#%'
AND I.ITEM_GROUP_ID IN ('BABYCARE','FEMCARE')
)AS BLOCKED_B2_PALLETS,
(SELECT COUNT(DISTINCT ICL.LUID_NO)
FROM L2000.ITEM_CARRIER IC
INNER JOIN L2000.ITEM_UNITS_DEFINITION IUD ON IUD.PV_ID=IC.PV_ID AND IUD.STORER_ID=IC.STORER_ID AND IUD.ITEM_ID=IC.ITEM_ID
INNER JOIN L2000.ITEM_CARRIER_LUID ICL ON ICL.ITEM_CARRIER_NO=IC.ITEM_CARRIER_NO
INNER JOIN L2000.ITEM_PROPERTY IP ON IC.PROPERTY_INDEX=IP.PROPERTY_INDEX
INNER JOIN L2000.ITEM I ON I.ITEM_ID = IC.ITEM_ID AND I.STORER_ID = IC.STORER_ID
WHERE IC.PV_ID = 'B3'
AND IP.BLOCK_CODE IS NOT NULL
AND IUD.UNIT_DEF_ID = 'LU'
AND IC.STORER_ID = 'PG'
AND ICL.PHYSIC_QTY > 0
AND I.ITEM_DESC LIKE '#%'
AND I.ITEM_GROUP_ID IN ('BABYCARE','FEMCARE')
)AS BLOCKED_B3_PALLETS
FROM L2000.ITEM_CARRIER
WHERE ROWNUM = 1
使用条件聚合
SELECT COUNT(DISTINCT CASE WHEN IC.PV_ID = 'B2'
AND IP.BLOCK_CODE IS NULL THEN ICL.LUID_NO END) AS B2_PALLETS,
COUNT(DISTINCT CASE WHEN IC.PV_ID = 'B3'
AND IP.BLOCK_CODE IS NULL THEN ICL.LUID_NO END) AS B3_PALLETS,
COUNT(DISTINCT CASE WHEN IC.PV_ID = 'B2'
AND IP.BLOCK_CODE IS NOT NULL THEN ICL.LUID_NO END) AS BLOCKED_B2_PALLETS,
COUNT(DISTINCT CASE WHEN IC.PV_ID = 'B3'
AND IP.BLOCK_CODE IS NOT NULL THEN ICL.LUID_NO END) AS BLOCKED_B3_PALLETS
FROM L2000.ITEM_CARRIER IC
INNER JOIN L2000.ITEM_UNITS_DEFINITION IUD ON IUD.PV_ID=IC.PV_ID AND IUD.STORER_ID=IC.STORER_ID AND IUD.ITEM_ID=IC.ITEM_ID
INNER JOIN L2000.ITEM_CARRIER_LUID ICL ON ICL.ITEM_CARRIER_NO=IC.ITEM_CARRIER_NO
INNER JOIN L2000.ITEM_PROPERTY IP ON IC.PROPERTY_INDEX=IP.PROPERTY_INDEX
INNER JOIN L2000.ITEM I ON I.ITEM_ID = IC.ITEM_ID AND I.STORER_ID = IC.STORER_ID
WHERE IUD.UOM_QTY = ICL.PHYSIC_QTY
AND IUD.UNIT_DEF_ID = 'LU'
AND IC.STORER_ID = 'PG'
AND ICL.PHYSIC_QTY > 0
AND I.ITEM_DESC LIKE '#%'
AND I.ITEM_GROUP_ID IN ('BABYCARE','FEMCARE')
此查询的目的是计算 "same" 类型商品的四种不同变体的每个唯一 ID (LUID_NO)。
它按预期工作。
现在我的问题是,这个查询是否可以简化或以其他方式编写?
当我看到它时,我无法摆脱这样一种感觉,即可能可以用另一种更简单的方式来编写它。
SELECT
(SELECT COUNT(DISTINCT ICL.LUID_NO)
FROM L2000.ITEM_CARRIER IC
INNER JOIN L2000.ITEM_UNITS_DEFINITION IUD ON IUD.PV_ID=IC.PV_ID AND IUD.STORER_ID=IC.STORER_ID AND IUD.ITEM_ID=IC.ITEM_ID
INNER JOIN L2000.ITEM_CARRIER_LUID ICL ON ICL.ITEM_CARRIER_NO=IC.ITEM_CARRIER_NO
INNER JOIN L2000.ITEM_PROPERTY IP ON IC.PROPERTY_INDEX=IP.PROPERTY_INDEX
INNER JOIN L2000.ITEM I ON I.ITEM_ID = IC.ITEM_ID AND I.STORER_ID = IC.STORER_ID
WHERE IC.PV_ID = 'B2'
AND IP.BLOCK_CODE IS NULL
AND IUD.UOM_QTY = ICL.PHYSIC_QTY
AND IUD.UNIT_DEF_ID = 'LU'
AND IC.STORER_ID = 'PG'
AND ICL.PHYSIC_QTY > 0
AND I.ITEM_DESC LIKE '#%'
AND I.ITEM_GROUP_ID IN ('BABYCARE','FEMCARE')
)AS B2_PALLETS,
(SELECT COUNT(DISTINCT ICL.LUID_NO)
FROM L2000.ITEM_CARRIER IC
INNER JOIN L2000.ITEM_UNITS_DEFINITION IUD ON IUD.PV_ID=IC.PV_ID AND IUD.STORER_ID=IC.STORER_ID AND IUD.ITEM_ID=IC.ITEM_ID
INNER JOIN L2000.ITEM_CARRIER_LUID ICL ON ICL.ITEM_CARRIER_NO=IC.ITEM_CARRIER_NO
INNER JOIN L2000.ITEM_PROPERTY IP ON IC.PROPERTY_INDEX=IP.PROPERTY_INDEX
INNER JOIN L2000.ITEM I ON I.ITEM_ID = IC.ITEM_ID AND I.STORER_ID = IC.STORER_ID
WHERE IC.PV_ID = 'B3'
AND IP.BLOCK_CODE IS NULL
AND IUD.UOM_QTY = ICL.PHYSIC_QTY
AND IUD.UNIT_DEF_ID = 'LU'
AND IC.STORER_ID = 'PG'
AND ICL.PHYSIC_QTY > 0
AND I.ITEM_DESC LIKE '#%'
AND I.ITEM_GROUP_ID IN ('BABYCARE','FEMCARE')
)AS B3_PALLETS,
(SELECT COUNT(DISTINCT ICL.LUID_NO)
FROM L2000.ITEM_CARRIER IC
INNER JOIN L2000.ITEM_UNITS_DEFINITION IUD ON IUD.PV_ID=IC.PV_ID AND IUD.STORER_ID=IC.STORER_ID AND IUD.ITEM_ID=IC.ITEM_ID
INNER JOIN L2000.ITEM_CARRIER_LUID ICL ON ICL.ITEM_CARRIER_NO=IC.ITEM_CARRIER_NO
INNER JOIN L2000.ITEM_PROPERTY IP ON IC.PROPERTY_INDEX=IP.PROPERTY_INDEX
INNER JOIN L2000.ITEM I ON I.ITEM_ID = IC.ITEM_ID AND I.STORER_ID = IC.STORER_ID
WHERE IC.PV_ID = 'B2'
AND IP.BLOCK_CODE IS NOT NULL
AND IUD.UNIT_DEF_ID = 'LU'
AND IC.STORER_ID = 'PG'
AND ICL.PHYSIC_QTY > 0
AND I.ITEM_DESC LIKE '#%'
AND I.ITEM_GROUP_ID IN ('BABYCARE','FEMCARE')
)AS BLOCKED_B2_PALLETS,
(SELECT COUNT(DISTINCT ICL.LUID_NO)
FROM L2000.ITEM_CARRIER IC
INNER JOIN L2000.ITEM_UNITS_DEFINITION IUD ON IUD.PV_ID=IC.PV_ID AND IUD.STORER_ID=IC.STORER_ID AND IUD.ITEM_ID=IC.ITEM_ID
INNER JOIN L2000.ITEM_CARRIER_LUID ICL ON ICL.ITEM_CARRIER_NO=IC.ITEM_CARRIER_NO
INNER JOIN L2000.ITEM_PROPERTY IP ON IC.PROPERTY_INDEX=IP.PROPERTY_INDEX
INNER JOIN L2000.ITEM I ON I.ITEM_ID = IC.ITEM_ID AND I.STORER_ID = IC.STORER_ID
WHERE IC.PV_ID = 'B3'
AND IP.BLOCK_CODE IS NOT NULL
AND IUD.UNIT_DEF_ID = 'LU'
AND IC.STORER_ID = 'PG'
AND ICL.PHYSIC_QTY > 0
AND I.ITEM_DESC LIKE '#%'
AND I.ITEM_GROUP_ID IN ('BABYCARE','FEMCARE')
)AS BLOCKED_B3_PALLETS
FROM L2000.ITEM_CARRIER
WHERE ROWNUM = 1
使用条件聚合
SELECT COUNT(DISTINCT CASE WHEN IC.PV_ID = 'B2'
AND IP.BLOCK_CODE IS NULL THEN ICL.LUID_NO END) AS B2_PALLETS,
COUNT(DISTINCT CASE WHEN IC.PV_ID = 'B3'
AND IP.BLOCK_CODE IS NULL THEN ICL.LUID_NO END) AS B3_PALLETS,
COUNT(DISTINCT CASE WHEN IC.PV_ID = 'B2'
AND IP.BLOCK_CODE IS NOT NULL THEN ICL.LUID_NO END) AS BLOCKED_B2_PALLETS,
COUNT(DISTINCT CASE WHEN IC.PV_ID = 'B3'
AND IP.BLOCK_CODE IS NOT NULL THEN ICL.LUID_NO END) AS BLOCKED_B3_PALLETS
FROM L2000.ITEM_CARRIER IC
INNER JOIN L2000.ITEM_UNITS_DEFINITION IUD ON IUD.PV_ID=IC.PV_ID AND IUD.STORER_ID=IC.STORER_ID AND IUD.ITEM_ID=IC.ITEM_ID
INNER JOIN L2000.ITEM_CARRIER_LUID ICL ON ICL.ITEM_CARRIER_NO=IC.ITEM_CARRIER_NO
INNER JOIN L2000.ITEM_PROPERTY IP ON IC.PROPERTY_INDEX=IP.PROPERTY_INDEX
INNER JOIN L2000.ITEM I ON I.ITEM_ID = IC.ITEM_ID AND I.STORER_ID = IC.STORER_ID
WHERE IUD.UOM_QTY = ICL.PHYSIC_QTY
AND IUD.UNIT_DEF_ID = 'LU'
AND IC.STORER_ID = 'PG'
AND ICL.PHYSIC_QTY > 0
AND I.ITEM_DESC LIKE '#%'
AND I.ITEM_GROUP_ID IN ('BABYCARE','FEMCARE')