2

查找所有销售的啤酒比“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?

因为每个酒吧只有两种啤酒。然后动机有更便宜的啤酒。

这是我到目前为止所拥有的,但我没有得到正确的输出。

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

解决方案应该是动机,但我在尝试获得正确的查询时遇到了麻烦。

4

6 回答 6

1

您只需要比 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 瓶的酒吧,则此查询需要进行一些编辑。

于 2013-05-07T07:30:02.013 回答
1
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
        )
        ;
于 2013-05-07T10:26:44.640 回答
0

你需要:

  • 如果同一酒吧有多个符合条件的啤酒,则该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 子句断言“所有啤酒”标准的优雅方式。这仍然允许酒吧出售99 瓶不卖的其他啤酒。

此外,只是一个旁注 - 将包装引号保存在名称中是很有趣的。

于 2013-05-07T07:42:04.970 回答
0

以下查询将查找与以“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"

我希望这就是你要找的。

于 2013-05-07T08:56:06.157 回答
0

请尝试将“Group By”与“Having 子句”一起使用。

于 2013-05-07T09:10:55.797 回答
0

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

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)

结果如您所料。

于 2013-05-07T09:18:29.227 回答