Skip to Content

Assistance with SQL query

Hi Experts

I am trying to get query to return list of customer visits due to be made (due date from UDF in CRD1) alongside last date this customer was visited (last visit date should be most recent date taken from OCLG.Recontact where activities is of 'Visit to Customer' type.

This query is causing error.

I would be grateful for help to make it work

Regards,

Karen

SELECT T0.[CardName], T1.Address, T1.U_Territory, T1.U_NxtVisitDue, MAX(T2.Recontact) AS 'Last Visit Date'

FROM OCRD T0 INNER JOIN CRD1 T1 on T1.CardCode=T0.CardCode

WHERE DateDiff(dd,GetDate(), T1.U_NxtVisitDue) <0

INNER JOIN OCLG T2 on T2.CardCode=T0.CardCode where T2.CntctType='Visit to Customer'

FOR BROWSE

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Sep 28, 2014 at 11:22 AM

    Hi

    you cant write "where" before the inner join and you have the "where" twice

    try this:

    SELECT T0.[CardName], T1.Address, T1.U_Territory, T1.U_NxtVisitDue, MAX(T2.Recontact) AS 'Last Visit Date'

    FROM OCRD T0 INNER JOIN CRD1 T1 on T1.CardCode=T0.CardCode
    INNER JOIN OCLG T2 on T2.CardCode=T0.CardCode

    WHERE DateDiff(dd,GetDate(), T1.U_NxtVisitDue) <0

    and T2.CntctType='Visit to Customer'

    FOR BROWSE

    shachar

    Add a comment
    10|10000 characters needed characters exceeded

    • Hi Johan,

      My problem query was only returning customers where there is an entry in CRD1.U_NxtVisitDue AND where there is an Activity Type 21 in existance.

      I have not yet sufficient knowledge of types of JOIN to fully understand below solution.

      Thanks for your assistance

      Regards,

      Karen

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.