如何正确编写以下查询?

时间:2013-05-07 07:17:48

标签: sql postgresql

查找所有出售比“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

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

6 个答案:

答案 0 :(得分: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瓶便宜的酒吧,这个查询需要一些编辑。

答案 1 :(得分: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
        )
        ;

答案 2 :(得分: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 cluase断言“所有啤酒”标准的优雅方式。这仍然允许销售其他啤酒,99瓶不卖。

另外,只是旁注 - 将名片中的包装引号保存起来是很困难的。

答案 3 :(得分: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"

我希望这就是你要找的东西。

答案 4 :(得分:0)

请尝试使用“有条款”的“分组依据”。

答案 5 :(得分: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)

结果是你所期望的动力。