我无法弄清楚如何在表格中找到最近的条目< =到指定的日期。
示例表:
ENTRY_DATE ID PRICE
01/JAN/2010 1 1.2
01/JAN/2010 2 1.4
01/JAN/2010 3 2.6
02/JAN/2010 1 1.1
02/JAN/2010 2 1.5
02/JAN/2010 3 2.2
03/JAN/2010 1 1.0
03/JAN/2010 2 1.2
03/JAN/2010 3 2.4
04/JAN/2010 2 1.2
04/JAN/2010 3 2.8
05/JAN/2010 1 1.1
05/JAN/2010 3 2.9
我希望能够在05/01/2010或之前找到所有ID的最新价格。现在,并不总是必须在同一日期对每个ID进行价格更新,因此,对于上表,我希望得到以下结果:
ENTRY_DATE ID PRICE
04/JAN/2010 2 1.2
05/JAN/2010 1 1.1
05/JAN/2010 3 2.9
因此它返回了ID 1和3的价格,因为它找到了05/01/2010的条目,然后它找到了ID 2的最新非空值,这是从04/01/2010开始的。我已经看到一些以前的问题/例子几乎使用Coalesce的类似问题,但我无法理解它是如何工作的。我还看到了一些使用MAX的例子,我遇到了麻烦,因为它希望每一列都在group by语句中。注意:我使用的是Microsoft SQL Management Server而不是Oracle。
答案 0 :(得分:1)
有很多方法可以达到你想要的效果。其中之一是使用getUrl, getMethod
之类的分析函数根据最新getRequestHeaders
ROW_NUMBER()
ID
这是Demo。
以下查询将显示ENTRY_DATE
,该日期未包含在日期范围
SELECT ENTRY_DATE, ID, PRICE
FROM
(
SELECT ENTRY_DATE, ID, PRICE,
RN = ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ENTRY_DATE DESC)
FROM TableName
WHERE '20100105' <= ENTRY_DATE
) a
WHERE RN = 1
这是Demo。
答案 1 :(得分:1)
答案 2 :(得分:1)
这应该有效:
create table #t(entry_date datetime,id int,price float)
insert into #t values
('01/JAN/2010', 1, 1.2),
('01/JAN/2010', 2, 1.4),
('01/JAN/2010', 3, 2.6),
('02/JAN/2010', 1, 1.1),
('02/JAN/2010', 2, 1.5),
('02/JAN/2010', 3, 2.2),
('03/JAN/2010', 1, 1.0),
('03/JAN/2010', 2, 1.2),
('03/JAN/2010', 3, 2.4),
('04/JAN/2010', 2, 1.2),
('04/JAN/2010', 3, 2.8),
('05/JAN/2010', 1, 1.1),
('05/JAN/2010', 3, 2.9)
;with cte as
(
select *,
ROW_NUMBER() over (partition by id order by entry_date desc) as x
from #t
where entry_date <= '04/JAN/2010' /* -- here is your input param*/
)
select cast(entry_date as date), id, price from cte
where x=1
答案 3 :(得分:1)
与一些答案相同,但我在发布之前就开始研究
declare @t table (entry_date datetime, id tinyint, price money);
insert into @t values
('01/JAN/2010', 1, 1.2),
('01/JAN/2010', 2, 1.4),
('01/JAN/2010', 3, 2.6),
('02/JAN/2010', 1, 1.1),
('02/JAN/2010', 2, 1.5),
('02/JAN/2010', 3, 2.2),
('03/JAN/2010', 1, 1.0),
('03/JAN/2010', 2, 1.2),
('03/JAN/2010', 3, 2.4),
('04/JAN/2010', 2, 1.2),
('04/JAN/2010', 3, 2.8),
('05/JAN/2010', 1, 1.1),
('05/JAN/2010', 3, 2.9);
declare @searchDate datetime = '05/01/2010';
select @searchDate as 'searchDate';
select cast(tt.entry_date as date) 'entry_date', tt.id, tt.price
from ( select t.entry_date, t.id, t.price
, ROW_NUMBER() over (partition by t.id order by t.entry_date desc) as rn
from @t t
where t.entry_date <= @searchDate
) tt
where tt.rn = 1
order by tt.id