Solved

Min() problems in Group By... Having

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

726 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