Link to home
Start Free TrialLog in
Avatar of softsupport
softsupportFlag for United States of America

asked on

How to I specify which dated record to use in my query?

I have two tables
tblRates
with [EffectiveStart] and [EffectiveEnd] dates in each row.  These dates signify which payment rates to use.  

tblCount
has daily totals.

In my query, I compute a dollar amount by multiplying the payment rates by the daily totals.  The problem is the rates change annually and need to specify in the query which rates to use based on the [EffectiveStart] EffectiveEnd] dates.  In my query I have a [claimdate] field ( which is the end of every month).    I need the query to look at the [claimdate] and determine which rate to use if it is between the [EffectiveStart] [EffectiveEnd] dates.
Avatar of chaau
chaau
Flag of Australia image

It would be great if you could provide the column names as well. I will use "SELECT *" in my example. However, I advise you changing it in yours to make sure that you pull only those columns that are actually required:
SELECT *
FROM tblRates INNER JOIN tblCount
ON tblCount.[claimdate] >= tblRate.[EffectiveStart] and tblCount.[claimdate] < DateAdd("d", 1, tblRate.[EffectiveEnd])

Open in new window

In the query above I have used a DateAdd function. It is based on assumption that your tblRates table organised as per the follow:
Rate   [EffectiveStart]   [EffectiveEnd] 
1.0      2013-12-01         2013-12-25
1.2      2013-12-26         2014-01-05
1.1      2014-01-06         2014-02-14

Open in new window

i.e. the EffectiveStart for the next rate is a day after EffectiveEnd of the previous rate. If your Rates table has different rules, post the sample data here
It also depends on how complicated your yearly "Rates" are.
Is it one rate that changes every year, or are there many rates?

In a simple case (one rate) case like this, you may be able to "hardcode" the transactions with the specific rates...

A more "academic" solution (especially if you have many rates) would be to create a "RateHistory" table
rhID
rh_ProductID
rhYear
rhRate


Also note that this will be even more complicated if you are spanning one or more year in your query/reports
For example May 1, 2013 to May 1, 2014 (spanning two years with two different rates)

...So you may have t provide us with a bit more info on your rates.

JeffCoachman
Avatar of softsupport

ASKER

Attached please find tblRates, which displays two years of rates, both used within our fiscal year.  FY is Oct 1 to Sept 30, tblRates from July 1 to June 30.  Calculations are made with these rates so once the new rates are issued, I need the query to decipher which rates to use.  Attached please find some calculations using the tblRates.  

In the past, I just changed the rates each year, and it calculated correctly but we could not print out calculations prior to the rate change (because it was using wrong rates... new rate, not the old rates).  Now we want to see calculations with the old rate and the new rates in the same table. If I add both rates in the tblRates, I now receive 2 calculations for each month (because there are two rates in the table).  I hope this explains my dilemma better.
Calculations.xlsx
The abbreviations in the sheet tabs do not match?
Apologies...
The abbreviations in the sheet tabs are not suppose to match.  The sheet tab calculations have numbers used to multiply the tblRates with.  The results are shown in columns E, F, G (formulas in each cell)  

I made additional notes in this spreadsheet.  Hope I explained better.
Copy-of-Calculations.xlsx
Any suggestions to my above question?
So the values in tblRates Column F-L are irrelevant here?
They are irrelevant in this example, however, if I need to utilize those rates in calculations, they would be.  I just scaled down the calculation page for simplicity sake.  I want to specify, by [claimdate], which tblrate to use (as long as claimdate is within tblRates [EffectiveStartDate] [EffectiveEndDates]
Ok,
Now I think I understand...
Stay tuned...
What was wrong with the solution chaau provided?  Adding the non-equi join to your query should pull up the correct data.  

FYI, the QBE cannot represent a query that includes a non-equi join.  You will always have to view it in SQL view.
Pat... / Jeff
I have tried the above suggested items without results.  Please see the example I attached to clarify my request.  Jeff asked me to stay tuned as I think he understood after attaching examples above and answering some questions for him.  Please....I still need assistance with this question.
Please post the SQL for your version of chaau's solution.  The non-equi join is what you need to solve this problem.
As requested.
SQL.docx
You are creating a Cartesian Product  which chaau fixed also.

Replace the From clause with chaau's suggestion.  Once you do that, you will no longer be able to view the query in the QBE.  You will only be able to view it in SQL view because QBE only supports equi-joins (=).
I think this query will give you the desired result:
SELECT Center.[Center Id] AS [Center_Center Id], Center.[Center No], Center.[Center Name], CenterClaim.OperatingDays, CenterClaim.ClaimId, CenterClaim.[Center Id] AS [CenterClaim_Center Id], CenterClaim.ClaimDate, CenterClaim.Free, CenterClaim.Reduced, CenterClaim.[Non-Needy], CenterClaim.TitleXX, CenterClaim.BrkClaimed, CenterClaim.LunClaimed, CenterClaim.SnkClaimed, CenterClaim.OperCosts, [ReimbBrk]+[ReimbLun]+[ReimbCash]+[ReimbSnk] AS GrossReimb, [ReimbCash] AS CIL, [GrossReimb]-[CIL] AS MealReimbursement, CenterClaim.MrnSnkClaimed, CenterClaim.SupClaimed, CenterClaim.EveClaimed, CenterClaim.Notes, CenterClaim.Revision, CenterClaim.RevisionNo, CenterClaim.RevisionAmt, CenterClaim.RevisionDate, USDA.EffectiveEndDate, USDA.USDABrkFree, USDA.USDALunFree, USDA.USDASnkFree, USDA.USDABrkReduced, USDA.USDALunReduced, USDA.USDASnkReduced, USDA.USDABrkNon, USDA.USDALunNon, USDA.USDASnkNon, USDA.CashInLieu, [CenterClaim].[Free]+[CenterClaim].[Reduced]+[CenterClaim].[Non-Needy] AS TotalEnroll, [Free]/[TotalEnroll] AS PercFree, [Reduced]/[TotalEnroll] AS PercReduced, [Non-Needy]/[TotalEnroll] AS PercNonNeedy, [BrkClaimed]*[PercFree]*[USDABrkFree] AS BRBrkFree, [LunClaimed]*[PercFree]*[USDALunFree] AS BRLunFree, [SnkClaimed]*[PercFree]*[USDASnkFree] AS BRSnkFree, [BrkClaimed]*[PercReduced]*[USDABrkReduced] AS BRBrkRed, [LunClaimed]*[PercReduced]*[USDALunReduced] AS BRLunRed, [SnkClaimed]*[PercReduced]*[USDASnkReduced] AS BRSnkRed, [BrkClaimed]*[PercNonNeedy]*[USDABrkNon] AS BRBrkNon, [LunClaimed]*[PercNonNeedy]*[USDALunNon] AS BRLunNon, [SnkClaimed]*[PercNonNeedy]*[USDASnkNon] AS BRSnkNon, [BRBrkFree]+[BRBrkRed]+[BRBrkNon] AS BRBrkTotal, [BRLunFree]+[BRLunRed]+[BRLunNon] AS BRLunTotal, [BRSnkFree]+[BRSnkRed]+[BRSnkNon] AS BRSnkTotal, [BRBrkTotal] AS ReimbBrk, [BRLunTotal] AS ReimbLun, [LunClaimed]*[CashInLieu] AS ReimbCash, [BrSnkTotal] AS ReimbSnk
FROM Center INNER JOIN CenterClaim ON Center.[Center Id] = CenterClaim.[Center Id]
INNER JOIN USDA ON (CenterClaim.[claimdate] < DateAdd("d", 1, USDA.[EffectiveEnd])) AND (CenterClaim.[claimdate] >=USDA.[EffectiveStart])
WHERE (((Center.[Center Name])<>"Test Center"))
ORDER BY Center.[Center Name];

Open in new window

I am now receiving error message when running code.  Please see attached.
SQL-error.docx
Sorry, my mistake. Forgot about MS Access craziness about its requirement for brackets for inner joins. This query should give you the result without errors
SELECT Center.[Center Id] AS [Center_Center Id], Center.[Center No], Center.[Center Name], CenterClaim.OperatingDays, CenterClaim.ClaimId, CenterClaim.[Center Id] AS [CenterClaim_Center Id], CenterClaim.ClaimDate, CenterClaim.Free, CenterClaim.Reduced, CenterClaim.[Non-Needy], CenterClaim.TitleXX, CenterClaim.BrkClaimed, CenterClaim.LunClaimed, CenterClaim.SnkClaimed, CenterClaim.OperCosts, [ReimbBrk]+[ReimbLun]+[ReimbCash]+[ReimbSnk] AS GrossReimb, [ReimbCash] AS CIL, [GrossReimb]-[CIL] AS MealReimbursement, CenterClaim.MrnSnkClaimed, CenterClaim.SupClaimed, CenterClaim.EveClaimed, CenterClaim.Notes, CenterClaim.Revision, CenterClaim.RevisionNo, CenterClaim.RevisionAmt, CenterClaim.RevisionDate, USDA.EffectiveEndDate, USDA.USDABrkFree, USDA.USDALunFree, USDA.USDASnkFree, USDA.USDABrkReduced, USDA.USDALunReduced, USDA.USDASnkReduced, USDA.USDABrkNon, USDA.USDALunNon, USDA.USDASnkNon, USDA.CashInLieu, [CenterClaim].[Free]+[CenterClaim].[Reduced]+[CenterClaim].[Non-Needy] AS TotalEnroll, [Free]/[TotalEnroll] AS PercFree, [Reduced]/[TotalEnroll] AS PercReduced, [Non-Needy]/[TotalEnroll] AS PercNonNeedy, [BrkClaimed]*[PercFree]*[USDABrkFree] AS BRBrkFree, [LunClaimed]*[PercFree]*[USDALunFree] AS BRLunFree, [SnkClaimed]*[PercFree]*[USDASnkFree] AS BRSnkFree, [BrkClaimed]*[PercReduced]*[USDABrkReduced] AS BRBrkRed, [LunClaimed]*[PercReduced]*[USDALunReduced] AS BRLunRed, [SnkClaimed]*[PercReduced]*[USDASnkReduced] AS BRSnkRed, [BrkClaimed]*[PercNonNeedy]*[USDABrkNon] AS BRBrkNon, [LunClaimed]*[PercNonNeedy]*[USDALunNon] AS BRLunNon, [SnkClaimed]*[PercNonNeedy]*[USDASnkNon] AS BRSnkNon, [BRBrkFree]+[BRBrkRed]+[BRBrkNon] AS BRBrkTotal, [BRLunFree]+[BRLunRed]+[BRLunNon] AS BRLunTotal, [BRSnkFree]+[BRSnkRed]+[BRSnkNon] AS BRSnkTotal, [BRBrkTotal] AS ReimbBrk, [BRLunTotal] AS ReimbLun, [LunClaimed]*[CashInLieu] AS ReimbCash, [BrSnkTotal] AS ReimbSnk
FROM Center INNER JOIN CenterClaim (INNER JOIN USDA ON (CenterClaim.[claimdate] < DateAdd("d", 1, USDA.[EffectiveEnd])) AND (CenterClaim.[claimdate] >=USDA.[EffectiveStart])) on Center.[Center Id] = CenterClaim.[Center Id]
WHERE (((Center.[Center Name])<>"Test Center"))
ORDER BY Center.[Center Name];

Open in new window

Sorry.... another error
syntax-error.docx
ASKER CERTIFIED SOLUTION
Avatar of chaau
chaau
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect.  thank you so much