2013-05-07 61 views
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? 

由於只有兩個每個條啤酒。那麼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

回答

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 
     ) 
     ; 
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瓶便宜的酒吧,這個查詢需要一些編輯。

+0

我做了一個編輯,以進一步解釋什麼查詢應該輸出。我也嘗試過,但這不是我想要的。 – caaruiz 2013-05-07 07:40:42

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瓶不賣。

此外,只是一個側面說明 - 這是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

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" 

我希望這是你在找什麼。

0

請嘗試使用「Group By」和「Having clause」。

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) 

結果是動機,因爲你的預期。