如何正确编写以下查询?

如何正确编写以下查询?

问题描述:

发现销售是比“99瓶”销售的所有啤酒便宜啤酒所有的酒吧如何正确编写以下查询?

编辑:

解读: 因此,所有的啤酒从BAR1比较和检查所有的这些啤酒是不是更便宜“99瓶”

例如:

 Is bluemoon price in motiv cheaper than bluemoon in 99 bottles? 
    Is Guiness price in motiv cheaper than Guiness in 99 bottles? 

由于只有两个每个条啤酒。那么motiv有更便宜的啤酒。

这是我迄今为止,但我没有得到正确的输出。

select * from sells s1, sells s2 where s1.bar <>s2.bar and s2.bar <> 
'"99 bottles"' and s1.beer=s2.beer and s1.price < all 
(select s.price from sells s where s.bar ='"99 bottles"') ; 

以下是表格包含的内容。

 bar  | beer | price 
--------------+----------+------- 
"99 bottles" | Bluemoon | 10 
"99 bottles" | Guiness |  9 
"rosies"  | Bluemoon | 11 
"rosies"  | Guiness |  5 
"motiv"  | Bluemoon |  4 
"motiv"  | Guiness |  2 

该解决方案应该是motiv,但我无法尝试获得正确的查询。

+0

究竟是什么above..table – 2013-05-07 07:45:20

+0

的解决方案应该是MOTIV侑预期的输出。酒吧的名字。正如我上面所说... – caaruiz 2013-05-07 08:05:29

SELECT DISTINCT b.bar 
FROM barbeerprice b 
WHERE b.bar <> '99 bottles' 
     -- '99 bottles' must not sell a same beer cheaper 
AND NOT EXISTS (SELECT * 
     FROM barbeerprice me 
     JOIN barbeerprice nx99 
         ON nx99.beer = b.beer 
         AND nx99.bar = '99 bottles' 
         AND nx99.bar <> me.bar 
         AND nx99.price < me.price 
     WHERE me.bar = b.bar 
     ) 
     ; 

你只需要比99瓶最便宜的啤酒便宜的啤酒。尝试这样的:

SELECT * FROM sells s1 
where s1.price < (select MIN(price) FROM sells s2 where s2.bar = '"99 bottles"') and s1.bar <> '"99 bottles"' 

PS:如果你想显示所有啤酒比99瓶便宜的酒吧,这个查询需要一些编辑。

+0

我做了一个编辑,以进一步解释什么查询应该输出。我也尝试过,但这不是我想要的。 – caaruiz 2013-05-07 07:40:42

您需要:

  • DISTINCT关键字以防止多次列出一样的吧,如果有符合标准的多个啤酒
  • 一个WHERE子句与子查询使用MIN()功能找到最低的价格为酒吧

如下:

select distinc bar 
from sellsy 
where price < (
    select min(price) 
    from sells 
    where bar = '"99 bottles"') 

由啤酒比较啤酒的问题是:

查找其价格也比“99瓶”

和查询会更便宜所有的酒吧:

select s2.bar 
from sells s1 
join sells s2 
    ob s1.beer = s2.beer 
    and s2.price < s1.price 
where s1.bar = '"99 bottles"' 
group by 1 
having count(s2.beer) = count(s1.beer) 

请注意,“所有啤酒”标准通过HAVING cluase声明的优雅方式。这仍然允许卖出其他啤酒,99瓶不卖。

此外,只是一个侧面说明 - 这是steicy保存名称包装报价。

+0

这只给出他们出售的最便宜的啤酒,这不是问题所要求的。通过比较每种啤酒,哪个地方销售最便宜的啤酒。正如上面的问题所述。 – caaruiz 2013-05-07 08:15:55

+0

这实际上完全回答了原始问题。我想你会解释我们错了。这显然是家庭作业/课外作业,所以最好问问你的老师究竟是什么意思。 – Bohemian 2013-05-07 10:43:52

+0

如果问题被误解了,我表示歉意,但正如我之前说过的,我想找到哪些酒吧销售比99瓶更便宜的啤酒。我想比较啤酒一个接一个。所以像这样的东西'while(啤酒列表){if bar1.beer == bar1.beer && bar2.name =“99 bottles && bar1.price caaruiz 2013-05-07 11:17:20

下面的查询将找到与之相比,“99瓶”卖的啤酒是同样或更昂贵的所有啤酒:

select * from beers join beers compare on ( 
    beers.beer = compare.beer and beers.price >= compare.price 
) where compare.bar = '99 bottles'; 

结果如下:

bar;beer;price;bar;beer;price 
"99 bottles";"Bluemoon";10;"99 bottles";"Bluemoon";10 
"99 bottles";"Guiness";9;"99 bottles";"Guiness";9 
"rosies";"Bluemoon";11;"99 bottles";"Bluemoon";10 

此查询可以很容易地用来找到所有在所有啤酒价格较低的酒吧,我们需要找到所有不在上面列表中的酒吧:

select distinct bar from beers where bar not in (
    select beers.bar from beers join beers compare on ( 
      beers.beer = compare.beer and beers.price >= compare.price 
     ) where compare.bar = '99 bottles' 
); 

结果是:

bar 
"motiv" 

我希望这是你在找什么。

请尝试使用“Group By”和“Having clause”。

检查以下查询。这应该解决你的目的

Declare @sells table(Bar varchar(100),brand varchar(100),price int) 
insert into @sells 
select '99 bottles','Bluemoon',10 
union all 
select '99 bottles','Guiness',9 
union all 
select '99 bottles','KF',9 
union all 
select 'rosies','Bluemoon',11 
union all 
select 'rosies','Guiness',5 
union all 
select 'motiv','Bluemoon',4 
union all 
select 'motiv','Guiness',2 

;with cteBar 
as 
(
select s1.price as actualprice,s2.* from @sells as s1 right outer join 
@sells as s2 on(s1.brand=s2.brand and s1.price>s2.price and s1.Bar='99 bottles') 
) 

select Bar from cteBar group by Bar having count(actualprice)=count(price) 

结果是动机,因为你的预期。