Solved

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

Posted on 2014-07-28
20
165 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
The abbreviations in the sheet tabs do not match?
0
 

Author Comment

by:softsupport
Comment Utility
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
Comment Utility
Any suggestions to my above question?
0
 
LVL 74

Expert Comment

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

Author Comment

by:softsupport
Comment Utility
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
Comment Utility
Ok,
Now I think I understand...
Stay tuned...
0
 
LVL 34

Expert Comment

by:PatHartman
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:softsupport
Comment Utility
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
Comment Utility
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
Comment Utility
As requested.
SQL.docx
0
 
LVL 34

Expert Comment

by:PatHartman
Comment Utility
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
Comment Utility
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
Comment Utility
I am now receiving error message when running code.  Please see attached.
SQL-error.docx
0
 
LVL 24

Expert Comment

by:chaau
Comment Utility
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
Comment Utility
Sorry.... another error
syntax-error.docx
0
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
Comment Utility
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
Comment Utility
Perfect.  thank you so much
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

763 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

11 Experts available now in Live!

Get 1:1 Help Now