I have an equipment reservation database that is used to create reservations for multiple pieces of equipment for various time periods. I have a metric for "Equipment days used this fiscal year" and feel that the way I am doing my calculations is insane. I basically have 5 queries that each select unique reservations based on when they begin and end. Kind of like Pbaldy's overlapping record solution here:
http://www.baldyweb.com/OverLap.htm
Those queries are then joined in a Uni query with this resulting SQL code:
Now this is where I ask if there is a simpler way to do this? Or is this simply the way that you would normally do this type of calculation?
I probably left out some critical information, so please feel free to ask for clarification.
http://www.baldyweb.com/OverLap.htm
Those queries are then joined in a Uni query with this resulting SQL code:
Code:
SELECT tblReservation_details.DateOut, tblReservation_details.DateIn, tblBOM_Master.BOMDescription, weekdays([Forms]![New Main Form]![txtFiscalYearStart],[datein]) AS WorkingDays, Sum(DateDiff("d",[Forms]![New Main Form]![txtFiscalYearStart],[DateIn])+1) AS [Total Usage Days], tblReservations.ReservationNumber, tblCompanies.Category, tblReservations.CategoryID, tblBOM_Master.PurchasePrice, FormatCurrency(((([purchasePrice]/3)*0.026385)*[Total Usage Days]),2) AS IndustryLeaseRate
FROM tblCompanies INNER JOIN (tblReservations INNER JOIN (tblBOM_Master INNER JOIN tblReservation_details ON tblBOM_Master.BOMID = tblReservation_details.BOMNumber) ON tblReservations.ReservationID = tblReservation_details.ReservationID) ON tblCompanies.CompanyID = tblReservations.CompanyID
GROUP BY tblReservation_details.DateOut, tblReservation_details.DateIn, tblBOM_Master.BOMDescription, weekdays([Forms]![New Main Form]![txtFiscalYearStart],[datein]), tblReservations.ReservationNumber, tblCompanies.Category, tblReservations.CategoryID, tblBOM_Master.PurchasePrice, tblReservations.FiscalDateOut, tblReservations.FiscalDateIn, tblBOM_Master.BOMID, tblReservations.ReservationStatus
HAVING (((tblReservation_details.DateOut)<=[Forms]![New Main Form]![txtFiscalYearstart] And (tblReservation_details.DateOut)<=[Forms]![New Main Form]![txtFiscalYearEnd] And (tblReservation_details.DateOut)<=Date()) AND ((tblReservation_details.DateIn)>=[Forms]![New Main Form]![txtFiscalYearStart] And (tblReservation_details.DateIn)<=Date()) AND ((tblReservations.ReservationStatus) In (2,3)))
UNION ALL
SELECT tblReservation_details.DateOut, tblReservation_details.DateIn, tblBOM_Master.BOMDescription, weekdays([DateOut],Date()) AS WorkingDays, Sum(DateDiff("d",[DateOut],Date())+1) AS [Total Usage Days], tblReservations.ReservationNumber, tblCompanies.Category, tblReservations.CategoryID, tblBOM_Master.PurchasePrice, FormatCurrency(((([purchasePrice]/3)*0.026385)*[Total Usage Days]),2) AS IndustryLeaseRate
FROM tblCompanies INNER JOIN (tblReservations INNER JOIN (tblBOM_Master INNER JOIN tblReservation_details ON tblBOM_Master.BOMID = tblReservation_details.BOMNumber) ON tblReservations.ReservationID = tblReservation_details.ReservationID) ON tblCompanies.CompanyID = tblReservations.CompanyID
GROUP BY tblReservation_details.DateOut, tblReservation_details.DateIn, tblBOM_Master.BOMDescription, weekdays([DateOut],Date()), tblReservations.ReservationNumber, tblCompanies.Category, tblReservations.CategoryID, tblBOM_Master.PurchasePrice, tblReservations.FiscalDateOut, tblReservations.FiscalDateIn, tblBOM_Master.BOMID, tblReservations.ReservationStatus
HAVING (((tblReservation_details.DateOut)>=[Forms]![New Main Form]![TxtFiscalYearStart] And (tblReservation_details.DateOut)>Date()) AND ((tblReservation_details.DateIn)<=[Forms]![New Main Form]![txtFiscalYearEnd] And (tblReservation_details.DateIn)>=Date()) AND ((tblReservations.ReservationStatus) In (2,3)))
UNION ALL
SELECT tblReservation_details.DateOut, tblReservation_details.DateIn, tblBOM_Master.BOMDescription, weekdays([DateOut],[DateIn]) AS WorkingDays, Sum(DateDiff("d",[DateOut],[DateIn])+1) AS [Total Usage Days], tblReservations.ReservationNumber, tblCompanies.Category, tblReservations.CategoryID, tblBOM_Master.PurchasePrice, FormatCurrency(((([purchasePrice]/3)*0.026385)*[Total Usage Days]),2) AS IndustryLeaseRate
FROM tblLookup_Reservation_Status INNER JOIN (tblCompanies INNER JOIN (tblReservations INNER JOIN (tblBOM_Master INNER JOIN tblReservation_details ON tblBOM_Master.BOMID = tblReservation_details.BOMNumber) ON tblReservations.ReservationID = tblReservation_details.ReservationID) ON tblCompanies.CompanyID = tblReservations.CompanyID) ON tblLookup_Reservation_Status.ReservationStatusID = tblReservations.ReservationStatus
GROUP BY tblReservation_details.DateOut, tblReservation_details.DateIn, tblBOM_Master.BOMDescription, weekdays([DateOut],[DateIn]), tblReservations.ReservationNumber, tblCompanies.Category, tblReservations.CategoryID, tblBOM_Master.PurchasePrice, tblReservations.FiscalDateOut, tblReservations.FiscalDateIn, tblBOM_Master.BOMID, tblLookup_Reservation_Status.ReservationStatusID
HAVING (((tblReservation_details.DateOut)>=Forms![New Main Form]!txtFiscalYearStart And (tblReservation_details.DateOut)<=Date()) And ((tblReservation_details.DateIn)<=Date() And (tblReservation_details.DateIn)>=Forms![New Main Form]!txtFiscalYearStart) And ((tblLookup_Reservation_Status.ReservationStatusID) In (2,3)))
UNION ALL
SELECT tblReservation_details.DateOut, tblReservation_details.DateIn, tblBOM_Master.BOMDescription, weekdays([DateOut],Date()) AS WorkingDays, Sum(DateDiff("d",[DateOut],Date())+1) AS [Total Usage Days], tblReservations.ReservationNumber, tblCompanies.Category, tblReservations.CategoryID, tblBOM_Master.PurchasePrice, FormatCurrency(((([purchasePrice]/3)*0.026385)*[Total Usage Days]),2) AS IndustryLeaseRate
FROM tblCompanies INNER JOIN (tblReservations INNER JOIN (tblBOM_Master INNER JOIN tblReservation_details ON tblBOM_Master.BOMID = tblReservation_details.BOMNumber) ON tblReservations.ReservationID = tblReservation_details.ReservationID) ON tblCompanies.CompanyID = tblReservations.CompanyID
GROUP BY tblReservation_details.DateOut, tblReservation_details.DateIn, tblBOM_Master.BOMDescription, weekdays([DateOut],Date()), tblReservations.ReservationNumber, tblCompanies.Category, tblReservations.CategoryID, tblBOM_Master.PurchasePrice, tblReservations.FiscalDateOut, tblReservations.FiscalDateIn, tblBOM_Master.BOMID, tblReservations.ReservationStatus
HAVING (((tblReservation_details.DateOut)>=[Forms]![New Main Form]![TxtFiscalYearStart] And (tblReservation_details.DateOut)<Date()) AND ((tblReservation_details.DateIn)>=[Forms]![New Main Form]![txtFiscalYearEnd] And (tblReservation_details.DateIn)>=Date()) AND ((tblReservations.ReservationStatus) In (2,3)))
UNION ALL
SELECT tblReservation_details.DateOut, tblReservation_details.DateIn, tblBOM_Master.BOMDescription, weekdays([Forms]![New Main Form]![txtFiscalYearStart],[dateinreq]) AS WorkingDays, Sum(DateDiff("d",[Forms]![New Main Form]![txtFiscalYearStart],Date())+1) AS [Total Usage Days], tblReservations.ReservationNumber, tblCompanies.Category, tblReservations.CategoryID, tblBOM_Master.PurchasePrice, FormatCurrency(((([purchasePrice]/3)*0.026385)*[Total Usage Days]),2) AS IndustryLeaseRate
FROM tblCompanies INNER JOIN (tblReservations INNER JOIN (tblBOM_Master INNER JOIN tblReservation_details ON tblBOM_Master.BOMID = tblReservation_details.BOMNumber) ON tblReservations.ReservationID = tblReservation_details.ReservationID) ON tblCompanies.CompanyID = tblReservations.CompanyID
GROUP BY tblReservation_details.DateOut, tblReservation_details.DateIn, tblBOM_Master.BOMDescription, weekdays([Forms]![New Main Form]![txtFiscalYearStart],[dateinreq]), tblReservations.ReservationNumber, tblCompanies.Category, tblReservations.CategoryID, tblBOM_Master.PurchasePrice, tblReservations.FiscalDateOut, tblReservations.FiscalDateIn, tblBOM_Master.BOMID, tblReservations.ReservationStatus
HAVING (((tblReservation_details.DateOut)<=[Forms]![New Main Form]![txtFiscalYearstart] And (tblReservation_details.DateOut)<=[Forms]![New Main Form]![txtFiscalYearEnd] And (tblReservation_details.DateOut)<=Date()) AND ((tblReservation_details.DateIn)>=[Forms]![New Main Form]![txtFiscalYearEnd]) AND ((tblReservations.ReservationStatus) In (2,3)))
UNION ALL SELECT tblReservation_details.DateOut, tblReservation_details.DateIn, tblBOM_Master.BOMDescription, weekdays([Forms]![New Main Form]![txtFiscalYearStart],Date()) AS WorkingDays, Sum(DateDiff("d",[Forms]![New Main Form]![txtFiscalYearStart],Date())+1) AS [Total Usage Days], tblReservations.ReservationNumber, tblCompanies.Category, tblReservations.CategoryID, tblBOM_Master.PurchasePrice, (([purchasePrice]/3)*0.026385)*[workingdays] AS IndustryLeaseRate
FROM tblCompanies INNER JOIN (tblReservations INNER JOIN (tblBOM_Master INNER JOIN tblReservation_details ON tblBOM_Master.BOMID = tblReservation_details.BOMNumber) ON tblReservations.ReservationID = tblReservation_details.ReservationID) ON tblCompanies.CompanyID = tblReservations.CompanyID
GROUP BY tblReservation_details.DateOut, tblReservation_details.DateIn, tblBOM_Master.BOMDescription, weekdays([Forms]![New Main Form]![txtFiscalYearStart],Date()), tblReservations.ReservationNumber, tblCompanies.Category, tblReservations.CategoryID, tblBOM_Master.PurchasePrice, tblReservations.FiscalDateOut, tblReservations.FiscalDateIn, tblBOM_Master.BOMID, tblReservations.ReservationStatus
HAVING (((tblReservation_details.DateOut)<=[Forms]![New Main Form]![txtFiscalYearstart] And (tblReservation_details.DateOut)<=Date()) AND ((tblReservation_details.DateIn)>=[Forms]![New Main Form]![txtFiscalYearStart] And (tblReservation_details.DateIn)>=Date() And (tblReservation_details.DateIn)<=[Forms]![New Main Form]![txtFiscalYearEnd]) AND ((tblReservations.ReservationStatus) In (2,3)));
Now this is where I ask if there is a simpler way to do this? Or is this simply the way that you would normally do this type of calculation?
I probably left out some critical information, so please feel free to ask for clarification.