?
Solved

Total for first day

Posted on 2014-01-03
3
Medium Priority
?
376 Views
Last Modified: 2014-01-04
I have an access query that includes a row for every invoice built. Among other columns, it has an CustomerID (unique to every customer), DateInvoiceBuilt (the exact date the invoice was built), and Total Invoice Amount.

I would like to have a query that will give me the average each customer was billed for on the first day an invoice was made.

For example, if JohnDoe has three invoices: two created on Jan 1st for $200 each and one on January 10th for $500. The query would give me the average of the two created on January 1st ... which would be $200. The January 10th wasn't created on the first day, so it would not be included in the data.

Any ideas? Thanks!
0
Comment
Question by:cansevin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 48

Expert Comment

by:Dale Fye
ID: 39755234
so, by "for the first day", do you mean the first day of the year, or the first day of each month, so you would have:

Name         Mon   Sum   Count   Avg
John Doe    Jan     400         2      200
John Doe    Feb    800         3      266.67

If this is correct, you might try something like:

Select CustomerName
         , Month([InvoiceDate]) as Mon
         , SUM([InvoiceAmt]) as Total
         , Count([InvoiceAmt]) as InvCount
         , Avg([InvoiceAmt]) as InvAvg
FROM yourTable
WHERE Year([InvoiceDate]) = 2013
AND Day([InvoiceDate]) = 1
GROUP BY CustomerName, Month([InvoiceDate])
0
 

Author Comment

by:cansevin
ID: 39755241
Sorry for the confusion... the "first day" is meant the day the first invoice was made for that particular clients. So every client would have their own "first day". If a client has 20 different invoices, with the first to being March 3rd, then it would average the two on March 3rd.

Hope that clears some fog. Thanks!
0
 
LVL 48

Accepted Solution

by:
Dale Fye earned 2000 total points
ID: 39755398
In that case, you need to start out by identifying the first day for each client.

SELECT CustomerID, Min(InvoiceDate) as FirstDay
FROM yourTable
GROUP BY CustomerID

Then, you would make that a subquery and join it back to your table, something like:

Select CustomerName
         , Month([InvoiceDate]) as Mon
         , SUM([InvoiceAmt]) as Total
         , Count([InvoiceAmt]) as InvCount
         , Avg([InvoiceAmt]) as InvAvg
FROM yourTable
INNER JOIN (
SELECT CustomerID, Min(InvoiceDate) as FirstDay
FROM yourTable
GROUP BY CustomerID
) as CustFirstDay
ON yourTable.CustomerID = CustFirstDay.CustomerID
AND yourTable.InvoiceDate = CustFirstDay.FirstDay
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question