In thread220-564239 I got the answer on how to put mutiple rows into one query row.
Returning to my original example...
Table A:
Product Stage Attribute Value
Alpha One Height V1
Alpha One Width V2
Alpha One Length V3
Alpha Two Height V4
Alpha Two Width V5
Alpha Two Length V6
Alpha Three Height V7
Alpha Three Width V8
Alpha Three Length V9
Table B:
Product Stage Quantity
Alpha One Q1
Alpha Two Q2
Alpha Three Q3
Carp gave me this solution...
SELECT b.stage, b.quantity, a1.value, a2.value, a3.value
FROM tableB b,
tableA a1,
tableA a2,
tableA a3
WHERE a1.product = b.product
AND a1.stage = b.stage
AND a2.product = b.product
AND a2.stage = b.stage
AND a3.product = b.product
AND a3.stage = b.stage
AND a1.attribute = 'HEIGHT'
AND a2.attribute = 'LENGTH'
AND a3.attribute = 'WIDTH'
ORDER BY b.stage;
This returns...
Stage Quantity Height Width Length
One Q1 V1 V2 V3
Two Q2 V4 V5 V6
Three Q3 V7 V8 V9
The table aliasing has allowed multiple rows to be returned in one row.
What if a row is missing from Table A ?
eg if the following row is missing...
Alpha Two Width V5
I still want to return...
Stage Quantity Height Width Length
One Q1 V1 V2 V3
Two Q2 V4 V6
Three Q3 V7 V8 V9
See how the missing data is just a blank (NULL or empty or whatever) but the row is still there.
I would like to continue to use Carp's structure as I am very familiar with it so how do I change Carp's query to achieve this ?
I have tried many possible solutions based on two principals...
1) Outer joins (syntax is familiar to me but I often struggle when table aliasing simultaneously with outer joins)
2) 'attribute = xxx OR attribute = NULL')
but with my massively more complex real queries I get too few rows or too many rows or nothing and I cannot see the logical pattern.
I had thought to write a similar simple database somewhere where I copuld experiment with basic solutions before scaling it up to my real queries but I have no write access to this particular corporate database and that would mean setting something up locally which may not be as logically equivalent as I need. After all there is something I do not understand here. This offers no guarantee of an answer and may not help me understand fundamentally the principal. I thought I'd try you excellent people next.
Boggg1
Returning to my original example...
Table A:
Product Stage Attribute Value
Alpha One Height V1
Alpha One Width V2
Alpha One Length V3
Alpha Two Height V4
Alpha Two Width V5
Alpha Two Length V6
Alpha Three Height V7
Alpha Three Width V8
Alpha Three Length V9
Table B:
Product Stage Quantity
Alpha One Q1
Alpha Two Q2
Alpha Three Q3
Carp gave me this solution...
SELECT b.stage, b.quantity, a1.value, a2.value, a3.value
FROM tableB b,
tableA a1,
tableA a2,
tableA a3
WHERE a1.product = b.product
AND a1.stage = b.stage
AND a2.product = b.product
AND a2.stage = b.stage
AND a3.product = b.product
AND a3.stage = b.stage
AND a1.attribute = 'HEIGHT'
AND a2.attribute = 'LENGTH'
AND a3.attribute = 'WIDTH'
ORDER BY b.stage;
This returns...
Stage Quantity Height Width Length
One Q1 V1 V2 V3
Two Q2 V4 V5 V6
Three Q3 V7 V8 V9
The table aliasing has allowed multiple rows to be returned in one row.
What if a row is missing from Table A ?
eg if the following row is missing...
Alpha Two Width V5
I still want to return...
Stage Quantity Height Width Length
One Q1 V1 V2 V3
Two Q2 V4 V6
Three Q3 V7 V8 V9
See how the missing data is just a blank (NULL or empty or whatever) but the row is still there.
I would like to continue to use Carp's structure as I am very familiar with it so how do I change Carp's query to achieve this ?
I have tried many possible solutions based on two principals...
1) Outer joins (syntax is familiar to me but I often struggle when table aliasing simultaneously with outer joins)
2) 'attribute = xxx OR attribute = NULL')
but with my massively more complex real queries I get too few rows or too many rows or nothing and I cannot see the logical pattern.
I had thought to write a similar simple database somewhere where I copuld experiment with basic solutions before scaling it up to my real queries but I have no write access to this particular corporate database and that would mean setting something up locally which may not be as logically equivalent as I need. After all there is something I do not understand here. This offers no guarantee of an answer and may not help me understand fundamentally the principal. I thought I'd try you excellent people next.
Boggg1