Reply
Im Stuck with this query please help
Old 05-25-2007, 12:12 AM Im Stuck with this query please help
Skilled Talker

Posts: 51
Name: Paul
Location: South Africa
I have a site that is working and want to add an administration section to make my life easier, I need to know which customers are due to be invoiced, determined by if the client is new and never been invoiced and the account is more than 10 days old from the date of joining, also if the client is old and already been invoiced one or more times I need to get the last invoice date and add 1 month to that.
I have 2 tables one is (tblcustomers, these columns are what im working with CustID{Primary Key}, DateJoined) the secod table is (tblinvoice, these are all the columns (InvoiceNo{Primary Key, Auto Increment}, AmountNet, AmountVAT, Balace, AmountPaid, DateInvoiced, CustID{Foreign Key}, DatePaid)
Here is the SQl query so far

SELECT cu.CompanyName, inv.InvNo
FROM tblcustomers cu LEFT JOIN tblinvoice inv USING (CustID)
WHERE cu.CompanyName NOT LIKE '%demo%' AND
cu.DateJoined < (CURDATE() - INTERVAL 10 DAY) AND
cu.TermsID='Monthly' AND
(inv.DateInvoiced IS NULL OR inv.DateInvoiced < (CURDATE() - INTERVAL 1 MONTH)))

The problem with this is that If a customer has lets say 4 invoices and they are all 1 month apart under different invoice numbers the first is from January invoice number 1, the second from February invoice number 2, the third from March invoice number 3, the fourth from April invoice number 4, now the account is due to have the 5th invoice for May, but the query returns all the results and I only want the last invoice to show and only if it is 1 month or older

In the end of the day I need a list of accounts 1 month or older that are due to be invoiced regardless of if they have been invoiced before or not without all the duplicate entries and the DISTINCTquery will not let me specify only MAX invoice Numbers.

Any suggestions, I hope it is clear enough to understand.
scorpioserve is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 05-27-2007, 03:15 AM Re: Im Stuck with this query please help
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 953
Name: Jeremy Miller
Location: Reno, NV
If you make the added assumption that you have not ever missed an invoice (so there do not exist ANY invoices older than 2 months), you could do

Code:
(inv.DateInvoiced IS NULL OR (inv.DateInvoiced > (CURDATE() - INTERVAL 2 MONTH) AND inv.DateInvoiced < (CURDATE() - INTERVAL 1 MONTH)))
for
Code:
(inv.DateInvoiced IS NULL OR inv.DateInvoiced < (CURDATE() - INTERVAL 1 MONTH))
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-28-2007, 01:52 AM Re: Im Stuck with this query please help
Skilled Talker

Posts: 51
Name: Paul
Location: South Africa
I will give it a try and let you know the results, thanks for looking into this
scorpioserve is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Im Stuck with this query please help
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.11333 seconds with 13 queries