Welcome Guest Search | Active Topics | Log In | Register

Date Comparisons Options
Chris Bray
#1 Posted : Wednesday, February 03, 2010 5:18:45 AM
Rank: Team VistaDB

Groups: Member, Team VistaDB

Joined: 8/13/2006
Posts: 393
Points: 657
Location: Seaford, East Sussex, UK
Hi Guys,

I am trying to calculate ageing on transactions, but whilst it produces the correct total it seems not to work correctly displaying some columns as zero when I know that there are entries. I have tried using BETWEEN and DATEDIFF and cannot get either to work correctly.

I am passing in the end of day for the @CheckDate:

Code:


    parameter = command.CreateParameter();
    parameter.ParameterName = "@LinkTypeId";
    parameter.Value = (int)ListType.Customers;
    parameter.DbType = DbType.Int32;
    command.Parameters.Add(parameter);

    parameter = command.CreateParameter();
    parameter.ParameterName = "@CheckDate";
    parameter.Value = DateUtils.GetEndOfDay(checkDate);
    parameter.DbType = DbType.DateTime;
    command.Parameters.Add(parameter);

    parameter = command.CreateParameter();
    parameter.ParameterName = "@SalesInvoiceTransactionTypeId";
    parameter.Value = (int)TransactionType.SalesInvoice;
    parameter.DbType = DbType.Int32;
    command.Parameters.Add(parameter);

    parameter = command.CreateParameter();
    parameter.ParameterName = "@SalesCreditTransactionTypeId";
    parameter.Value = (int)TransactionType.SalesCredit;
    parameter.DbType = DbType.Int32;
    command.Parameters.Add(parameter);

    command.CommandText = @"
        SELECT AccountName, CurrentPeriod, ThirtyDays, SixtyDays, NinetyDays, Older, Total FROM
        (SELECT Customers.[AccountName],
        SUM(CASE WHEN  DocHeader.[DueDate]  > @CheckDate THEN (COALESCE(CurrencyDebitAmount, 0)-Coalesce(CurrencyCreditAmount,0)) ELSE 0 END)  AS CurrentPeriod,
        SUM(CASE WHEN  DocHeader.[DueDate]  BETWEEN @CheckDate AND @CheckDate-30 THEN  (COALESCE(CurrencyDebitAmount, 0)-Coalesce(CurrencyCreditAmount,0)-(GeneralJournal.[CurrencyAmountPaid]+GeneralJournal.[CurrencyAmountAllocated]))  ELSE 0 END)  AS ThirtyDays,
        SUM(CASE WHEN  DocHeader.[DueDate]  BETWEEN @CheckDate-31 AND @CheckDate-60 THEN  (COALESCE(CurrencyDebitAmount, 0)-Coalesce(CurrencyCreditAmount,0)-(GeneralJournal.[CurrencyAmountPaid]+GeneralJournal.[CurrencyAmountAllocated]))  ELSE 0 END)  AS SixtyDays,
        SUM(CASE WHEN  DocHeader.[DueDate]  BETWEEN @CheckDate-61 AND @CheckDate-90 THEN  (COALESCE(CurrencyDebitAmount, 0)-Coalesce(CurrencyCreditAmount,0)-(GeneralJournal.[CurrencyAmountPaid]+GeneralJournal.[CurrencyAmountAllocated]))  ELSE 0 END)  AS NinetyDays,
        SUM(CASE WHEN  DocHeader.[DueDate]  < @CheckDate-90 THEN  (COALESCE(CurrencyDebitAmount, 0)-Coalesce(CurrencyCreditAmount,0)-(GeneralJournal.[CurrencyAmountPaid]+GeneralJournal.[CurrencyAmountAllocated]))  ELSE 0 END)  AS Older,
        SUM(ABS(COALESCE(GeneralJournal.[CurrencyDebitAmount], 0) -Coalesce(GeneralJournal.[CurrencyCreditAmount], 0)) - (GeneralJournal.[CurrencyAmountPaid]+GeneralJournal.[CurrencyAmountAllocated]) ) AS Total
        FROM GeneralJournal
        LEFT JOIN Customers ON GeneralJournal.[LinkId] = Customers.[CustomerId]
        LEFT JOIN DocHeader ON GeneralJournal.TransactionId = DocHeader.TransactionId
        WHERE GeneralJournal.[MasterRecord] AND GeneralJournal.[LinkTypeId] = @LinkTypeId
        AND (GeneralJournal.[TransactionTypeId] = @SalesInvoiceTransactionTypeId
        OR GeneralJournal.[TransactionTypeId] = @SalesCreditTransactionTypeId)
        AND
        ABS(COALESCE(GeneralJournal.[CurrencyDebitAmount], 0) - Coalesce(GeneralJournal.[CurrencyCreditAmount], 0)) - (GeneralJournal.[CurrencyAmountPaid]+GeneralJournal.[CurrencyAmountAllocated]) != 0
        GROUP BY Customers.AccountName)
        ORDER BY AccountName;
        ";


Can anyone suggest where I am going wrong and how I can make this work consistently?
Chris Bray
Vertical Software
http://www.verticalsoftware.co.uk
js_vistadb
#2 Posted : Thursday, February 04, 2010 2:48:57 PM
Rank: VistaDB Staff

Groups: Administration, VistaDB Staff

Joined: 8/13/2006
Posts: 3,633
Points: 5,124
Location: Mount Dora, Florida
In general you have to flatten the datetimes to include a 00:00:00 for the time.

targetdatetime >= '2009-01-01 00:00:00' and < '2009-01-02 00:00:00'

That will give everything for a specific day. You can't compare the datetime with something like getdate.add some number because it will include the time component. And if all your fields have a time component, you would either have to flatten them first, or change your compares to work with the entire time range. Flattening each value in a row is a lot more expensive (in general).

This is a pretty common use case with SQL Server for needing date range tests. When you get to grouping on dates and want them all to flatten to just by date and ignore the time it gets more complicated. I think that was why Microsoft finally implemented date and time as column types, to make these types of things easier.

Searching for SQL Server date range comparison should give some examples.


Jason Short

davidmccallum
#3 Posted : Friday, February 05, 2010 2:09:10 AM
Rank: Team VistaDB

Groups: Member, Team VistaDB

Joined: 8/13/2006
Posts: 608
Points: 2,583
Location: Edinburgh, Scotland
Chris,
We do something similar except we use a StartOfDay and EndOfDay methods to return '2009-01-01 00:00:00' and '2009-01-02 23:59:59'
Code:
private static DateTime EndOfDay(DateTime dateTime)
{
     return StartOfDay(dateTime).AddDays(1).AddMilliseconds(-1);
}

private static DateTime StartOfDay(DateTime dateTime)
{
     return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day);
}

then do a BETWEEN using these two values.
Chris Bray
#4 Posted : Friday, February 05, 2010 3:06:48 AM
Rank: Team VistaDB

Groups: Member, Team VistaDB

Joined: 8/13/2006
Posts: 393
Points: 657
Location: Seaford, East Sussex, UK
Hi David and Jason,

Umm.... Is it me? I am really sorry if I continue to be unclear, but I thought that I had covered it in the initial question when I said:

Quote:
I am passing in the end of day for the @CheckDate:


As indicated in the original question the columns are DateTime, and I am comparing with dd/mm/yyyy 23:59:59 - is this not what you are both suggesting?

Are you suggesting something else that I am not understanding?
Chris Bray
Vertical Software
http://www.verticalsoftware.co.uk
davidmccallum
#5 Posted : Friday, February 05, 2010 10:17:29 AM
Rank: Team VistaDB

Groups: Member, Team VistaDB

Joined: 8/13/2006
Posts: 608
Points: 2,583
Location: Edinburgh, Scotland
Chris,

Can I clarify, are you seeing info in the total column for which there is no (or incorrect) data in aged columns?

If yes, remove the aged columns that should be zero (for testing) and simpolify the CASE condition (you should now have someshing like
Code:
SUM(( CASE WHEN  DocHeader.[DueDate]  BETWEEN @CheckDate AND @CheckDate-30 THEN
        COALESCE(CurrencyDebitAmount, 0)
        -Coalesce(CurrencyCreditAmount,0)
        -GeneralJournal.[CurrencyAmountPaid]
        -GeneralJournal.[CurrencyAmountAllocated]
      END
    ))  AS ThirtyDays

then check the values.

NOTE if "GeneralJournal.[CurrencyAmountPaid]" or "GeneralJournal.[CurrencyAmountAllocated]" is null the result will be null. Also I have removed the brackets surrounding these fields and adjusted the arithmetic symbols as appropriate.

One small criticism, the inner SQL groups on "Customers.[AccountName]" if this column is not the primary key, nor a unique index on the Customers table, then tere is a possiblity of duplicate records (Companys with the same name) giving incorrect results. I don't think this is the reason for your errors, but it is open to abuse.
Chris Bray
#6 Posted : Friday, February 05, 2010 11:18:22 AM
Rank: Team VistaDB

Groups: Member, Team VistaDB

Joined: 8/13/2006
Posts: 393
Points: 657
Location: Seaford, East Sussex, UK
Hi David,

I sometimes get an answer, and sometimes not... In the first few items I have a total, but no entries in any of the ageing columns which is obviously wrong. In others I have a figure in some columns.

If I change the check date to 31/12/2009 then the initial test entry appears in the first column. If I set it at 31/01/10 then it does not appear in any columns.

I used End Of Day with Between because it is inclusive, intending that this would give the correct dates. Perhaps I have messed up on the algorithm but as far as I can see it ought to work! I just wondered if this was the problem, given that changing the date brings it into current but it does not appear in 30 days.

In relation to the other issues you raise:

• Removing the other columns makes no difference.
• AmountPaid and AmountAllocated are non-nullable columns, hence my not bothering to colaesce them.
• Customers.[AccountName] is indexed uniquely, hopefully removing the possibility of duplicates.

If you wish I can email you sample data and schema.

Chris Bray
Vertical Software
http://www.verticalsoftware.co.uk
Chris Bray
#7 Posted : Friday, February 05, 2010 11:32:12 AM
Rank: Team VistaDB

Groups: Member, Team VistaDB

Joined: 8/13/2006
Posts: 393
Points: 657
Location: Seaford, East Sussex, UK
Hi David,

In case it helps, swapping to your case condition comes back with an empty column, whereas mine comes back with zero...

Chris Bray
Vertical Software
http://www.verticalsoftware.co.uk
davidmccallum
#8 Posted : Friday, February 05, 2010 1:26:28 PM
Rank: Team VistaDB

Groups: Member, Team VistaDB

Joined: 8/13/2006
Posts: 608
Points: 2,583
Location: Edinburgh, Scotland
Chris,

email the data needed to replicate the problem along with any paramter values needed
davidmccallum
#9 Posted : Saturday, February 06, 2010 2:35:37 AM
Rank: Team VistaDB

Groups: Member, Team VistaDB

Joined: 8/13/2006
Posts: 608
Points: 2,583
Location: Edinburgh, Scotland
Chris,

It looks like the BETWEEN function, the SQL
Code:
SELECT AccountName, CurrentPeriod, ThirtyDays, SixtyDays, NinetyDays, Older, Total FROM
(SELECT Customers.[AccountName],
SUM(CASE WHEN DocHeader.[DueDate] > @CHECKDATE THEN (COALESCE(CurrencyDebitAmount, 0)-Coalesce(CurrencyCreditAmount,0)) ELSE 0 END) AS CurrentPeriod,
SUM(CASE WHEN DocHeader.[DueDate]>(@CHECKDATE-30) AND duedate<@CHECKDATE THEN (COALESCE(CurrencyDebitAmount, 0)-Coalesce(CurrencyCreditAmount,0)-(GeneralJournal.[CurrencyAmountPaid]+GeneralJournal.[CurrencyAmountAllocated])) ELSE 0 END) AS ThirtyDays,
SUM(CASE WHEN DocHeader.[DueDate]>(@CHECKDATE-31) AND duedate<(@CHECKDATE-60) THEN (COALESCE(CurrencyDebitAmount, 0)-Coalesce(CurrencyCreditAmount,0)-(GeneralJournal.[CurrencyAmountPaid]+GeneralJournal.[CurrencyAmountAllocated])) ELSE 0 END) AS SixtyDays,
SUM(CASE WHEN DocHeader.[DueDate]>(@CHECKDATE-61) AND duedate<(@CHECKDATE-120) THEN (COALESCE(CurrencyDebitAmount, 0)-Coalesce(CurrencyCreditAmount,0)-(GeneralJournal.[CurrencyAmountPaid]+GeneralJournal.[CurrencyAmountAllocated])) ELSE 0 END) AS NinetyDays,
SUM(CASE WHEN DocHeader.[DueDate] < (@CHECKDATE-90) THEN (COALESCE(CurrencyDebitAmount, 0)-Coalesce(CurrencyCreditAmount,0)-(GeneralJournal.[CurrencyAmountPaid]+GeneralJournal.[CurrencyAmountAllocated])) ELSE 0 END) AS Older,
SUM(ABS(COALESCE(GeneralJournal.[CurrencyDebitAmount], 0) -Coalesce(GeneralJournal.[CurrencyCreditAmount], 0)) - (GeneralJournal.[CurrencyAmountPaid]+GeneralJournal.[CurrencyAmountAllocated]) ) AS Total
FROM GeneralJournal
LEFT JOIN Customers ON GeneralJournal.[LinkId] = Customers.[CustomerId]
LEFT JOIN DocHeader ON GeneralJournal.TransactionId = DocHeader.TransactionId
WHERE GeneralJournal.[MasterRecord] AND GeneralJournal.[LinkTypeId] = 9
AND (GeneralJournal.[TransactionTypeId] = 13
OR GeneralJournal.[TransactionTypeId] = 14)
AND
ABS(COALESCE(GeneralJournal.[CurrencyDebitAmount], 0) - Coalesce(GeneralJournal.[CurrencyCreditAmount], 0)) - (GeneralJournal.[CurrencyAmountPaid]+GeneralJournal.[CurrencyAmountAllocated]) != 0
GROUP BY Customers.AccountName)
ORDER BY AccountName;

seems to work OK, tho' it need checked for accuracy.

I would raise a ticket for this, sending the data you sent me.
Chris Bray
#10 Posted : Saturday, February 06, 2010 5:25:55 AM
Rank: Team VistaDB

Groups: Member, Team VistaDB

Joined: 8/13/2006
Posts: 393
Points: 657
Location: Seaford, East Sussex, UK
Hi David,

Ticket raised, as you suggested.

BETWEEN is inclusive, so the query needed a little work to get the right results - basically the 31 and 61 days were no longer needed and an equality operator had to be added. As far as I can tell the following now works correctly:

Code:
SELECT AccountName, CurrentPeriod, ThirtyDays, SixtyDays, NinetyDays, Older, Total FROM
(SELECT Customers.[AccountName],
SUM(CASE WHEN DocHeader.[DueDate] > @CHECKDATE THEN (COALESCE(CurrencyDebitAmount, 0)-Coalesce(CurrencyCreditAmount,0)) ELSE 0 END) AS CurrentPeriod,
SUM(CASE WHEN DocHeader.[DueDate] >(@CHECKDATE-30) AND DocHeader.[DueDate] <= @CHECKDATE THEN (COALESCE(CurrencyDebitAmount, 0)-Coalesce(CurrencyCreditAmount,0)-(GeneralJournal.[CurrencyAmountPaid]+GeneralJournal.[CurrencyAmountAllocated])) ELSE 0 END) AS ThirtyDays,
SUM(CASE WHEN DocHeader.[DueDate] >(@CHECKDATE-60) AND DocHeader.[DueDate] <= (@CHECKDATE-30) THEN (COALESCE(CurrencyDebitAmount, 0)-Coalesce(CurrencyCreditAmount,0)-(GeneralJournal.[CurrencyAmountPaid]+GeneralJournal.[CurrencyAmountAllocated])) ELSE 0 END) AS SixtyDays,
SUM(CASE WHEN DocHeader.[DueDate] >(@CHECKDATE-90) AND DocHeader.[DueDate] <= (@CHECKDATE-60) THEN (COALESCE(CurrencyDebitAmount, 0)-Coalesce(CurrencyCreditAmount,0)-(GeneralJournal.[CurrencyAmountPaid]+GeneralJournal.[CurrencyAmountAllocated])) ELSE 0 END) AS NinetyDays,
SUM(CASE WHEN DocHeader.[DueDate] <= (@CHECKDATE-90) THEN (COALESCE(CurrencyDebitAmount, 0)-Coalesce(CurrencyCreditAmount,0)-(GeneralJournal.[CurrencyAmountPaid]+GeneralJournal.[CurrencyAmountAllocated])) ELSE 0 END) AS Older,
SUM(ABS(COALESCE(GeneralJournal.[CurrencyDebitAmount], 0) -Coalesce(GeneralJournal.[CurrencyCreditAmount], 0)) - (GeneralJournal.[CurrencyAmountPaid]+GeneralJournal.[CurrencyAmountAllocated]) ) AS Total
FROM GeneralJournal
LEFT JOIN Customers ON GeneralJournal.[LinkId] = Customers.[CustomerId]
LEFT JOIN DocHeader ON GeneralJournal.TransactionId = DocHeader.TransactionId
WHERE GeneralJournal.[MasterRecord] AND GeneralJournal.[LinkTypeId] = 9
AND (GeneralJournal.[TransactionTypeId] = 13
OR GeneralJournal.[TransactionTypeId] = 14)
AND
ABS(COALESCE(GeneralJournal.[CurrencyDebitAmount], 0) - Coalesce(GeneralJournal.[CurrencyCreditAmount], 0)) - (GeneralJournal.[CurrencyAmountPaid]+GeneralJournal.[CurrencyAmountAllocated]) != 0
GROUP BY Customers.AccountName)
ORDER BY AccountName;


Naturally there will be more testing to do, but all tests so far have been positive. Once that is done I can use the same pattern to update all the other queries of the same type in the application and we should be home and dry. Thanks again for your help.

Chris Bray
Vertical Software
http://www.verticalsoftware.co.uk
davidmccallum
#11 Posted : Sunday, February 07, 2010 4:32:11 AM
Rank: Team VistaDB

Groups: Member, Team VistaDB

Joined: 8/13/2006
Posts: 608
Points: 2,583
Location: Edinburgh, Scotland
Chris,

One isty bitsy thing, just the perfectionist in me. Some o you calculations consist of A-B-(C+D), I think it would be more efficient to use A-B-C-D

Also if the total column is the sum of the previous column, it would be better to calculate it outside the sub-select, so the final SQL statement would be
Code:
SELECT     AccountName,
        CurrentPeriod,
        ThirtyDays,
        SixtyDays,
        NinetyDays,
        Older,
        (CurrentPeriod+ThirtyDays+SixtyDays+NinetyDays+Older) AS Total FROM
(    SELECT     Customers.[AccountName],
            SUM(    CASE WHEN DocHeader.[DueDate] > @CHECKDATE THEN
                        (COALESCE(CurrencyDebitAmount, 0)-Coalesce(CurrencyCreditAmount,0))
                    ELSE
                        0
                    END
                ) AS CurrentPeriod,
            SUM(    CASE WHEN DocHeader.[DueDate]>(@CHECKDATE-30) AND duedate<@CHECKDATE THEN
                        (COALESCE(CurrencyDebitAmount, 0)-Coalesce(CurrencyCreditAmount,0)-GeneralJournal.[CurrencyAmountPaid]-GeneralJournal.[CurrencyAmountAllocated])
                    ELSE
                        0
                    END
                ) AS ThirtyDays,
            SUM(    CASE WHEN DocHeader.[DueDate]>(@CHECKDATE-31) AND duedate<(@CHECKDATE-60) THEN
                        (COALESCE(CurrencyDebitAmount, 0)-Coalesce(CurrencyCreditAmount,0)-GeneralJournal.[CurrencyAmountPaid]-GeneralJournal.[CurrencyAmountAllocated])
                    ELSE
                        0
                    END
                ) AS SixtyDays,
            SUM(    CASE WHEN DocHeader.[DueDate]>(@CHECKDATE-61) AND duedate<(@CHECKDATE-120) THEN
                        (COALESCE(CurrencyDebitAmount, 0)-Coalesce(CurrencyCreditAmount,0)-GeneralJournal.[CurrencyAmountPaid]-GeneralJournal.[CurrencyAmountAllocated])
                    ELSE
                        0
                    END
                ) AS NinetyDays,
            SUM(    CASE WHEN DocHeader.[DueDate] < (@CHECKDATE-90) THEN
                        (COALESCE(CurrencyDebitAmount, 0)-Coalesce(CurrencyCreditAmount,0)-GeneralJournal.[CurrencyAmountPaid]-GeneralJournal.[CurrencyAmountAllocated])
                    ELSE
                        0
                    END
                ) AS Older,
            SUM(ABS(COALESCE(GeneralJournal.[CurrencyDebitAmount], 0) -Coalesce(GeneralJournal.[CurrencyCreditAmount], 0)) - GeneralJournal.[CurrencyAmountPaid]-GeneralJournal.[CurrencyAmountAllocated] ) AS Total
    FROM GeneralJournal
    LEFT JOIN Customers ON GeneralJournal.[LinkId] = Customers.[CustomerId]
    LEFT JOIN DocHeader ON GeneralJournal.TransactionId = DocHeader.TransactionId
    WHERE GeneralJournal.[MasterRecord] AND GeneralJournal.[LinkTypeId] = 9
    AND (GeneralJournal.[TransactionTypeId] = 13 OR GeneralJournal.[TransactionTypeId] = 14)
    AND ABS(COALESCE(GeneralJournal.[CurrencyDebitAmount], 0) - Coalesce(GeneralJournal.[CurrencyCreditAmount], 0)) - (GeneralJournal.[CurrencyAmountPaid]+GeneralJournal.[CurrencyAmountAllocated]) != 0
    GROUP BY Customers.AccountName
)
ORDER BY AccountName;
Chris Bray
#12 Posted : Monday, February 08, 2010 3:10:39 AM
Rank: Team VistaDB

Groups: Member, Team VistaDB

Joined: 8/13/2006
Posts: 393
Points: 657
Location: Seaford, East Sussex, UK
Hi David,

Thanks for the extra suggestions. I note in the code you have provided the equality check is missing and the total is being calculated both inside and outside the sub select - I take it these are typos rather than deliberate changes to the code?

Chris Bray
Vertical Software
http://www.verticalsoftware.co.uk
Chris Bray
#13 Posted : Monday, February 08, 2010 3:19:29 AM
Rank: Team VistaDB

Groups: Member, Team VistaDB

Joined: 8/13/2006
Posts: 393
Points: 657
Location: Seaford, East Sussex, UK
Hi David,

I see what happened - you used a previous one as your starting point. OK, I will revamp the working one I posted along your lines and see what happens. Thanks again.
Chris Bray
Vertical Software
http://www.verticalsoftware.co.uk
Chris Bray
#14 Posted : Monday, February 08, 2010 3:49:42 AM
Rank: Team VistaDB

Groups: Member, Team VistaDB

Joined: 8/13/2006
Posts: 393
Points: 657
Location: Seaford, East Sussex, UK
Hi David,

In reviewing the code you posted I noticed that I had missed the deduction of payments and credits on the CurrentPeriod which was a major drop-off and is now fixed. The Total should be the total of the columns, but I left it inside the sub select to allow me to do a comparison between the total calculated from the data and the total calculated from the columns. I will consider totalling the columns instead once I am totally confident of the results. I have taken on board replacing the second part of the calculation with all negatives as you suggested - thanks for that.

New code so far, which seems to work well:

Code:
DECLARE @CHECKDATE DateTime;
SET @CHECKDATE = '31/01/2010 23:59:59';

SELECT AccountName, CurrentPeriod, ThirtyDays, SixtyDays, NinetyDays, Older, Total FROM
(SELECT Customers.[AccountName],
SUM(CASE WHEN DocHeader.[DueDate] > @CHECKDATE THEN (COALESCE(CurrencyDebitAmount, 0) - Coalesce(CurrencyCreditAmount,0) - GeneralJournal.[CurrencyAmountPaid] - GeneralJournal.[CurrencyAmountAllocated]) ELSE 0 END) AS CurrentPeriod,
SUM(CASE WHEN DocHeader.[DueDate] >(@CHECKDATE-30) AND DocHeader.[DueDate]  <= @CHECKDATE THEN (COALESCE(CurrencyDebitAmount, 0) - Coalesce(CurrencyCreditAmount,0) - GeneralJournal.[CurrencyAmountPaid] - GeneralJournal.[CurrencyAmountAllocated]) ELSE 0 END) AS ThirtyDays,
SUM(CASE WHEN DocHeader.[DueDate] >(@CHECKDATE-60) AND DocHeader.[DueDate]  <= (@CHECKDATE-30) THEN (COALESCE(CurrencyDebitAmount, 0)- Coalesce(CurrencyCreditAmount,0) - GeneralJournal.[CurrencyAmountPaid] - GeneralJournal.[CurrencyAmountAllocated]) ELSE 0 END) AS SixtyDays,
SUM(CASE WHEN DocHeader.[DueDate] >(@CHECKDATE-90) AND DocHeader.[DueDate]  <= (@CHECKDATE-60) THEN (COALESCE(CurrencyDebitAmount, 0)- Coalesce(CurrencyCreditAmount,0) - GeneralJournal.[CurrencyAmountPaid] - GeneralJournal.[CurrencyAmountAllocated]) ELSE 0 END) AS NinetyDays,
SUM(CASE WHEN DocHeader.[DueDate] <= (@CHECKDATE-90) THEN (COALESCE(CurrencyDebitAmount, 0)-Coalesce(CurrencyCreditAmount,0) - GeneralJournal.[CurrencyAmountPaid] - GeneralJournal.[CurrencyAmountAllocated]) ELSE 0 END) AS Older,
SUM(ABS(COALESCE(GeneralJournal.[CurrencyDebitAmount], 0) - Coalesce(GeneralJournal.[CurrencyCreditAmount], 0)) - GeneralJournal.[CurrencyAmountPaid]-GeneralJournal.[CurrencyAmountAllocated]) AS Total
FROM GeneralJournal
LEFT JOIN Customers ON GeneralJournal.[LinkId] = Customers.[CustomerId]
LEFT JOIN DocHeader ON GeneralJournal.TransactionId = DocHeader.TransactionId
WHERE GeneralJournal.[MasterRecord] AND GeneralJournal.[LinkTypeId] = 9
AND (GeneralJournal.[TransactionTypeId] = 13
OR GeneralJournal.[TransactionTypeId] = 14)
AND
ABS(COALESCE(GeneralJournal.[CurrencyDebitAmount], 0) - Coalesce(GeneralJournal.[CurrencyCreditAmount], 0)) - GeneralJournal.[CurrencyAmountPaid]-GeneralJournal.[CurrencyAmountAllocated] != 0
GROUP BY Customers.AccountName)
ORDER BY AccountName;


Chris Bray
Vertical Software
http://www.verticalsoftware.co.uk
ms_vistadb
#15 Posted : Wednesday, February 10, 2010 2:05:44 PM
Rank: VistaDB Staff

Groups: Administration, VistaDB Staff

Joined: 7/5/2008
Posts: 252
Points: 1,776
Location: Mount Dora, Florida
Note:
I am posting the follow up the between issue here so anybody who searches this issue will know the proper answer. Also you can find more information on the issues and caveats of between through these stack overflow articles.

http://stackoverflow.com/questi...owest-and-highes/1022005
http://stackoverflow.com/questi.../sql-query-by-date-range

Chris,

I checked your query and the reason the betweens are not giving you the results you want is because the ranges are backwards. BETWEEN validates a low range AND a high range. Your query of DocHeader.[DueDate] BETWEEN @CheckDate AND (@CheckDate-30) was a high range and a low range. Thus between would never validate. This is the same behavior SQL server has, and even access.

Reversing the low and high values fixes this problem.

Michael Swain
VistaDB Software
Chris Bray
#16 Posted : Wednesday, February 10, 2010 2:33:56 PM
Rank: Team VistaDB

Groups: Member, Team VistaDB

Joined: 8/13/2006
Posts: 393
Points: 657
Location: Seaford, East Sussex, UK
Thanks Mike for the information - obviously something of which all contributors were unaware, and something to remember for the future.

As always, great support from VistaDB.

Chris Bray
Vertical Software
http://www.verticalsoftware.co.uk
davidmccallum
#17 Posted : Thursday, February 11, 2010 1:04:22 AM
Rank: Team VistaDB

Groups: Member, Team VistaDB

Joined: 8/13/2006
Posts: 608
Points: 2,583
Location: Edinburgh, Scotland
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAFPro Theme Created by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.3 | YAF © 2003-2009, Yet Another Forum.NET
This page was generated in 0.141 seconds.