BeardedSith
Member
- Local time
- Today, 18:24
- Joined
- Feb 5, 2020
- Messages
- 73
Hi there!
First off, I'm a long-time reader but first time account-creator and poster. So, hi!
I have a query I'm struggling to create. I need to SELECT and COUNT records from tbPurchases where tbPurchases.PurchaseDate is > Last(tblRewards.IssueDate) WHERE CustomerID = tbPurchases.MemberID. Then I need a total of tbPurchases.PurchaseAmount. I'm not the greatest at SQL so the fact that this will likely require aliases and sub-queries, I'm at a total loss. I tried to accomplish the same feat using multiple queries, but I always get backed up with trying to Count() and use Last().
BACKGROUND:
I'm creating a Rewards System database for my employer. There are several tables to note:
These queries perform similar calculations to what I need to accomplish, but where I'm struggling is grabbing the purchases since the last time the customer was issued a rewards credit. Any help y'all can provide would be immensely helpful in finishing this project.
RELATIONSHIPS
Attached is an image of my table relationships.
First off, I'm a long-time reader but first time account-creator and poster. So, hi!
I have a query I'm struggling to create. I need to SELECT and COUNT records from tbPurchases where tbPurchases.PurchaseDate is > Last(tblRewards.IssueDate) WHERE CustomerID = tbPurchases.MemberID. Then I need a total of tbPurchases.PurchaseAmount. I'm not the greatest at SQL so the fact that this will likely require aliases and sub-queries, I'm at a total loss. I tried to accomplish the same feat using multiple queries, but I always get backed up with trying to Count() and use Last().
BACKGROUND:
I'm creating a Rewards System database for my employer. There are several tables to note:
- tblMembers - This table has all membership/customer data such as address, phone, etc.
- tblOptions - This table includes several options such as the number of purchases within a calendar year to qualify for a Rewards Credit. (currently 6)
- tblRewards - When a customer meets the requirements to be issued a Rewards Credit, it will add a row to this table including the amount of the rewards credit (10% of their purchases since their last Rewards Credit, up to $100.00 - these are set in tblOptions also).
- tbPurchases - yeah, I left out the "L" in "tbl", oops. This is where each purchase is cataloged. Includes total spent, date, customer ID, etc.
- qryCountCurrentYear - This will count the number of purchases the customer has had in the current calendar year.
-
Code:
SELECT qryCountCurrentYear.MemberID, FormatCurrency([qryCountCurrentYear]![SumOfPurchaseAmount]*[tblOptions]![PerDiscount]) AS RewardAmount, qryCountCurrentYear.NumPurchases FROM qryCountCurrentYear, tblOptions WHERE (((qryCountCurrentYear.MemberID)=[Forms]![frmRewards]![txtID]));
-
- qryEligibiles - This pulls a list of potentially eligible customers from qryCountCurrentYear (above) using options from tblOptions.
-
Code:
SELECT qryCountCurrentYear.MemberID, qryCountCurrentYear.NumPurchases, FormatDateTime([LastOfPurchaseDate],2) AS LastPurchase, FormatCurrency([qryCountCurrentYear]![SumOfPurchaseAmount]*[tblOptions]![PerDiscount]) AS EligibleAmount, qryCountCurrentYear.SumOfPurchaseAmount AS TotalSpent FROM qryCountCurrentYear, tblOptions WHERE (((qryCountCurrentYear.NumPurchases)>=[tblOptions]![NumPurchases]));
-
- qryEligibility - This pulls the list from qryEligibles, find when their last reward was issued and how much the actual reward should be based on the calculation from qryEligibles and the Max Rewards that can be issued from tblOptions.
-
Code:
SELECT qryEligibles.MemberID, FormatCurrency(IIf([qryEligibles]![EligibleAmount]>[tblOptions]![MaxIssue],[tblOptions]![MaxIssue],[qryEligibles]![EligibleAmount])) AS Amount, Nz([tblRewards]![IssueDate],"1/1/" & Year(Now())) AS LastReward FROM tblOptions, qryEligibles INNER JOIN tblRewards ON qryEligibles.MemberID = tblRewards.CustomerID;
-
These queries perform similar calculations to what I need to accomplish, but where I'm struggling is grabbing the purchases since the last time the customer was issued a rewards credit. Any help y'all can provide would be immensely helpful in finishing this project.
RELATIONSHIPS
Attached is an image of my table relationships.