Solved

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

Posted on 2014-07-28
20
170 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
 
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 34

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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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 34

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 34

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now