sql server:选择日期与年末相对应的行并计算价格

kyks70gy  于 2021-06-15  发布在  Mysql
关注(0)|答案(4)|浏览(315)

我有一个数据表:

ProductNum |   ProductVariation |   Past_Price  | Current_Price  |     Order_Date       
 ------------  ------------------    ------------  ---------------  --------------------- 
       1                 33              96.05          100.10       2014-01-01 00:00:00  
       1                 33              97.65          100.10       2014-12-03 12:34:52  
       1                 33              98.98          100.10       2015-01-02 05:50:32  
       1                 33              99.98          100.10       2016-03-02 06:50:43  
       1                 33              100.01         100.10       2016-12-12 06:05:43  
       1                 33              100.05         100.10       2017-01-02 05:34:43

我想知道是否可以查询这些行,以便得到最接近12月31日{year}的行?
所以输出是:

ProductNum  | ProductVariation  | Past_Price |  Current_Price   |  Order_Date       
------------ ------------------ ------------   ---------------    --------------------- 
       1                 33        98.98          100.10           2015-01-02 05:50:32  
       1                 33        99.98          100.10           2016-03-02 06:50:43  
       1                 33       100.01          100.10           2017-01-02 05:34:43

每个订单最接近12月31日,{年}年:201420152016

piwo6bdm

piwo6bdm1#

你可以用这个。这将避免硬编码和复制粘贴工会。

declare @currDate datetime;
select @currDate = '12/31/2019';

while @currDate > '12/31/2013'

begin
select *
   from Product
   where abs(datediff(second, OrderDate, @currDate))
   = (select min(
       abs(datediff(second, OrderDate, @currDate))
   )
   from Product )

 select @currDate = dateadd(year,-1,@currDate);
end

我用了以下小提琴:

create table Product (ProdNum int, ProdVar int, PastPrice decimal, CurrentPrice decimal, OrderDate datetime);
insert into Product values (1, 33, 96.05, 100.10, '2014-01-01 00:00:00');
insert into Product values (1, 33, 97.65, 100.10, '2014-12-03 12:34:52');
insert into Product values (1, 33, 98.98, 100.10, '2015-01-02 05:50:32');
insert into Product values (1, 33, 99.98, 100.10, '2016-03-02 06:50:43');
insert into Product values (1, 33, 100.01, 100.10, '2016-12-12 06:05:43');
insert into Product values (1, 33, 100.05, 100.10, '2017-01-02 05:34:43');
wko9yo5t

wko9yo5t2#

你似乎真的想要在年底后的第一次约会:

select top (1) with ties t.*
from t
order by row_number() over (partition by year(order_date) order by order_date asc);
taor4pac

taor4pac3#

得到 row_number() 按绝对值排序的每年 datediff() 在订单日期和一年中的31-12日之间。然后选择其中一个行号等于的所有行 1 .

SELECT *
       FROM (SELECT *,
                    row_number() OVER (ORDER BY abs(datediff(second, '2014-12-31', t.order_date))) rn2014,
                    row_number() OVER (ORDER BY abs(datediff(second, '2015-12-31', t.order_date))) rn2015,
                    row_number() OVER (ORDER BY abs(datediff(second, '2016-12-31', t.order_date))) rn2016
                    FROM elbat t) x
       WHERE 1 IN (x.rn2014,
                   x.rn2015,
                   x.rn2016);

db<>小提琴

lokaqttq

lokaqttq4#

您可以按日期差排序,并获得每年的前1行。
对于sqlserver:

DECLARE @year2014 datetime2 = '2014-12-31 12:00:00';
DECLARE @year2015 datetime2 = '2015-12-31 12:00:00';
DECLARE @year2016 datetime2 = '2016-12-31 12:00:00';

select * from (
  select top(1) * from products
  order by abs(datediff(second, @year2014, Order_Date))
) as p    
union all
select * from (
  select top(1) * from products
  order by abs(datediff(second, @year2015, Order_Date))
)as p    
union all
select * from (
  select top(1) * from products
  order by abs(datediff(second, @year2016, Order_Date))
) as p

你可以随意改变12月31日的时间。
对于mysql:

set @year2014 = '2014-12-31 12:00:00';
set @year2015 = '2015-12-31 12:00:00';
set @year2016= '2016-12-31 12:00:00';

select * from (
  select * from products
  order by abs(TIMESTAMPDIFF(second, @year2014, Order_Date)) limit 1
) as p    
union all
select * from (
  select * from products
  order by abs(TIMESTAMPDIFF(second, @year2015, Order_Date)) limit 1
)as p    
union all
select * from (
  select * from products
  order by abs(TIMESTAMPDIFF(second, @year2016, Order_Date)) limit 1
) as p

相关问题