Solved

Min() problems in Group By... Having

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

911 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

17 Experts available now in Live!

Get 1:1 Help Now