简体   繁体   中英

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.

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. Those customers with only one Pstng_date should display as zero

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]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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