Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



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

Posts: 116
Name: Paul
Location: South Africa
Trades: 0
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
 
 
Register now for full access!
Old 05-27-2007, 04:15 AM Re: Im Stuck with this query please help
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
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

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-28-2007, 02:52 AM Re: Im Stuck with this query please help
Super Talker

Posts: 116
Name: Paul
Location: South Africa
Trades: 0
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 Search this Thread
Search this Thread:

Advanced Search

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

BB 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.66471 seconds with 12 queries