Solved

Min() problems in Group By... Having

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

756 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