Solved

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

Posted on 2014-07-28
20
171 Views
Last Modified: 2014-08-25
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.
0
Comment
Question by:softsupport
  • 9
  • 4
  • 4
  • +1
20 Comments
 
LVL 24

Expert Comment

by:chaau
ID: 40225655
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
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40227826
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
0
 

Author Comment

by:softsupport
ID: 40244966
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
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40245289
The abbreviations in the sheet tabs do not match?
0
 

Author Comment

by:softsupport
ID: 40245338
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
0
 

Author Comment

by:softsupport
ID: 40256448
Any suggestions to my above question?
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40259801
So the values in tblRates Column F-L are irrelevant here?
0
 

Author Comment

by:softsupport
ID: 40260644
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]
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40262890
Ok,
Now I think I understand...
Stay tuned...
0
 
LVL 35

Expert Comment

by:PatHartman
ID: 40268559
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.
0
 

Author Comment

by:softsupport
ID: 40277268
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.
0
 
LVL 35

Expert Comment

by:PatHartman
ID: 40277412
Please post the SQL for your version of chaau's solution.  The non-equi join is what you need to solve this problem.
0
 

Author Comment

by:softsupport
ID: 40277536
As requested.
SQL.docx
0
 
LVL 35

Expert Comment

by:PatHartman
ID: 40277604
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 (=).
0
 
LVL 24

Expert Comment

by:chaau
ID: 40277717
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

0
 

Author Comment

by:softsupport
ID: 40279243
I am now receiving error message when running code.  Please see attached.
SQL-error.docx
0
 
LVL 24

Expert Comment

by:chaau
ID: 40280384
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

0
 

Author Comment

by:softsupport
ID: 40280742
Sorry.... another error
syntax-error.docx
0
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 40282222
Sorry, I have misplaced the brackets. Try this one:
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 (CenterClaim INNER JOIN USDA ON CenterClaim.[claimdate] < DateAdd("d", 1, USDA.[EffectiveEnd]))
INNER JOIN Center ON Center.[Center Id] = CenterClaim.[Center Id]
WHERE Center.[Center Name]<>"Test Center" AND CenterClaim.[claimdate] >=USDA.[EffectiveStart]
ORDER BY Center.[Center Name];

Open in new window

0
 

Author Closing Comment

by:softsupport
ID: 40284607
Perfect.  thank you so much
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question