繁体   English   中英

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

[英]SQL Server: Select rows with dates that correspond to end of year and calculate price

我有一个数据表:

  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  

年份:2014、2015、2016年,每个订单最接近{Year} 12月31日

您可以按日期差异进行排序,并获取每年的前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

获取在订购日期和该年的31-12之间的绝对datediff()订购的每年的row_number() 然后选择所有行号之一等于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 <>小提琴

您可以使用它。 这样可以避免多年的硬编码和复制粘贴联合。

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');

您似乎实际上希望在年末之后的第一个日期:

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

暂无
暂无

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

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