I have a table called Modifiers :
This is the structure:
-----------------------------------------------------
| ModId | ModText | AdditionalCost |
-----------------------------------------------------
| 58 | RANCH | 2000 |
-----------------------------------------------------
| 86 | GUACAMOLE | 500 |
-----------------------------------------------------
| 52 | CEBOLLA | 0 |
-----------------------------------------------------
| 87 | QUESO | 150 |
-----------------------------------------------------
I have another table called "Orders", whose structure is as follows:
---------------------------------------------
| OrderId | OrderDateTime |
---------------------------------------------
| 20001 | 2017-06-01 00:00:00 |
---------------------------------------------
| 20001 | 2017-06-01 00:00:00 |
---------------------------------------------
| 15224 | 2017-06-02 00:00:00 |
---------------------------------------------
| 13501 | 2017-06-03 00:00:00 |
---------------------------------------------
I have another table called "Transactions", whose structure is as follows:
---------------------------------------------------------------------
| TransaccionID | Mod1ID | Mod2ID | Mod3ID |
---------------------------------------------------------------------
| 1 | | 86 | 87 |
---------------------------------------------------------------------
| 2 | 58 | | |
---------------------------------------------------------------------
| 3 | 87 | 52 | |
---------------------------------------------------------------------
| 4 | | 58 | |
---------------------------------------------------------------------
The Transactions table has these characteristics:
- Columns range from Mod1ID to Mod20ID .
- As you can see in the example, the Ids in the Modifiers table are located in the different ModXID columns of Transactions . . li>
- A Transaction can be composed of one or more modifiers.
What I'm looking for is to create a query in ms-access (which gets the expected results) , but I would like to know if there is a better way to get the same results "that is, the expected results":
NOTE: For short, this query only uses the Mod1ID columns, Mod2Id, Mod3ID and Mod4ID:
SELECT
t.MenuModifierText AS [Modificador],
COUNT(*) AS [Cantidad],
(SELECT MenuModCost.AdditionalCost FROM MenuModifiers AS MenuModCost WHERE MenuModCost.MenuModifierID = t.MenuModifierID) AS ValorMod,
(SELECT MenuModCost.AdditionalCost FROM MenuModifiers AS MenuModCost WHERE MenuModCost.MenuModifierID = t.MenuModifierID) * COUNT(*) AS [Valor]
FROM(
SELECT MENUMODS.MenuModifierText, MENUMODS.MenuModifierID
FROM (OrderTransactions AS ORDTRDS INNER JOIN OrderHeaders AS ORDHRD
ON ORDTRDS.OrderID = ORDHRD.OrderID) INNER JOIN MenuModifiers AS MENUMODS
ON MENUMODS.MenuModifierID = ORDTRDS.Mod1ID
WHERE ORDHRD.OrderDateTime BETWEEN #06/21/2017 00:00:00# AND #06/22/2017 23:59:59#
UNION ALL
SELECT MENUMODS.MenuModifierText, MENUMODS.MenuModifierID
FROM (OrderTransactions AS ORDTRDS INNER JOIN OrderHeaders AS ORDHRD
ON ORDTRDS.OrderID = ORDHRD.OrderID) INNER JOIN MenuModifiers AS MENUMODS
ON MENUMODS.MenuModifierID = ORDTRDS.Mod2ID
WHERE ORDHRD.OrderDateTime BETWEEN #06/21/2017 00:00:00# AND #06/22/2017 23:59:59#
UNION ALL
SELECT MENUMODS.MenuModifierText, MENUMODS.MenuModifierID
FROM (OrderTransactions AS ORDTRDS INNER JOIN OrderHeaders AS ORDHRD
ON ORDTRDS.OrderID = ORDHRD.OrderID) INNER JOIN MenuModifiers AS MENUMODS
ON MENUMODS.MenuModifierID = ORDTRDS.Mod3ID
WHERE ORDHRD.OrderDateTime BETWEEN #06/21/2017 00:00:00# AND #06/22/2017 23:59:59#
UNION ALL
SELECT MENUMODS.MenuModifierText, MENUMODS.MenuModifierID
FROM (OrderTransactions AS ORDTRDS INNER JOIN OrderHeaders AS ORDHRD
ON ORDTRDS.OrderID = ORDHRD.OrderID) INNER JOIN MenuModifiers AS MENUMODS
ON MENUMODS.MenuModifierID = ORDTRDS.Mod4ID
WHERE ORDHRD.OrderDateTime BETWEEN #06/21/2017 00:00:00# AND #06/22/2017 23:59:59#
) AS t
GROUP BY t.MenuModifierID, t.MenuModifierText;