?
Solved

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

Posted on 2014-07-28
20
Medium Priority
?
177 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 4
  • 4
  • +1
20 Comments
 
LVL 25

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure 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 38

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 38

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 38

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 25

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 25

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 25

Accepted Solution

by:
chaau earned 2000 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Suggested Courses

764 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