Accedi al ciclo db: per ogni record in una tabella crea una matrice di record in un'altra tabella

StackOverflow https://stackoverflow.com/questions/808331

Domanda

È possibile creare una query ciclica nidificata in Access DB che aggiornerà una terza tabella?

Ho una tabella principale (intestazione):

------------------------
masters
------------------------
num | modality | cost  |
------------------------
01  | thing    | 23.00 |
02  | thing    | 42.00 |
03  | thing    | 56.00 |
04  | apple    | 11.00 |
05  | apple    | 17.00 |

e una tabella temporanea contenente informazioni dettagliate che dovrò creare una terza tabella (effettiva) dettagli che si staccherà dalla tabella master

ecco un esempio della tabella dei dettagli della temp.

----------------------------------
temps
----------------------------------
modelnumber | modality | priceEa |
----------------------------------
| 123       | thing    | 1.00    |
| 234       | apple    | 2.00    |
| 345       | apple    | 3.00    |
| 456       | apple    | 4.00    |
| 567       | thing    | 5.00    |

Fondamentalmente, devo scorrere tutti i record nella tabella master .

Ciclo esterno:

Per ogni record nella tabella dei master, prendi la modalità.

Anello interno:

Quindi, per ogni record nella tabella temps, in cui le modalità corrispondono, crea un record nella tabella dei dettagli (e nel processo, esegui alcuni calcoli basati su temps.priceEa e masters.cost).

Questo dovrebbe creare (master * temps) numero di nuovi record nella tabella dei dettagli per ogni record nella tabella dei master.

la tabella dei dettagli, dovrebbe apparire come

----------------------------------------------------------
details
----------------------------------------------------------
num  | modelnumber | modality | priceEa  |  adjustedCost |
----------------------------------------------------------
| 01 | 123         | thing     | 1.00    | (do calc here)
| 01 | 567         | thing     | 5.00    | (do calc here)
| 02 | 123         | thing     | 1.00    | (do calc here)
| 02 | 567         | thing     | 5.00    | (do calc here)
| 03 | 123         | thing     | 1.00    | (do calc here)
| 03 | 567         | thing     | 5.00    | (do calc here)
| 04 | 234         | apple     | 2.00    | (do calc here)
| 04 | 345         | apple     | 3.00    | (do calc here)
| 04 | 456         | apple     | 4.00    | (do calc here)
| 05 | 234         | apple     | 2.00    | (do calc here)
| 05 | 345         | apple     | 3.00    | (do calc here)
| 05 | 456         | apple     | 4.00    | (do calc here)
...etc
È stato utile?

Soluzione


SELECT m.num, t.modelnumber, m.modality, t.priceea
into myNewTempTable
from masters m  inner  join temp t on m.modality = t.modality
order by m.num, t.modelnumber

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top