简体   繁体   English

从sql server中的最小日期获取最高价格

[英]Getting Maximum price from minimum date in sql server

I am try to make Procedure for fetching maximum price of lowest nearest date from given table. 我试图使程序从给定的表中获取最低最近日期的最高价格。

In procedure i have to pass only MaterialNo and Date, base on that i want max price of lowest nearest month. 在程序中我必须只传递MaterialNo和Date,基于我想要最低最近月份的最高价格。

Procedure 程序

Declare @MaterialNo numeric(18,0),
        @PassDate smalldatetime,

set @MaterialNo = 3
set @PassDate = '01-jun-2013'

Select Statement (i need This Statement)

Where MaterialNo = @MaterialNo

My Table Structure is like below 我的表结构如下所示

Temp Table 临时表

MaterialNo  Date                Price
1           May, 01 2013        28
1           June, 01 2013       25
1           June, 01 2013       30
1           August, 01 2013     35
2           November, 01 2013   40
2           November, 01 2013   50
2           May, 05 2013        15
3           July, 01 2013       14
3           August, 01 2013     20
3           December, 01 2012   12

Expected Output, 预期产出,

if i pass @MaterialNo 1 and @PassDate = '30-Jun-2013' output = 30 如果我通过@MaterialNo 1@PassDate = '30-Jun-2013'输出= 30

if i pass @MaterialNo 1 and @PassDate = '30-Jul-2013' output = 30 如果我通过@MaterialNo 1@PassDate = '30-Jul-2013'输出= 30

if i pass @MaterialNo 1 and @PassDate = '30-Aug-2013' output = 35 如果我通过@MaterialNo 1@PassDate = '30-Aug-2013'输出= 35

if i pass @MaterialNo 1 and @PassDate = '30-May-2013' output = 28 如果我通过@MaterialNo 1@PassDate = '30-May-2013'输出= 28

if i pass @MaterialNo 2 and @PassDate = '30-Nov-2013' output = 50 如果我通过@MaterialNo 2@PassDate = '30-Nov-2013'输出= 50

if i pass @MaterialNo 2 and @PassDate = '30-Aug-2013' output = 15 如果我通过@MaterialNo 2@PassDate = '30-Aug-2013'输出= 15

if i pass @MaterialNo 3 and @PassDate = '15-Dec-2013' output = 12 如果我通过@MaterialNo 3@PassDate = '15-Dec-2013'输出= 12

if i pass @MaterialNo 3 and @PassDate = '10-Nov-2013' output = 20 如果我通过@MaterialNo 3@PassDate = '10-Nov-2013'输出= 20

if i pass @MaterialNo 3 and @PassDate = '20-Sep-2013' output = 20 如果我通过@MaterialNo 3@PassDate = '20-Sep-2013'输出= 20

if i pass @MaterialNo 3 and @PassDate = '30-Jul-2013' output = 14 如果我通过@MaterialNo 3@PassDate = '30-Jul-2013'输出= 14

SQLFiddle SQLFiddle

I would use the following SELECT statement: 我会使用以下SELECT语句:

SELECT TOP 1 Price
FROM TempTable
WHERE
  MaterialNo = @MaterialNo
  AND @PassDate >= Date
ORDER BY Date DESC, Price DESC

I sorted by Date and then by Price DESC to handle the condition where there are duplicate entries for a MaterialNo and Date (ie MaterialNo = 1 , Date = '01-Jun-2013' ). 我按Date排序,然后按Price DESC排序,以处理MaterialNoDate有重复条目的情况(即MaterialNo = 1Date = '01-Jun-2013' )。

Your query is: 您的查询是:

declare @materialNo int = 3
declare @date date = '10-Nov-2013'

Select max(Price) from TempTable
where MaterialNo = @materialNo
and [Date] = (select max(T.[Date]) from TempTable T where T.[Date] < @date
         and T.MaterialNo = TempTable.MaterialNo)

Which is really pretty simple: the date selected is the maximum before your parameter date for the given material. 这非常简单:选择的日期是给定材料的参数日期之前的最大值。 Then you get the max price on that date. 然后您将获得该日期的最高价格。

Thanks for using SQL Fiddle! 感谢您使用SQL Fiddle! Your script has an error though, the last insert there should be: 你的脚本有错误,最后一个插入应该是:

Insert Into TempTable Values( 3,'01-Dec-2013',12 ) -- it had wrong year

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM