?
Solved

group by query advice

Posted on 2018-02-14
6
Medium Priority
?
17 Views
Last Modified: 2018-02-14
I need a way to identify records in a table. There are 3 key columns of interest, one is "invoice number", one is "amount", and one is "supplier ID". I need a way of grouping records where invoice number and amount are the same for as many rows as that is the case, and then to filter to identify instances where there are more than one supplier ID against the identical invoice number and amount.
0
Comment
Question by:pma111
  • 3
  • 2
6 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 42468698
See if this gives you a count of suppliers for Invoice/Amount combinations:

SELECT  InvoiceNumber, Amount, 
(SELECT COUNT(SupplierID)  FROM YourTable b WHERE b.InvoiceNumber = a.InvoiceNumber AND b.Amount = a.Amount)  As NumberOfSuppliers
FROM YourTable a

Open in new window

0
 
LVL 3

Author Comment

by:pma111
ID: 42468711
that gives a syntax error in access and highlights "(SELECT COUNT(SupplierID)  FROM YourTable b WHERE b.InvoiceNumber = a.InvoiceNumber AND b.Amount = a.Amount)  As NumberOfSuppliers
FROM YourTable a"
0
 
LVL 3

Author Comment

by:pma111
ID: 42468720
and I did replace fieldnames/table names to our ones.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 61

Expert Comment

by:mbizup
ID: 42468740
Can you post your SQL?  (what I posted works for me syntactically)...
0
 
LVL 61

Accepted Solution

by:
mbizup earned 2000 total points
ID: 42468749
Actually... Try this instead.  My last post wont give you distinct suppliers.  Make sure that any field or table names containing spaces or special characters are enclosed in square brackets: [table Name].[Field Name With Spaces]

SELECT Count(q.SupplierID) AS DistinctSuppliers, q.InvoiceNumber, q.amount
FROM (SELECT DISTINCT a.SupplierID, a.InvoiceNumber, a.Amount
FROM Table1 AS a)  AS q
GROUP BY q.InvoiceNumber, q.amount;

Open in new window

0
 
LVL 5

Expert Comment

by:Ares Kurklu
ID: 42468751
Try this:
SELECT invoiceNo, Count(SupplierId) AS CountOfSupplierId, max(SupplierId) AS SupId, Amount
FROM Table
GROUP BY invoiceNo, Amount, SupplierId;

Open in new window

1

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.

Join & Write a Comment

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
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…

601 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