我有这个问题:
select
s_acctbal, s_name, n_name, p_partkey, p_mfgr,
s_address, s_phone, s_comment
from
part, supplier, partsupp, nation, region
where
p_partkey = ps_partkey
and s_suppkey = ps_suppkey
and p_size = 15
and p_type like '%BRASS'
and s_nationkey = n_nationkey
and n_regionkey = r_regionkey
and r_name = 'EUROPE'
and ps_supplycost = (
select
min(ps_supplycost)
from
partsupp, supplier, nation, region
where
p_partkey = ps_partkey
and s_suppkey = ps_suppkey
and s_nationkey = n_nationkey
and n_regionkey = r_regionkey
and r_name = 'EUROPE'
)
order by
s_acctbal desc, n_name, s_name, p_partkey;
首先怀疑:
为什么再次设置子选择(内部选择)r_name='EUROPE'
?我理解第p_partkey = ps_partkey
行给了这个子选择与outter选择相同的条件,所以不需要再设置它,不是吗?
第二个疑问:
内部选择中的行r_name = 'EUROPE'
在查询中产生了一些差异(我无法看到,但我想知道)。如果我计算原始查询的结果我有460个托管。如果我退出r_name = 'EUROPE'
内部选择,我只得到了144次。
这看起来很疯狂,有这种情况(因此有更多限制),并且比我摆脱这个条件时有更多的结果。为什么会这样?
仅供参考(也许这有助于我的想法为什么这个问题):
原始查询返回:
s_acctbal s_name n_name p_partkey p_mfgr s_address s_phone s_comment
--------- ------------------ -------------- --------- -------------- -------------------------------------- --------------- ----------------------------------------------------------------------------------------------
9938.53 Supplier#000005359 UNITED KINGDOM 185358 Manufacturer#4 QKuHYh,vZGiwu2FWEJoLDx04 33-429-790-6131 blithely silent pinto beans are furiously. slyly final deposits acros
9937.84 Supplier#000005969 ROMANIA 108438 Manufacturer#1 ANDENSOSmk,miq23Xfb5RWt6dvUcvt6Qa 29-520-692-3537 carefully slow deposits use furiously. slyly ironic platelets above the ironic
9936.22 Supplier#000005250 UNITED KINGDOM 249 Manufacturer#4 B3rqp0xbSEim4Mpy2RH J 33-320-228-2957 blithely special packages are. stealthily express deposits across the closely final instructi
9923.77 Supplier#000002324 GERMANY 29821 Manufacturer#4 y3OD9UywSTOk 17-779-299-1839 quickly express packages breach quiet pinto beans. requ
9871.22 Supplier#000006373 GERMANY 43868 Manufacturer#5 J8fcXWsTqM 17-813-485-8637 never silent deposits integrate furiously blit
9870.78 Supplier#000001286 GERMANY 81285 Manufacturer#2 YKA,E2fjiVd7eUrzp2Ef8j1QxGo2DFnosaTEH 17-516-924-4574 final theodolites cajole slyly special,
9870.78 Supplier#000001286 GERMANY 181285 Manufacturer#4 YKA,E2fjiVd7eUrzp2Ef8j1QxGo2DFnosaTEH 17-516-924-4574 final theodolites cajole slyly special,
9852.52 Supplier#000008973 RUSSIA 18972 Manufacturer#2 t5L67YdBYYH6o,Vz24jpDyQ9 32-188-594-7038 quickly regular instructions wake-- carefully unusual braids into the expres
9847.83 Supplier#000008097 RUSSIA 130557 Manufacturer#2 xMe97bpE69NzdwLoX 32-375-640-3593 slyly regular dependencies sleep slyly furiously express dep
9847.57 Supplier#000006345 FRANCE 86344 Manufacturer#1 VSt3rzk3qG698u6ld8HhOByvrTcSTSvQlDQDag 16-886-766-7945 silent pinto beans should have to snooze carefully along the final reques
9847.57 Supplier#000006345 FRANCE 173827 Manufacturer#2 VSt3rzk3qG698u6ld8HhOByvrTcSTSvQlDQDag 16-886-766-7945 silent pinto beans should have to snooze carefully along the final reques
没有内部选择条件r_name = 'EUROPA'
的查询返回:
s_acctbal s_name n_name p_partkey p_mfgr s_address s_phone s_comment
--------- ------------------ -------------- --------- -------------- --------------------------------------- --------------- -------------------------------------------------------------------------------------
9938.53 Supplier#000005359 UNITED KINGDOM 185358 Manufacturer#4 QKuHYh,vZGiwu2FWEJoLDx04 33-429-790-6131 blithely silent pinto beans are furiously. slyly final deposits acros
9817.10 Supplier#000002352 RUSSIA 124815 Manufacturer#2 4LfoHUZjgjEbAKw TgdKcgOc4D4uCYw 32-551-831-1437 blithely pending packages across the ironic accounts grow slyly after the furiously
9721.95 Supplier#000008757 UNITED KINGDOM 156241 Manufacturer#3 Atg6GnM4dT2 33-821-407-2995 ironic, even dolphins above the furiously ironic foxes sleep slyly around the caref
9643.55 Supplier#000005148 ROMANIA 107617 Manufacturer#1 kT4ciVFslx9z4s79p Js825 29-252-617-4850 doggedly even ideas boost furiously against the furiously express
9612.94 Supplier#000003228 ROMANIA 198189 Manufacturer#4 KDdpNKN3cWu7ZSrbdqp7AfSLxx,qWB 29-325-784-8187 carefully pending accounts serve. furiously close deposits boost slyly. q
9571.83 Supplier#000004305 ROMANIA 179270 Manufacturer#2 qNHZ7WmCzygwMPRDO9Ps 29-973-481-1831 furiously final deposits
9558.10 Supplier#000003532 UNITED KINGDOM 88515 Manufacturer#4 EOeuiiOn21OVpTlGguufFDFsbN1p0lhpxHp 33-152-301-2164 daring, sly accounts breach about th
9280.27 Supplier#000007194 ROMANIA 47193 Manufacturer#3 zhRUQkBSrFYxIAXTfInj vyGRQjeK 29-318-454-2133 slyly ironic requests despite the unusual ins
9249.35 Supplier#000003973 FRANCE 26466 Manufacturer#1 d18GiDsL6Wm2IsGXM,RZf1jCsgZAOjNYVThTRP4 16-722-866-1658 quickly ironic sauternes use b
9249.35 Supplier#000003973 FRANCE 33972 Manufacturer#1 d18GiDsL6Wm2IsGXM,RZf1jCsgZAOjNYVThTRP4 16-722-866-1658 quickly ironic sauternes use b
9201.47 Supplier#000009690 UNITED KINGDOM 67183 Manufacturer#5 CB BnUTlmi5zdeEl7R7 33-121-267-9529 blithely unusual accounts integrate slyly. platelets
9094.57 Supplier#000004582 RUSSIA 39575 Manufacturer#1 WB0XkCSG3r,mnQ n,h9VIxjjr9ARHFvKgMDf 32-587-577-1351 asymptotes above the slyly even requests haggle furiously about the regular accounts
最后没有(ps_supplycost = ...)的查询返回:
s_acctbal s_name n_name p_partkey p_mfgr s_address s_phone s_comment
--------- ------------------ -------------- --------- -------------- -------------------------------------- --------------- --------------------------------------------------------------------------------------------------
9984.69 Supplier#000008875 ROMANIA 13872 Manufacturer#3 hRdOqKqyU,sHq 29-132-904-4395 ironic instructions among the ironic requests boost at the ironic, ironic deposits. quick
9955.05 Supplier#000008810 UNITED KINGDOM 73795 Manufacturer#2 ,Ot93zDXOFSjWSKsKrT7XJ4YPCP,A 33-527-478-5988 furiously unusual pinto beans nag ironic platelets. regular, bold fox
9955.05 Supplier#000008810 UNITED KINGDOM 81285 Manufacturer#2 ,Ot93zDXOFSjWSKsKrT7XJ4YPCP,A 33-527-478-5988 furiously unusual pinto beans nag ironic platelets. regular, bold fox
9938.53 Supplier#000005359 UNITED KINGDOM 185358 Manufacturer#4 QKuHYh,vZGiwu2FWEJoLDx04 33-429-790-6131 blithely silent pinto beans are furiously. slyly final deposits acros
9937.84 Supplier#000005969 ROMANIA 108438 Manufacturer#1 ANDENSOSmk,miq23Xfb5RWt6dvUcvt6Qa 29-520-692-3537 carefully slow deposits use furiously. slyly ironic platelets above the ironic
9936.22 Supplier#000005250 UNITED KINGDOM 249 Manufacturer#4 B3rqp0xbSEim4Mpy2RH J 33-320-228-2957 blithely special packages are. stealthily express deposits across the closely final instructi
9923.77 Supplier#000002324 GERMANY 29821 Manufacturer#4 y3OD9UywSTOk 17-779-299-1839 quickly express packages breach quiet pinto beans. requ
9871.22 Supplier#000006373 GERMANY 43868 Manufacturer#5 J8fcXWsTqM 17-813-485-8637 never silent deposits integrate furiously blit
9870.78 Supplier#000001286 GERMANY 81285 Manufacturer#2 YKA,E2fjiVd7eUrzp2Ef8j1QxGo2DFnosaTEH 17-516-924-4574 final theodolites cajole slyly special,
9870.78 Supplier#000001286 GERMANY 181285 Manufacturer#4 YKA,E2fjiVd7eUrzp2Ef8j1QxGo2DFnosaTEH 17-516-924-4574 final theodolites cajole slyly special,
9852.52 Supplier#000008973 RUSSIA 18972 Manufacturer#2 t5L67YdBYYH6o,Vz24jpDyQ9 32-188-594-7038 quickly regular instructions wake-- carefully unusual braids into the expres
9847.83 Supplier#000008097 RUSSIA 130557 Manufacturer#2 xMe97bpE69NzdwLoX 32-375-640-3593 slyly regular dependencies sleep slyly furiously express dep
9847.57 Supplier#000006345 FRANCE 66344 Manufacturer#4 VSt3rzk3qG698u6ld8HhOByvrTcSTSvQlDQDag 16-886-766-7945 silent pinto beans should have to snooze carefully along the final reques
9847.57 Supplier#000006345 FRANCE 86344 Manufacturer#1 VSt3rzk3qG698u6ld8HhOByvrTcSTSvQlDQDag 16-886-766-7945 silent pinto beans should have to snooze carefully along the final reques
9847.57 Supplier#000006345 FRANCE 173827 Manufacturer#2 VSt3rzk3qG698u6ld8HhOByvrTcSTSvQlDQDag 16-886-766-7945 silent pinto beans should have to snooze carefully along the final reques
9836.93 Supplier#000007342 RUSSIA 4841 Manufacturer#4 JOlK7C1,7xrEZSSOw 32-399-414-5385 final accounts haggle. bold accounts are furiously dugouts. furiously silent asymptotes are slyly
由于
答案 0 :(得分:0)
看起来你正试图为p_size = 15和p_type之类的'%BRASS'找到最便宜的供应成本......你的SUBQUERY没有这个限制......所以你的主要查询可能有100个产品(通过类似),但您的SUBQUERY可能有50,000个条目,因为您没有将其限制为仅具有相同产品类型的p_size = 15的相同条件。
你可能需要让你的内部查询还包括要应用MIN()的p_type和GROUP BY,然后将其作为连接的基础。我通过您的命名约定对表/字段关系进行了最佳估计,重写了查询...
另外,请注意我的内部查询是显式使用alias.field名称引用。希望不允许混淆外部查询的相同字段名称/值。我可能不准确,但我认为我接近你正在寻找的东西。
select
s_acctbal,
s_name,
n_name,
p_partkey,
p_mfgr,
s_address,
s_phone,
s_comment,
p_type,
ps_supplycost
from
part
JOIN partsupp
ON p_partkey = ps_partkey
JOIN supplier
ON ps_suppkey = s_suppkey
JOIN nation
ON s_nationkey = n_nationkey
JOIN region
ON n_regionkey = r_regionkey
AND r_name = 'EUROPE'
JOIN ( SELECT
P.p_type,
MIN( PS.ps_supplycost ) MinCost
from
part P
JOIN partsupp PS
ON P.p_partkey = PS.ps_partkey
JOIN supplier S
ON PS.ps_suppkey = S.s_suppkey
JOIN nation N
ON S.s_nationkey = N.n_nationkey
JOIN region R
ON N.n_regionkey = R.r_regionkey
AND R.r_name = 'EUROPE'
where
P.p_size = 15
AND P.p_type like '%BRASS'
) MinPerType
ON p_type = MinPerType.p_type
AND ps_supplycost = MinPerType.MinCost
where
p_size = 15
AND p_type like '%BRASS'
order by
s_acctbal desc,
n_name,
s_name,
p_partkey;
因此,正如您所看到的,检测最低价格的内部查询基于每个“P_TYPE”值。如果处理黄铜制品,乐器或黄铜金属用于制造,管道等,每种“类型”可能会有巨大的价格差异,你正在寻找前:
BRASS #1 (small thing) that supply cost range $1.50 - $2.15 vs
BRASS #83 (huge item) that supply cost range $42.00 - $48.50
你永远不会选择BRASS#83,因为它的价格标签成本不会接近1.50美元。但是,您可以在1.50美元范围内拥有10个BRASS#1供应商,在43.00美元范围内拥有7个BRASS#83项目供应商,因此您的最终查询将返回17条记录(按照此示例)