0

我正在为一个项目的 SQL 查询而苦苦挣扎。

给出了下表:

tblProduct => (proProductID, proProductName)
tblSeller => (selSellerID, selSellerName)
linkProductSeller => (linkID, linkProductID, linkSellerID, linkPrice, linkShippingPrice, linkDatetime)

每次产品的价格/运费在卖家处发生变化时,都会在 linkProductSeller 中添加一个新行。

我需要知道产品的当前最低价格/运费组合和卖家。

要获取产品的所有当前价格,我使用此查询

SELECT linkProductID, linkSellerID, linkPrice+linkShippingPrice as price, linkDatetime
FROM linkProductSeller AS a
WHERE linkDatetime = (
    SELECT MAX(linkDatetime)
    FROM linkProductSeller AS b
    WHERE a.linkProductID = b.linkProductID
AND a.linkSellerID = b.linkSellerID)
ORDER BY linkProductID ASC, price ASC, linkDatetime DESC

感谢您的支持。

4

2 回答 2

1

您需要的所有数据都在 linkProductSeller 中,因此应该可以

select l.linkProductId, min(linkPrice + linkShippingPrice) as minPrice
  from linkProductSeller l,
       (select linkProductID, linkSellerId, max(linkDateTime) as linkDateTime
          from linkProductSeller 
         group by linkProductID, linkSellerId) m
 where l.linkProductID = m.linkProductID
   and l.linkSellerID = m.linkSellerID
   and l.linkDateTime = m.linkDateTime
 group by linkProductId
于 2013-08-12T07:18:15.740 回答
0

我猜上面的答案并不完全正确。如果您需要产品和卖家的名称,并且价格的确切更改时间并不重要(您只需要当前的价格),那么我认为您将需要这样的东西:

select 
    p.proProductID, 
    p.proProductName, 
    s.selSellerID, 
    s.selSellerName, 
    min(l.linkPrice + l.linkShippingPrice) as Price
  from 
    tblProduct p,
    tblSeller s,
    (select * from linkProductSeller 
    where (linkProductID, linkSellerID, linkDateTime) 
    in 
        (select linkProductID, linkSellerID, max(linkDateTime) from linkProductSeller 
        group by linkProductID, linkSellerID 
        having linkDateTime = max(linkDateTime)) l
where 
    p.proProductID= l.linkProductID
    and s.selSellerID= m.linkSellerID
group by 
    p.proProductID, 
    p.proProductName, 
    s.selSellerID, 
    s.selSellerName
于 2013-08-12T07:51:47.867 回答