Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 358
  • Last Modified:

Count values in a query

I have a query in which I query ten tables for cross reference information.

Each table has
Item_nr
Vendor_item_nr
Price

My master table has
Item_nr
30 other fields

My query selects Vendor_item_nr from each table where Item_Nr=tbl_master!item_nr

My results
Item_nr, tbl_1!Vendor_item_nr, tbl_2!Vendor_item_nr, tbl_3!Vendor_item_nr...ect

How can I count the number of Vendor_item_nr results returned for each Item_nr?
0
MCaliebe
Asked:
MCaliebe
  • 3
  • 2
  • 2
2 Solutions
 
jerryb30Commented:
SELECT master.item_nr, table1.vendor_item_nr, "table1" AS Source
FROM master INNER JOIN table1 ON master.item_nr = table1.item_nr
union all
SELECT master.item_nr, table2.vendor_item_nr, "table2" AS Source
FROM master INNER JOIN table2 ON master.item_nr = table2.item_nr
union all
SELECT master.item_nr, table3.vendor_item_nr, "table3" AS Source
FROM master INNER JOIN table3 ON master.item_nr = table3.item_nr

etc

Then:
Select Item_nr, count(vendor_item_nr) from yourQuery
group by item_nr
0
 
Dale FyeCommented:
You need to  get rid of the 10 separate (Vendor) tables and normalize that data into a single table with fields:

VendorID
Item_nr
Vendor_item_nr
Price

With that type of a structure, you simply count the number of VendorID values for each Item_Nr.

You could accomplish the same thing with a normalizing query, but having separate tables for each vendor makes the entire database application far more complex than it needs to by.
0
 
jerryb30Commented:
Meant to say: Create a union query, and use as source of count query.
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
MCaliebeAuthor Commented:
I was given each vendor as it's own spread sheet.  I admit I thought about putting them all together, however not all the vendors use a unique vendor_item_nr so I could have 3 vendors with unique Vendor_item_nr and 3 using the same. I needed to keep each record identified back to the vendor as well.  Some vendors have multiple prices as well.
0
 
Dale FyeCommented:
A lot of vendors have multiple prices based on the way the items are packaged, so create a single table with:

VendorID
Item_nr    'I assume this is your item number
Vendor_item_nr
Pckg          'this might be 'each', 'Pack of 10', 'Pack of 100', 'Box of 500'  whatever
Price

and vendor prices will also change, so it is common to include an Effective Date (and possibly Thru Date) in tables like this so you can keep track of the effective date of a price and how long it is good for.
0
 
MCaliebeAuthor Commented:
Although I'm certain Jerry's solution would work, I think the best solution for me is to do as fyed suggest and try to combine this data in a more data friendly format.  I'll probably have to ask this questions again, however at this time...I'll work with the data.
0
 
Dale FyeCommented:
You can use Jerry's union query to get the data into a normalized format I recommended.  That is where you need to start.  Then create a Make-Table query from the union query to create the new normalized table.  As long as you don't delete the original 10 vendor tables, you can tweak that union query until you get the structure the way you want it.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now