Foamee beer or coffee for more help

Hello

I hope someone can point me in the right direction on a SQL query writing. Sorry if this is getting a bit long but I want to provide enough information to get help I need.

Here's the skinny:

I wrote a CRM system that allow users to keep their list of clients (names and mailing address, etc.). Every 2 months (Consultants) users can connect and order/send their clients (Customers) personalized mailing brochures where brochures to get a personal message printed on them to the sender.

I have a MySQL database with 4 tables.

Table 1: Consultants - registered users
Table 2: Customers - registered users
Table 3: Messages
Table 4: ConsultantOrders

The problem is that a Consultant can order more than one during each cycle.

So I want to be able to do is query the DB, find all the consultants who have placed an order - but only pull THE MOST RECENT order as the newOrder - and then extract customers all advisers to compile a master merge.

What I'm doing right now is first run a query to find consultants have several orders of the current cycle. Then I go and manually delete duplicate orders. Once it's done I then run this query:

SELECT DISTINCT
'ConsultantOrders '. "ConsultantID,"
'ConsultantOrders '. "OrderID"
"Consultants". "ConsultantID,"
"Consultants". "First name",
"Consultants". "LastName."
"Consultants". "Address1"
"Consultants". "Address2"
"Consultants". 'City ',.
"Consultants". "State":
"Consultants". "Zip,"
"Consultants". "Phone."
"Consultants". "SecondaryPhone,"
"' Consultants." EmailAddress',.
"Consultants". "Web site."
"Consultants". 'MessageID '.
"Consultants". "Authorized."
'Clients '. "First name",
'Clients '. 'Name',
'Clients '. "Address1"
'Clients '. "Address2"
'Clients '. 'City ',.
'Clients '. "State":
'Clients '. "Zip,"
'Clients '. "Language."
'Clients '. "Mail."
'Clients '. "ConsultantID,"
"Messages". 'MessageID '.
Message "Messages." "
Of
"ConsultantOrders,"
"Consultants".
Inner Join 'Customers' ON "=",
"Messages".
WHERE
"Consultants." ConsultantID' = ConsultantID of the 'ConsultantOrders '. ' AND
ConsultantID 'Customers.' ' = ConsultantID of the 'ConsultantOrders '. ' AND
Mail 'Customers.' '= ' 1' AND
MessageID "Consultants." "="Messages." MessageID' AND
Status 'Customers.' '= ' 1' AND
'Advisers.' allowed '= ' 1' AND
'ConsultantOrders '. "OrderID" > 2512

If someone can tell me how I would write a direct query to do not have to manually delete duplicate orders?

I think I need to use a function (Max), but for the life of me, I can't make it work right.

Any help would be greatly appreciated. Beer or coffee through foamee.com as a reward?

See you soon,.

Eric

AWESOME!

It worked like a charm. I just had to add a 'ON' in the first line INNER JOIN and it worked perfectly. Thanks to a bouquet.

I will send you a private message as well for a "Thank you" gift

See you soon,.

Eric

Tags: ColdFusion

Similar Questions

Maybe you are looking for