Re: Summary Query based on First Occurrence
- From: Jason Lepack <jlepack@xxxxxxxxx>
- Date: Tue, 12 Jun 2007 12:32:10 -0700
Fine then :p Try this:
SELECT
Q1.Account,
Q1.FirstInvoiceDate,
Sum(I.TotBill) AS SumFirstDate
FROM
(
SELECT
Account,
Min(InvoiceDate) AS FirstInvoiceDate
FROM
Invoices
GROUP BY
Account) AS Q1
INNER JOIN Invoices AS I
ON Q1.Account=I.Account
AND Q1.FirstInvoiceDate=I.InvoiceDate
GROUP BY
Q1.Account,
Q1.FirstInvoiceDate
Cheers,
Jason Lepack
On Jun 12, 2:43 pm, Dave Schoenbrun
<DaveSchoenb...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
Absolutely wonderful solution, with one problem I failed to mention....
The query has to be self contained. I cannot create objects on our LIVE
database. I can create the query in a DEVELOPMENT database and copy the
resultant SQL statement into a new query in LIVE, run it and copy the
results, but I can't save anything (such as a 2nd query) in LIVE.
Sorry for not mentioning that upfront, but I appreciate the response. It's
what I would do if I had the capabilities.
"Jason Lepack" wrote:
Is that invoice date a text field? Why not actually use a field that
would store dates? But anyways, it will still work the same.
I used a table called invoices with your table structure.
I created a query called Query1:
SELECT
Account,
Min(InvoiceDate) AS FirstInvoiceDate
FROM
Invoices
GROUP BY
Account
I then created a second query called Query2:
SELECT
Q1.Account,
Q1.FirstInvoiceDate,
Sum(I.TotBill) AS SumFirstDate
FROM
Query1 AS Q1
INNER JOIN Invoices AS I
ON Q1.Account=I.Account
AND Q1.FirstInvoiceDate=I.InvoiceDate
GROUP BY
Q1.Account,
Q1.FirstInvoiceDate
Cheers,
Jason Lepack
On Jun 12, 2:01 pm, Dave Schoenbrun
<DaveSchoenb...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
I've got a list of invoices for businesses, some which have multiple invoices
on a given day. I want to be able to return ONLY the First Day a customer
was invoiced AND the TOTAL for just that day. Here's what the data looks
like:
Account InvoiceDate TotBill
A B C Corp. 20041231 3500.00
A B C Corp. 20041231 9500.00
A B C Corp. 20050131 1500.00
A B C Corp. 20050131 2000.00
BARBERS INC. 20041431 1500.00
BARBERS INC. 20041431 12000.00
BARBERS INC. 20050131 300.00
BARBERS INC. 20050131 470.00
Ideally, my Query results would look like the following:
Account InvoiceDate TotBill
A B C Corp. 20041431 13000.00
BARBERS INC. 20041431 12150.00
Any Ideas?- Hide quoted text -
- Show quoted text -
.
- Follow-Ups:
- Re: Summary Query based on First Occurrence
- From: Jason Lepack
- Re: Summary Query based on First Occurrence
- References:
- Re: Summary Query based on First Occurrence
- From: Jason Lepack
- Re: Summary Query based on First Occurrence
- From: Dave Schoenbrun
- Re: Summary Query based on First Occurrence
- Prev by Date: Re: Week format
- Next by Date: Re: Query Not Working
- Previous by thread: Re: Summary Query based on First Occurrence
- Next by thread: Re: Summary Query based on First Occurrence
- Index(es):
Relevant Pages
|