2012-04-05 129 views
-1

我有一個3列的表。 下面是一個只有幾個演示條目的例子。該表格表示物品價格變化的日期。我需要一個查詢來告訴我特定日期的所有商品的價格。具體日期可能在價格變化之間。在午夜的價格變化,所以變化之日起,就是價格從那時起直到與當日之前的變化前:需要基本的SQL查詢幫助

itemCode datePriceEffective price 
AB   2012-01-01   9.99 
AB   2012-03-02   10.50 
XY   2011-09-20   34.99 

我希望得到所有的物品價格給定日期。每個日期都沒有條目,只是價格變化的日期。 所以2012-03-05會回來

AB 10.50 
XY 34.99 

和2012-02-27將返回:

得到這個逃脫我需要
AB 9.99 
XY 34.99 

的SQL查詢。解決讚賞。

編輯爲答案會發生錯誤的方向請參閱斜體的編輯。之後更多信息

+0

你有使用日期另一列?因爲您在查詢中提到的日期不會在任何列中看到。 – Virus 2012-04-05 10:20:09

回答

1

您將需要加入回表如下:

declare @effdate datetime 
set @effdate = CONVERT(datetime,'2012-02-27') 

;WITH CTE_DATA as (
    select itemCode='AB', datePriceEffective = CONVERT(Datetime,'2012-01-01'), price = 9.99 
    union all select itemCode='AB', datePriceEffective = CONVERT(Datetime,'2012-03-02'), price = 10.50 
    union all select itemCode='XY', datePriceEffective = CONVERT(Datetime,'2011-09-20'), price = 34.99 
) 
select 
    d.itemcode, 
    price 
from 
    CTE_DATA d 
    join (
     select 
      itemcode, 
      effdate = MAX(datepriceeffective) 
     from CTE_DATA sub where sub.datepriceeffective <= @effdate 
     group by itemcode 
    ) x 
     on x.itemCode = d.itemCode 
     and x.effdate = d.datePriceEffective 

注意,CTE只是在這個例子中,你應該將其交換爲您的真正的表。

UPDATE:另一種方法是使用ROW_NUMBER和分區如下:

SELECT   
    itemcode, 
    price 
FROM  
    (
     SELECT 
      itemcode, 
      price, 
      rowno = ROW_NUMBER() over (partition by itemcode order by datePriceEffective desc) 
     from 
      CTE_DATA 
      where datePriceEffective <= @effdate 
    ) x 
where 
    rowno = 1 

(替代這次選擇了一個在前面的查詢嘗試它的數據)

+0

這看起來很複雜。 – 2012-04-05 10:26:57

+0

CTE只是爲了生成一些數據來運行。實際的查詢很簡單。尼古拉的解決方案與我的第二個答案類似,但更加整齊地過濾了所需的結果。 – 2012-04-05 10:49:48

+1

標記爲接受的答案,因爲這是第一次工作。其他一些人也工作 – 2012-04-05 10:53:57

2
SELECT itemCode,price FRom TableNameHere WHERE datePriceEffective <= '2012-03-05' 

編輯 - 一個「簡單」的答案,希望可以很容易地遵循。

你得到thelatest日期是在所需的日期範圍內是有效的項目,然後加入表本身得到的價格在該日期

SELECT t1.itemCode,t1.price FROM 
    (
    SELECT itemCode,MAX(datePriceEffective) AS datePriceEffective 
    FROM TableNameHere 
    WHERE datePriceEffective <= '2012-03-05' 
) a 
INNER JOIN TableNameHere t1 ON t1.itemCode = a.itemCode AND t1.datePriceEffective = a.datePriceEffective 
+0

這不可能是正確的,那天沒有條目! – 2012-04-05 10:16:13

+0

對不起,是你在你的問題中列出了那個日期,然後編輯你的問題,以解釋你的後... – 2012-04-05 10:24:51

+0

提供datePriceEffective是一個日期時間列,你可以使用小於和大於運營商。 – 2012-04-05 10:26:46

0

假設你有一個名爲另一列「 GivenDate',下面是查詢。

SELECT itemCode, price 
FROM tblName 
WHERE GivenDate= '2012-03-05' 
+0

我沒有另一列 – 2012-04-05 10:23:03

+0

您是否必須使用datePriceEffective列來獲取給定日期的結果?並且datePriceEffective經常更新? – Virus 2012-04-05 10:24:26

+0

如果是這種情況,那麼您可以查詢datePriceEffective列中的日期。你不能使用任何'其他'給定的日期。 – Virus 2012-04-05 10:25:43

1
declare @date datetime = '2012-03-05' 

select distinct itemcode, 
    (
     select top(1) itemprice 
     from mytable mt2 
     where 
      mt1.itemcode = mt2.itemcode and 
      mt2.datepriceeffective <= @date 
     order by datepriceeffective desc 
    ) as itemprice 
from 
    mytable mt1 
where 
    datepriceeffective <= @date 
+0

這工作謝謝。雖然不是第一次,所以錯過了接受。 – 2012-04-05 10:54:31

3

這會按照每個itemCode的datePriceEffective降序檢索第一條記錄。

select top 1 with ties * 
from ATable 
where datePriceEffective <= @givenDate 
order by row_number() over (partition by itemCode 
          order by datePriceEffective desc) 
+0

+1:這是'WITH TIES'的一個很好的用法 - 提供了一個非常好的解決方案! – 2012-04-05 10:40:29

+0

這個作品謝謝。雖然不是第一次,所以錯過了接受。 – 2012-04-05 10:55:28

1
SELECT b.itemCode, 
(SELECT Price FROM dbo.tblProducts a WHERE datePriceEffective = 
(SELECT MAX(a.datePriceEffective) FROM dbo.tblProducts a WHERE a.itemCode = b.itemCode)) AS Price 
FROM dbo.tblProducts b 
WHERE b.datePriceEffective <= '2012-03-05' 
GROUP BY b.itemCode 

我已經測試了!它會給你最新的價格爲每個項目

0

這適用於SQL2000

SELECT  s.itemCode 
      , (SELECT  TOP 1 price 
       FROM  #MyTable 
       WHERE  itemCode = s.itemCode 
          AND datePriceEffective < @SearchDate) AS price 
FROM  (
      SELECT DISTINCT itemCode 
      FROM #MyTable 
      ) s