I have a series of related tables.
Transaction_Type
Transaction_ID (primary)
Transaction_amount
Transaction_Type
Transaction
Transaction_ID (primary)
Timestamp
Purchase
Transaction_ID
Item_ID
Purchase_ID (primary)
Item
Item_ID
Client_ID
I need to select transaction_type rows based on a time stamp and client_id
My query is as follows:
SELECT
SUM(tt.Transaction_Amount)
FROM
ItemTracker_dbo.Transaction_Type tt
JOIN
ItemTracker_dbo.Transaction t
ON
tt.Transaction_ID = t.Transaction_ID
JOIN
ItemTracker_dbo.Purchase p
ON
p.Transaction_ID = tt.Transaction_ID
JOIN
ItemTracker_dbo.Item i
ON
i.Item_ID = p.Item_ID
WHERE
t.TimeStamp >= $start AND t.TimeStamp <= $end
AND
tt.Transaction_Format IN ('cash', 'credit')
AND
i.Client_ID = $client_ID
The issue:
If a customer purchases more than one item in a transaction - a transaction_id will have multiple rows in the Purchase table. So the join is repeating the transaction_amount for transaction_id's that have multiple rows in purchase.
Is there a way to apply UNIQUE
to the rows in purchase for this query?
Is there any other way to get around the multiple rows in purchase?
Or do I need to re-evaluate the architecture of my database?
If any additional info would be helpful please let me know.