Datediff为每行最大的行 [英] Datediff for each row agaimst maximum row

查看:104
本文介绍了Datediff为每行最大的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有一个SQL表,其中我有一个日期列,它具有以下格式1yymmdd。我已使用以下sql查询将此转换为datetime。



 Cast(左(右(ltrim(str(date,25,5)) ,LEN(ltrim(str(date,25,5))) -  3),2)+' - '+ left(right(ltrim(str(date,25,5)),LEN(ltrim(str(date,date, 25,5))) -  5),2)+' - '+ 
左(右(ltrim(str(date,25,5)),LEN(ltrim(str(date,25,5)) )-1),2)as datetime)





以上查询给出了以下格式的日期YYYY-MM-DD HH:MM :SS。

但是混淆是我想用查询返回的最大日期来计算每一行的日期差异。



例如:



如果我运行查询并返回以下3个日期

2016-06-08 00:00:00.000

2016-06-05 00:00:00.000

2016-06-03 00:00: 00.000



因此在上面的结果集中,最高日期是2016-06-08 00:00:00.000,当我计算它与其他2天之间的差异时应该分别返回3天​​和5天,

所以我想要的是计算最大日期和每隔一个日期之间的差异,并选择只有少于3个日的差异的日期。



我一如既往地期待着您的帮助。非常感谢。



我尝试了什么:



谷歌搜索它和找不到任何确切的情况

解决方案

解决方案很简单。您只需使用SQL窗口函数。无论如何,看看SQL文档。



假设我们有一个带有OrderDate字段的表。获得所需内容的查询将是:



选择OrderDate,datediff(day,MAX(OrderDate)over(),OrderDate)销售
订单由OrderDate desc





注意,查询中的over运算符。



问候。


好吧,



1)为了把日子放在积极的你只有获取绝对值或在datediff函数中切换日期值的顺序。



2)要过滤值,您应该使用公用表表达式然后过滤。



这是代码:



;与cte as(选择OrderDate,datediff( day,OrderDate,MAX(OrderDate)over())as sales from sales)
select * from cte
其中天数在1到6
之间由OrderDate desc订购


Hello,

I have a SQL table where i have a date column and it has the following format 1yymmdd. I have converted this to datetime using following sql query.

Cast(left(right(ltrim(str(date,25,5)), LEN(ltrim(str(date,25,5)))-3),2) +'-' + left(right(ltrim(str(date,25,5)), LEN(ltrim(str(date,25,5)))-5),2) +'-' +
left(right(ltrim(str(date,25,5)), LEN(ltrim(str(date,25,5)))-1),2) as datetime)



The above query gives me date in following format YYYY-MM-DD HH:MM:SS.
But the confusion is i want to calculate the datedifference of each row with maximum date that the query returns.

For Example:

If i run the query and it returns the following 3 dates
2016-06-08 00:00:00.000
2016-06-05 00:00:00.000
2016-06-03 00:00:00.000

So in above result set the highest date is 2016-06-08 00:00:00.000 and when i calculate the difference between it and other 2 days it should return 3 and 5 days respectively,
so what i want is to calculate the difference between the max date and every other date and select only the dates which has difference of less than 3 dayys.

I am looking forward to your help as always. Thanks alot.

What I have tried:

Googled it and could not find any exact situation

解决方案

Hi, the solution is easy. You just have to use a "SQL Windowing function". Anyway, have a look to SQL Documentation.

Let's suppose we have a table with a "OrderDate" field. The query to get what you want would be:

select OrderDate, datediff(day,MAX(OrderDate) over(),OrderDate) from sales
order by OrderDate desc



Note, "over" operator above in the query.

Regards.


Well,

1) To put days in positive you only have to take absolute value or switching the order of date values in datediff function.

2) To filter values you should use a Common Table Expression and then filter.

Here is the code:

;with cte as (select OrderDate, datediff(day,OrderDate,MAX(OrderDate) over()) as Days from sales)
select * from cte
where Days between 1 and 6
order by OrderDate desc


这篇关于Datediff为每行最大的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆