Solved

Min() problems in Group By... Having

Posted on 2003-11-18
5
461 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
[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
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

737 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