seraphi
09-29-2006, 06:24 AM
Hello all,
i was wondering if you can help me with a problem i'm having with a MS SQL query.
The application holds customers, each customer has a balance, once their balance gets below $10 the customer is sent an email saying their balance is low, this email is then recorded into the emailLogs database table.
I'm trying to put together a report that lists all of the customers in the database that have been emailed a reminder email about their balance being low, and then showing their current balance. However the problem with my query is that if a customer has used their balance more than once during the date period then they will be listed that many times, as their is an email record for each time.
I need them to only be listed once in my table.
Here are the tables:
Users:
ID
CompanyName
Balance
CurrencyTypeID
EmailLog:
ID
Subject
Message
DateRecordCreated
And here is my query:
SELECT Users.ID as UserID, Users.CompanyName as CompanyName, convert(char(11), EmailLog.DateRecordCreated, 106) as DateRecordCreated, EmailLog.Message as EmailMessage, Users.Balance as Balance, Users.CurrencyTypeID as CurrencyTypeID
FROM EmailLog
LEFT OUTER JOIN Users ON Users.ID = EmailLog.UserID
WHERE Users.CurrencyTypeID=1 AND EmailLog.DateRecordCreated between convert(dateTime, '2006-09-1 00:00:00', 110) and convert(dateTime, '2006-09-29 23:59:59', 110) AND EmailLog.Subject LIKE '%Your account balance is low%'
ORDER BY EmailLog.DateRecordCreated DESC
How do i stop a customer being shown more than once?
Thanks in advance.
i was wondering if you can help me with a problem i'm having with a MS SQL query.
The application holds customers, each customer has a balance, once their balance gets below $10 the customer is sent an email saying their balance is low, this email is then recorded into the emailLogs database table.
I'm trying to put together a report that lists all of the customers in the database that have been emailed a reminder email about their balance being low, and then showing their current balance. However the problem with my query is that if a customer has used their balance more than once during the date period then they will be listed that many times, as their is an email record for each time.
I need them to only be listed once in my table.
Here are the tables:
Users:
ID
CompanyName
Balance
CurrencyTypeID
EmailLog:
ID
Subject
Message
DateRecordCreated
And here is my query:
SELECT Users.ID as UserID, Users.CompanyName as CompanyName, convert(char(11), EmailLog.DateRecordCreated, 106) as DateRecordCreated, EmailLog.Message as EmailMessage, Users.Balance as Balance, Users.CurrencyTypeID as CurrencyTypeID
FROM EmailLog
LEFT OUTER JOIN Users ON Users.ID = EmailLog.UserID
WHERE Users.CurrencyTypeID=1 AND EmailLog.DateRecordCreated between convert(dateTime, '2006-09-1 00:00:00', 110) and convert(dateTime, '2006-09-29 23:59:59', 110) AND EmailLog.Subject LIKE '%Your account balance is low%'
ORDER BY EmailLog.DateRecordCreated DESC
How do i stop a customer being shown more than once?
Thanks in advance.
