Dear all,
I'm trying to make something that looks like this:
into this:
The database I have to work with is not properly normalized, I can't change that unfortunately. Normally ITEMS would have a many-to-many relationship with PRODUCTS.
I have somewhat of a solution which uses VBA to:
- dlookup the record with fldCode value (fldCode = PK)
- loop through the columns and build a string that contains column names that are not empty
Returns this:
I'm wondering if either format can be accomplished with pure SQL (maybe multiple queries). One of the major problems is that I need to retrieve the field names from the PRODUCTS table. However, if it helps I can create a new table with all the existing field names (they are static anyway).
Any help is much appreciated.
I'm trying to make something that looks like this:
Code:
PRODUCTS
--------------------------------------------------------------
| fldCode | fldItem1 | fldItem2 | fldItem3 | ... | fldItem50 |
--------------------------------------------------------------
| ARA010 | 1 | | | | 4 |
| ARB011 | | 2 | 2 | | |
--------------------------------------------------------------
into this:
Code:
PRODUCTS
-------------------------------
| fldCode | Items | Count |
-------------------------------
| ARA010 | fldItem1 | 1 |
| ARA010 | fldItem50 | 4 |
| ARB011 | fldItem2 | 2 |
| ARB011 | fldItem3 | 2 |
-------------------------------
The database I have to work with is not properly normalized, I can't change that unfortunately. Normally ITEMS would have a many-to-many relationship with PRODUCTS.
I have somewhat of a solution which uses VBA to:
- dlookup the record with fldCode value (fldCode = PK)
- loop through the columns and build a string that contains column names that are not empty
Code:
SELECT p.Code, GetDetails(nz(p.Code, ""))
FROM Products p
IN "C:\MyDB.mdb"
Returns this:
Code:
---------------------------------
| fldCode | Items |
---------------------------------
| ARA010 | fldItem1; fldItem50 |
| ARA011 | fldItem2; fldItem3 |
---------------------------------
I'm wondering if either format can be accomplished with pure SQL (maybe multiple queries). One of the major problems is that I need to retrieve the field names from the PRODUCTS table. However, if it helps I can create a new table with all the existing field names (they are static anyway).
Any help is much appreciated.