Solved

Min() problems in Group By... Having

Posted on 2003-11-18
5
456 Views
Last Modified: 2007-12-19
I have a query that joins a couple of tables: invoice header and detail info. There are several fields of interest, including VendorName, InvBalance, InvDate.  There may be several entries in the detail table for an invoice as the balance gets paid off. I want to select the smallest balance remaining for any invoices with non-zero balances. I've tried using Min() with Group By and Having w/o much luck. Am I missing something or just going about it incorrectly?

This query returns all lines with AmountInvoiced > 0, not just the Min.

INSERT INTO POCash ( PONum, VendorName, ReqDate, InvBalance )
SELECT [PO2_PurchaseOrderEntryLine].[PurchaseOrderNumber], [PO1_PurchaseOrderEntryHeader].[VendorName], [PO2_PurchaseOrderEntryLine].[RequiredDate], Min([PO2_PurchaseOrderEntryLine].[AmountInvoiced]) AS MinOfAmountInvoiced
FROM PO1_PurchaseOrderEntryHeader INNER JOIN PO2_PurchaseOrderEntryLine ON [PO1_PurchaseOrderEntryHeader].[PurchaseOrderNumber]=[PO2_PurchaseOrderEntryLine].[PurchaseOrderNumber]
WHERE ((([PO1_PurchaseOrderEntryHeader].[AmountInvoiced])<>0) And (([PO1_PurchaseOrderEntryHeader].[OrderStatus])="C" Or ([PO1_PurchaseOrderEntryHeader].[OrderStatus])="N" Or ([PO1_PurchaseOrderEntryHeader].[OrderStatus])="O" Or ([PO1_PurchaseOrderEntryHeader].[OrderStatus])="B"))
GROUP BY [PO2_PurchaseOrderEntryLine].[PurchaseOrderNumber], [PO1_PurchaseOrderEntryHeader].[VendorName], [PO2_PurchaseOrderEntryLine].[RequiredDate]
HAVING Min(PO2_PurchaseOrderEntryLine.AmountInvoiced) > 0
ORDER BY [PO1_PurchaseOrderEntryHeader].[VendorName];

Thanks!
Don
0
Comment
Question by:strider235
5 Comments
 
LVL 1

Expert Comment

by:bcad
ID: 9774506
Once you have your query created, have you considered building a report based on it?  Then you can group by invoice, and have your Min function be performed in the group footer.  If that sounds like something you might want to persue, and you need more details on creating the report, just let me know...
0
 
LVL 2

Accepted Solution

by:
cbail earned 125 total points
ID: 9774758
Try something like this.
I could not test it, but I think it will do what you want. The inner query is the key here.

INSERT INTO POCash ( PONum, VendorName, ReqDate, InvBalance )
SELECT [PO2_PurchaseOrderEntryLine].[PurchaseOrderNumber], [PO1_PurchaseOrderEntryHeader].[VendorName], [PO2_PurchaseOrderEntryLine].[RequiredDate], [PO2_PurchaseOrderEntryLine].[AmountInvoiced]) FROM PO1_PurchaseOrderEntryHeader INNER JOIN PO2_PurchaseOrderEntryLine ON [PO1_PurchaseOrderEntryHeader].[PurchaseOrderNumber]=[PO2_PurchaseOrderEntryLine].[PurchaseOrderNumber]
WHERE ((([PO1_PurchaseOrderEntryHeader].[AmountInvoiced])<>0) And (([PO1_PurchaseOrderEntryHeader].[OrderStatus])="C" Or ([PO1_PurchaseOrderEntryHeader].[OrderStatus])="N" Or ([PO1_PurchaseOrderEntryHeader].[OrderStatus])="O" Or ([PO1_PurchaseOrderEntryHeader].[OrderStatus])="B")) AND [PO2_PurchaseOrderEntryLine].[AmountInvoiced]) IN (SELECT Min([PO2_PurchaseOrderEntryLine].[AmountInvoiced]) FROM PO1_PurchaseOrderEntryHeader INNER JOIN PO2_PurchaseOrderEntryLine ON [PO1_PurchaseOrderEntryHeader].[PurchaseOrderNumber]=[PO2_PurchaseOrderEntryLine].[PurchaseOrderNumber]
WHERE ((([PO1_PurchaseOrderEntryHeader].[AmountInvoiced])<>0) And (([PO1_PurchaseOrderEntryHeader].[OrderStatus])="C" Or ([PO1_PurchaseOrderEntryHeader].[OrderStatus])="N" Or ([PO1_PurchaseOrderEntryHeader].[OrderStatus])="O" Or ([PO1_PurchaseOrderEntryHeader].[OrderStatus])="B")) AND [PO2_PurchaseOrderEntryLine].[AmountInvoiced])  ORDER BY [PO1_PurchaseOrderEntryHeader].[VendorName];

Chris
0
 
LVL 9

Expert Comment

by:ornicar
ID: 10064328
----------------------------------------------------------------------------------------
This question has been abandoned and needs to be finalized.
 You can accept an answer, split the points, or get a refund (information at http:/help.jsp#hs5)
  If you need a moderator to help you, post a question at Community Support (http:/Community_Support/)

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

ornicar
Cleanup Volunteer

---------------------------------------------------------------------------------------------
0
 
LVL 39

Expert Comment

by:stevbe
ID: 10103706
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: cbail {http:#9774758}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

stevbe
EE Cleanup Volunteer
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now