简体   繁体   English

根据 MA​​X 和 MIN 值完成 datediff 计算

[英]Complete datediff calculation based on MAX and MIN values

Sorry I did post a question similar earlier, but I was not that clear.抱歉,我之前确实发布过类似的问题,但我不是很清楚。 I have a table with the fields, Customer, ID_Date, Pstng_Date, SUMOfAmount, Days_BetweenMax and days_between Min.我有一个包含字段的表,客户,ID_Date,Pstng_Date,SUMOfAmount,Days_BetweenMax 和 days_between Min。

What I want is a query that shows me the date difference between the pstng_date and the ID_Date where the pstng_date is the max value for that customer and another column that shows the same calculation where the pstng_date is the minimum value for that customer.我想要的是一个查询,它显示 pstng_date 和 ID_Date 之间的日期差异,其中 pstng_date 是该客户的最大值,另一列显示相同的计算,其中 pstng_date 是该客户的最小值。 Those customers with only one Pstng_date should display as zero那些只有一个 Pstng_date 的客户应该显示为零

So the Query should display the results like this:所以查询应该显示这样的结果:

Customer ID_Date    Pstng_Date SumOfAmount Days_BetweenMAX days_betweenMIN
-------- ---------- ---------- ----------- ------------
Holmes   31/01/2014 10/01/2014  $21,545.59            0       0
James    31/01/2014 10/01/2014 -$21,197.89            0       21
James    31/01/2014  5/01/2014  -$7,823.14            0       0
James    31/01/2014 24/01/2014     $308.00            7       0
Rod      31/01/2014 17/01/2014  -$2,603.95            0       0
Lisa     31/01/2014 17/01/2014  $22,019.49            0       0

Assuming that your existing table is called [Postings], you could create a query to calculate the MIN() and MAX() values of [Pstng_Date]假设您现有的表名为 [Postings],您可以创建一个查询来计算 [Pstng_Date] 的 MIN() 和 MAX() 值

SELECT
    Customer,
    MIN(Pstng_Date) AS MinOfPstng_Date,
    MAX(Pstng_Date) AS MaxOfPstng_Date
FROM Postings
GROUP BY Customer

returning回来

Customer  MinOfPstng_Date  MaxOfPstng_Date
--------  ---------------  ---------------
Holmes    2014-01-10       2014-01-10     
James     2014-01-05       2014-01-24     
Lisa      2014-01-17       2014-01-17     
Rod       2014-01-17       2014-01-17     

Then you could use that as a subquery in the query to calculate the date differences然后您可以将其用作查询中的子查询来计算日期差异

SELECT
    p.Customer,
    p.ID_Date,
    p.Pstng_Date,
    p.SumOfAmount,
    IIf(q.MaxOfPstng_Date=q.MinOfPstng_Date,0,IIf(p.Pstng_Date=q.MaxOfPstng_Date,DateDiff("d",p.Pstng_Date,p.ID_Date),0)) AS Days_BetweenMAX,
    IIf(q.MaxOfPstng_Date=q.MinOfPstng_Date,0,IIf(p.Pstng_Date=q.MinOfPstng_Date,DateDiff("d",p.Pstng_Date,p.ID_Date),0)) AS Days_BetweenMIN
FROM
    Postings AS p
    INNER JOIN
    (
        SELECT
            Customer,
            MIN(Pstng_Date) AS MinOfPstng_Date,
            MAX(Pstng_Date) AS MaxOfPstng_Date
        FROM Postings
        GROUP BY Customer
    ) AS q
        ON p.Customer = q.Customer

returning回来

Customer  ID_Date     Pstng_Date  SumOfAmount  Days_BetweenMAX  Days_BetweenMIN
--------  ----------  ----------  -----------  ---------------  ---------------
Holmes    2014-01-31  2014-01-10     21545.59                0                0
James     2014-01-31  2014-01-10    -21197.89                0                0
James     2014-01-31  2014-01-05     -7823.14                0               26
James     2014-01-31  2014-01-24       308.00                7                0
Rod       2014-01-31  2014-01-17     -2603.95                0                0
Lisa      2014-01-31  2014-01-17     22019.49                0                0

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

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