簡體   English   中英

需要基本SQL查詢幫助

[英]Basic SQL query help needed

我有一張有 3 列的桌子。 這是一個示例,其中只有幾個要演示的條目。 該表表示項目價格更改的日期。 我需要一個查詢來告訴我特定日期所有商品的價格。 具體日期可能在價格變化之間。 價格在午夜變化,因此變化的日期,即從那時到上次變化前一天的價格

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 查詢使我逃脫了。 回答表示贊賞。

由於答案朝錯誤的方向進行編輯,請參閱斜體進行編輯。

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

select top 1 with ties *
from ATable
where datePriceEffective <= @givenDate
order by row_number() over (partition by itemCode 
                            order by datePriceEffective desc)
SELECT itemCode,price FRom TableNameHere WHERE datePriceEffective <= '2012-03-05'

在獲得更多信息后進行編輯 - 一個“簡單”的答案,希望能讓它更容易理解。

您正在獲取在您要求的日期范圍內有效的項目的最新日期,然后將表格連接到自身以獲取該日期的價格

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

您將需要按如下方式重新加入表:

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 僅適用於此示例,您應該將其換成您的真實表。

更新:另一種方法是使用 ROW_NUMBER 和 PARTITION,如下所示:

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

(將這個 select 替換為上一個查詢中的那個,以在數據上嘗試一下)

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
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

我已經測試過了! 它將為您提供每件商品的最新價格

假設您有另一個名為“GivenDate”的列,下面是查詢。

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

這適用於 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM