简体   繁体   中英

Assistance with Access ADP and/or SQL iif statement

I'm using Access ADP as a front end to SQL. I have two tables:

  • One is Price_2018
  • One is Price_2020

I want to choose Price from the Price_2018 or the Price_2020 table, depending upon the Purchase_Date. I tried first putting IiF statement in the Query Designer:

Iif ([Purchase_Date] < Convert(DateTime, '2020-01-01 00:00:00'),[Price_2018],[Price_2020])

Access didn't allow that and ended up putting the whole thing in quotes in the SQL pane, so all I got was text output.

Someone suggested putting in the Select section:

CASE ([Purchase_Date] < CONVERT(DATETIME, '2020-01-01 00:00:00')   WHEN 1 THEN [Price_2018] ELSE [Price_2020] 

That didn't work either and gave me this error:

Error in list of function arguments: '<' not recognized.Error in list of function arguments: ',' not recognized.Error in list of function arguments: 'FROM' not recognized.
Unable to parse query text.

How to resolve this error. I'm not familiar with using a CASE statement in Access or SQL.

I combined 2 Price tables (combined table called VWC_2018_2020) as suggested. Full query:

SELECT      TOP 100 PERCENT dbo.VHCSERVER_Ntier_VHC_dbo_vwGenSvcInfo.Service_ID,                         dbo.VHCSERVER_Ntier_VHC_dbo_vwGenSvcInfo.Service_Date_From, dbo.VHCSERVER_Ntier_VHC_dbo_vwGenSvcInfo.Update_Status,                         dbo.VHCSERVER_Ntier_VHC_dbo_vwGenSvcInfo.Account_Number, dbo.VHCSERVER_Ntier_VHC_dbo_vwGenSvcInfo.Service_Units,                         dbo.VHCSERVER_Ntier_VHC_dbo_vwGenSvcInfo.Modifiers, dbo.VHCSERVER_Ntier_VHC_dbo_vwGenSvcInfo.Procedure_Code,                         NormalUnionCPASumAdjustments.SumAmount AS Adjustment, NormalUnionCPASumPayments.SumAmount AS Payment,                         dbo.VHCSERVER_Ntier_VHC_dbo_vwGenSvcInfo.Service_Fee AS Charge,                         dbo.VHCSERVER_Ntier_VHC_dbo_vwGenSvcInfo.Service_Fee - ISNULL(NormalUnionCPASumPayments.SumAmount, 0) AS Unpaid,                         dbo.VHCSERVER_Ntier_VHC_dbo_vwGenSvcInfo.Primary_Diagnosis_Code, LastInsurancePmt.last_insurance_pmt,                         dbo.VHCSERVER_Ntier_VHC_dbo_vwGenSvcInfo.Patient_Number, dbo.VWCFees_2018_2020.VWC_2020, dbo.VWCFees_2018_2020.VWC_2018,                        IIF (dbo.VHCSERVER_Ntier_VHC_dbo_vwGenSvcInfo.Service_Date_From < CDate('2020-01-01 00:00:00'),dbo.VWCFees_2018_2020.VWC_2018,dbo.VWCFees_2018_2020.VWC_2020) AS VWCFEE
FROM          dbo.VHCSERVER_Ntier_VHC_dbo_vwGenSvcInfo LEFT OUTER JOIN                        dbo.VWCFees_2018_2020 ON dbo.VHCSERVER_Ntier_VHC_dbo_vwGenSvcInfo.Procedure_Code = dbo.VWCFees_2018_2020.CPT LEFT OUTER JOIN                        dbo.NormalUnionCPASumAdjustments() NormalUnionCPASumAdjustments ON                         dbo.VHCSERVER_Ntier_VHC_dbo_vwGenSvcInfo.Service_ID = NormalUnionCPASumAdjustments.Service_ID LEFT OUTER JOIN                        dbo.NormalUnionCPASumPayments() NormalUnionCPASumPayments ON                         dbo.VHCSERVER_Ntier_VHC_dbo_vwGenSvcInfo.Service_ID = NormalUnionCPASumPayments.Service_ID LEFT OUTER JOIN                        dbo.LastInsurancePmt() LastInsurancePmt ON dbo.VHCSERVER_Ntier_VHC_dbo_vwGenSvcInfo.Service_ID = LastInsurancePmt.Service_ID
WHERE      (dbo.VHCSERVER_Ntier_VHC_dbo_vwGenSvcInfo.Update_Status <= 1) AND                         (dbo.VHCSERVER_Ntier_VHC_dbo_vwGenSvcInfo.Service_Date_From BETWEEN @StartDate AND @EndDate) AND                         (dbo.VHCSERVER_Ntier_VHC_dbo_vwGenSvcInfo.Patient_Number = @VHC_NumberChild)

Running in Access, gives error message: '<' not recognized. Missing FROM clause. Unable to parse query text.

What now? Thanks

Since ADP files use an SQL Server backend (no longer supported as of MS Access 2013), you must use its TSQL dialect which does support IIF (or CASE ) and can compare dates with string representations without conversion.

Below I extend to fuller query assuming various columns. Adjust as needed:

SELECT
  ...
  IIF(t.[Purchase_Date] < '2020-01-01', p18.price, p20.price)
  ...
FROM [Transactions] t
INNER JOIN [Price_2018] p18
  ON t.price_id = p18.price_id
INNER JOIN [Price_2020] p20
  ON t.price_id = p20.price_id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM