简体   繁体   English

如何使用 SQL 获取连续日期之间的最大差异

[英]How to get Maximum Difference between Dates in a row using SQL

Date 1日期 1 Date 2日期 2 Date 3日期 3 Date 4日期 4 LineCount线数 Month_Gap月_差距
2020-01-01 2020-01-01 2019-10-01 2019-10-01 2019-09-06 2019-09-06 1 1个
2020-01-01 2020-01-01 2019-10-01 2019-10-01 2019-09-13 2019-09-13 2019-09-06 2019-09-06 2 2个 0 0
2020-01-01 2020-01-01 2019-10-01 2019-10-01 2019-08-13 2019-08-13 2019-09-06 2019-09-06 2 2个 1 1个

If the LineCount is 1, then Month_Gap should be the maximum month difference between (Date1 & Date3) and (Date2 & Date3).如果LineCount为 1,则Month_Gap应该是 (Date1 & Date3) 和 (Date2 & Date3) 之间的最大月份差。 Date3 will always be in between Date1 and Date2. Date3 将始终在 Date1 和 Date2 之间。

In this Case, the output should be the max month difference between (2020/01/01 - 2019/09/06) and (2019/10/01 - 2019/09/06), which is 3 months:在这种情况下,output 应该是 (2020/01/01 - 2019/09/06) 和 (2019/10/01 - 2019/09/06) 之间的最大月差,即 3 个月:

Date 1日期 1 Date 2日期 2 Date 3日期 3 Date 4日期 4 LineCount线数 Month_Gap月_差距
2020-01-01 2020-01-01 2019-10-01 2019-10-01 2019-09-06 2019-09-06 1 1个 3 3个
2020-01-01 2020-01-01 2019-10-01 2019-10-01 2019-09-13 2019-09-13 2019-09-06 2019-09-06 2 2个 0 0
2020-01-01 2020-01-01 2019-10-01 2019-10-01 2019-08-13 2019-08-13 2019-09-06 2019-09-06 2 2个 1 1个

I was trying something like this but not sure how to go about it - CASE WHEN LineCount = 1 THEN MAX(DATE_DIFF(.....), which won't work I guess.我正在尝试这样的事情,但不确定如何解决 go - CASE WHEN LineCount = 1 THEN MAX(DATE_DIFF(.....),我猜这是行不通的。

The pattern you should use is您应该使用的模式是

SELECT TIMESTAMPDIFF("MONTH", LEAST(date1,date2,date3,date4), GREATEST(date1,date2,date3,date4)) as `maximum_difference`;

This will simply look through your columns, find the least and greatest, and return the result.这将简单地查看您的列,找到最小和最大的列,然后返回结果。

    SELECT 
    CASE WHEN LineCount = 1 THEN GREATEST(DATE_DIFF('month', Date3, Date1), 
    DATE_DIFF('month', Date3, Date2)) END AS Month_Gap

暂无
暂无

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

相关问题 在 Azure Time Series Insights Explorer 中使用 Time Series Expression Syntax 以秒为单位获取两个日期之间的差异 - Get difference between two dates in seconds using Time Series Expression Syntax in Azure Time Series Insights Explorer 如何完成和填补 SQL 中日期之间的空白? - How to complete and fill in gaps between dates in SQL? SQS ApproximateAgeOfOldestMessage 平均值和最大值之间的区别? - Difference between SQS ApproximateAgeOfOldestMessage Average and Maximum? SQL - 按 id 分组的日期之间的天数 - SQL - number of days between dates grouped by id 如何在 bigquery sql 中使用 windows function 在一段时间内获取聚合值的差异? - How to get difference in aggregated value over a certain period of time using windows function in bigquery sql? 如何使用 2 个 DateTime 计数之间的差异找到百分比? - How can I find Percentage using difference between 2 DateTime counts? 如何使用 sql 在 BigQuery 表中查找缺失的日期 - How to find missing dates in BigQuery table using sql TSQL 计算最旧行和最新行之间的差异 - TSQL Calculate difference between oldest and newest row 2 个日期(时间戳)之间的差异 Android Studio 和 Cloud Firestore - Difference between 2 dates ( timestamps ) Android Studio and Cloud Firestore 如何在多个星期的两个日期之间使用 CASE 语句创建自定义的“一年中的一周”? - How can I create a custom 'Week of Year' using a CASE Statement BETWEEN two dates over multiple weeks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM