具有多个选择的组BY(Oracle)

时间:2014-04-25 20:51:06

标签: sql oracle oracle-sqldeveloper

我在使用group by多次选择时遇到问题。我想为每次拍卖选择最低出价报价。但我还必须得到出价的用户名。正如您在结果中看到的,我为每次拍卖获得了多个结果,如果我从Groupby语句中删除元素,则会收到错误消息。如何通过ID_Auction进行分组并仍然显示用户的名称?谢谢你的帮助

SELECT A.ID_AUCTION,MIN(B.PRICE) AS PRICE,U.NAME
    FROM AUCTION A,BIDS B,USERS U,PRODUCT P
    WHERE  P.TYPE='CocaCola'
    --Joins
    and A.ID_AUCTION=B.ID_AUCTION
    and BID.ID_USER=U.ID_USER
    and A.ID_PRODUCT=P.ID_PRODUCT
    GROUP BY A.ID_AUCTION,U.NAME;

ID_AUCTION   PRICE         NAME                                     
---------- ----------  -------------- ------------------------------------------
    27         25         Andrew                                    
    28         40          John                                    
    27         30         Michael                                     
    28         35          Peter   

我想要的输出:

ID_AUCTION   PRICE         NAME                                     
---------- ----------  -------------- ------------------------------------------
    27         25         Andrew                                                                                                         
    28         35          Peter   

4 个答案:

答案 0 :(得分:0)

SELECT A.ID_AUCTION,MIN(B.PRICE) AS PRICE,U.NAME
    FROM AUCTION A,BIDS B,USERS U,PRODUCT P
    WHERE  P.TYPE='CocaCola'
    --Joins
    and A.ID_AUCTION=B.ID_AUCTION
    and BID.ID_USER=U.ID_USER
    and A.ID_PRODUCT=P.ID_PRODUCT
    GROUP BY A.ID_AUCTION,U.NAME;

应该是这样的:

SELECT A.ID_AUCTION, U.NAME, B.PRICE as PRICE
from AUCTION A
inner join (SELECT ID_AUCTION, NAME, MIN(Price) from Bids order by 1,2,3) B
ON a.ID_AUCTION = b.ID_AUCTION
inner join Users U
ON B.ID_USER = U.ID_USER
inner join Product P
ON A.ID_PRODUCT = P.ID_PRODUCT
WHERE P.TYPE = 'CocaCola'
GROUP BY A.ID_AUCTION, U.NAME

我从来没有见过加入按照你的方式工作,但这可能是甲骨文的事情......实际上我认为这在大多数SQL领域被认为是不好的做法...

答案 1 :(得分:0)

尝试一下,这样就可以了。它的编码风格还可以。一些旧系统仍然适用于ANSI-89 SQL样式:

SELECT A.ID_AUCTION,
       B.PRICE,
       U.NAME
FROM AUCTION A,
     BIDS B,
     USERS U,
     PRODUCT P
WHERE P.TYPE='CocaCola' --Joins

  AND A.ID_AUCTION=B.ID_AUCTION
  AND B.ID_USER=U.ID_USER
  AND A.ID_PRODUCT=P.ID_PRODUCT
  AND B.PRICE = (SELECT MIN(bids.PRICE) FROM BIDS     --get only the bid with MIN price
                 WHERE bids.ID_AUCTION = A.ID_AUCTION);

答案 2 :(得分:0)

select * from 
(
SELECT A.ID_AUCTION,B.PRICE,U.NAME,
 Dense_Rank() over (partition by ID_AUCTION order by Price Asc)AS Rank
    FROM 
    AUCTION A
    JOIN
    BIDS B
    ON A.ID_AUCTION=B.ID_AUCTION
    JOIN
    USERS U
   ON BID.ID_USER=U.ID_USER
   JOIN
    PRODUCT P
   ON A.ID_PRODUCT=P.ID_PRODUCT
    WHERE  P.TYPE='CocaCola')
   where rank='1'

答案 3 :(得分:-1)

首先应该按A.ID_AUCTION分组,然后在你的选择中排第二,你可以试一下(U.NAME)

SELECT A.ID_AUCTION,MIN(B.PRICE) AS PRICE,min(U.NAME)
FROM AUCTION A,BIDS B,USERS U,PRODUCT P
WHERE  P.TYPE='CocaCola'
--Joins
and A.ID_AUCTION=B.ID_AUCTION
and BID.ID_USER=U.ID_USER
and A.ID_PRODUCT=P.ID_PRODUCT
GROUP BY A.ID_AUCTION;