0

表格如下所示:

tblProducts(大约 20 万条记录)

SKU,Title,CategoryID
100,Apple,0
101,Orange,0
102,Carrot,1

tblCategories

CategoryID,CategoryName
0,Fruit
1,Vegetables

tblPrices(大约 1000 万条记录)

SKU,BuyPrice,SellPrice,Timestamp
100,1,2,2013-1-1 23:04
100,3,6,2013-1-2 19:04
100,4,8,2013-1-3 21:04
100,4,8,2013-1-4 20:04
100,4,8,2013-1-5 22:04

我需要获取所有产品的当前 BuyPrice(来自 tblPrices 的最新产品)并将其与 X 天前来自 NOW() 的最新 BuyPrice 进行比较。我只需要在 BuyPrice 中更改的产品。

这样我就可以回答这个问题,“过去 X 天内哪些产品的价格发生了变化?”。鉴于上面的一小部分数据,我会得到一个空表 1 天或 2 天,但是 3 天,我想检索:

SKU,Title,CategoryName,OldBuyPrice,OldSellPrice,NewBuyPrice,NewSellPrice, NBP/OBP
100,Apple,Fruit,       3,          6,           4,          8,            2.00

4天:

SKU,Title,CategoryName,OldBuyPrice,OldSellPrice,NewBuyPrice,NewSellPrice, NBP/OBP
100,Apple,Fruit,       1,          2,           4,          8,            4.00

我一直在网上寻找类似的解决方案,但没有找到。任何订购都可以。提前致谢!

4

3 回答 3

1

我尝试创建表 oldtpc ,它将在 X 天后获取日期最大的产品。还有另一个具有最近日期价格的newtpc。在“oldtpc”和“newtpc”之间的开启条件下,我正在检查这些日期是否不匹配

select tp.SKU, tp.Title, tc.CategoryName, oldtpc.BuyPrice, oldtpc.Sellprice, newtpc.buyprice, newtpc.Sellprice
from tblProducts tp
join  tblCategories tc 
on tp.CategoryId= tc.CateogryId
join (select SKU, BuyPrice, SellPrice, max(TimeStamp) as TimeStamp 
             from tblPrices new
             where DATEDIFF ( dd, timestamp, getdate()) < @xdays
             group by SKU, BuyPrice, SellPrice ) as newtpcnewtpc 
on tp.SKU = newtpc .sku 
join (select SKU, BuyPrice, SellPrice, max(TimeStamp) as TimeStamp 
             from tblPrices old 
             where DATEDIFF ( dd, timestamp, getdate()) >= @xdays 
             group by SKU, BuyPrice, SellPrice ) as oldtpc 
on oldtpc.SKU = tp.SKU and oldtpc.timestamp <> newtpc.timestamp

PS:一些语法可能是错误的,但我认为一般的想法应该可以正常工作

于 2013-01-04T21:53:58.987 回答
1

试试这个:SQL 小提琴

Select 
   tt.SKU,tt.BuyPrice,tt.SellPrice,tr.BuyPrice As NewBuyPrice,tr.SellPrice As NewSellPrice, tr.BuyPrice/tt.BuyPrice NNBP 
From
(
   select SKU, Max(Timestamps) timestamps
   from t
   Group by t.SKU
) t
Join t tr on t.SKU = tr.SKU AND t.timestamps = tr.Timestamps
Join t tt ON t.SKU = tt.SKU
AND DATEDIFF(D, tt.timestamps, t.timestamps) = 4
AND tt.BuyPrice <> tr.BuyPrice
于 2013-01-04T23:10:11.657 回答
1

当然,这是可行的。有一个不错的窗口功能版本,尽管可能还有更好的方法来做到这一点:

WITH Current_Price (sku, buyPrice, sellPrice) as 
                   (SELECT sku, buyPrice, sellPrice
                    FROM (SELECT sku, buyPrice, sellPrice,
                                 ROW_NUMBER() OVER(PARTITION BY sku
                                                   ORDER BY timestamp DESC) as rownum
                          FROM price) t
                    WHERE rownum = 1),

 Price_Back_Previous_Days (sku, buyPrice, sellPrice) as 
                          (SELECT sku, buyPrice, sellPrice
                           FROM (SELECT sku, buyPrice, sellPrice,
                                        ROW_NUMBER() OVER(PARTITION BY sku
                                                         ORDER BY timestamp DESC) as rownum
                                 FROM price
                                 WHERE timestamp < DATEADD(DAY, -3, CONVERT(DATE, GETDATE()))) t
                           WHERE rownum = 1)

SELECT p.sku, p.title, c.categoryName,
       prev.buyPrice as oldBuyPrice, prev.sellPrice as oldSellPrice,
       curr.buyPrice as newBuyPrice, curr.sellPrice as newSellPrice,
       CASE WHEN prev.buyPrice = 0 
            THEN curr.buyPrice
            ELSE 1.0 * curr.buyPrice / prev.buyPrice END as 'NBP/OBP' 
FROM Product p
JOIN Category c
     ON c.categoryId = p.categoryId
JOIN Current_Price curr
     ON curr.sku = p.sku
JOIN Price_Back_Previous_Days prev
     ON prev.sku = p.sku
     AND (prev.buyPrice <> curr.buyPrice
          OR prev.sellPrice <> curr.sellPrice)

产生预期的

SKU  TITLE  CATEGORYNAME  OLDBUYPRICE  OLDSELLPRICE  NEWBUYPRICE  NEWSELLPRICE  NBP/OBP
100  Apple  Fruit         1            2             4            8             4

(有一个有效的SQL Fiddle Example,用一个特定的日期代替GETDATE()未来的原因。)

您会注意到我正在使用-3(而不是预期的那样),因为无论何时(白天)运行查询-4,我都在尝试检索“截至本日期”的相同值。显然,“当前价格”仍然可以改变(尽管在那里添加时间戳参数也可以解决这个问题);但是,这应该确保您在给定的一天中看到的是一致的价格。

于 2013-01-05T00:49:02.577 回答