I've been stuck on a solution for the problem I am about to describe below. If there is something that needs clarification please let me know so that I can update my post. First of all, let me explain the database structure. I have the following tables (and more, but they are not part of this topic):
1) Divisions 2) Companies 3) Sectors 4) Products
All the above tables are related.
1) Divisions has many Companies, but Companies can only belong to a single Division. 1-* 2) Each Company can have one or more Sectors, but a Sector can belong to a single Company. 1-* 3) Each Sector can have one or more Products and each Product can belong to one or more Sector. -
My problem(s) are below: Given the above, regardless of Companies, I need to generate or retrieve all Sectors under a given Division. But the trick is they need to be merged based on their names not ids, so suppose you have Division 1 and under it Company A and under that Sector A. Of course Sector A could exist again as another entity in another Company under the same name, but different ID (other than Company A) that also falls under Division 1. So in that new "merger" Sector A should be distinct meaning there should be a single entity for it, not more. Let us call the table that stores these mergers table X. Now table X (which contains unique Sectors under a given Division regardless or their respective Companies) should be mapped to another table (Table Y) that will contain documents and other files. Now the second issue is that Table Y's documents need to be related to Products that are mapped to Sectors listed in table X. Of course if Product 1 is under Sector A and Sector B its documents might be different, just because it belongs to two different Sectors.
How could the above be translated into SQL Tables / Views or whatever is needed? And if that's not possible, what is the right approach to solving the issues? Also, what happens if a Sector is deleted? or its name was updated? Or a product update/delete? What is the best thing to do in this particular case.
Note: the tables 1, 2, 3, 4 listed above are already created and functional. But I am required to add this extra functionality described as the problem above. I cannot do anything here to change the logic and requirements.
Using MS SQL Server 2008 R2.
Many thanks in advance. Please let me know if anything needs clarification.
EDIT : How the structure currently is vs. How it should become generated when adding documents.
Expected output:
The merger here is based solely on Product names. But now, what happens if Product A has been Updated? Deleted? How does that reflect on the output and mapped Documents?
Thanks again.
(BTW: I can generate that output it's no issue given the first data Model (First image) But I need that placed somehow in a table which proper relationships so that I can map Documents to Products based on all the above.)