Tim
Tim

Reputation: 301

How to update one table with data from separate table using conditionals

This query may be absolutely ridiculous or maybe I just have one little thing wrong. I'm not quite sure. But basically I'm trying to update a table with data from another table in Access. The columns that are giving me issues are those set to "Yes/No" types and the data I'm trying to update it with are set to "Text" types whose values are strings "Yes" or "No."

So what I had in mind with this query, if it's even possible, is to roughly convert it the "Text" fields to 1's or 0's depending on their Yes or No values, respectively. Anyway, enough background, here's the query:

UPDATE Group_Pricing SET 
Contract_Type = (SELECT Contract_Type FROM ContractTypesFinal WHERE ContractTypesFinal.FirstGroup = Group_Pricing.GroupID), 
Hybrid_Retail =  
(SELECT CASE (SELECT RetailSpread FROM ContractTypesFinal WHERE ContractTypesFinal.FirstGroup = Group_Pricing.GroupID)
CASE "Yes"
RetailSpread = 1
CASE "No"
RetailSpread = 0), 
Hybrid_Mail =  
(SELECT CASE (SELECT MailSpread FROM ContractTypesFinal WHERE ContractTypesFinal.FirstGroup = Group_Pricing.GroupID)
CASE "Yes"
MailSpread = 1
CASE "No"
MailSpread = 0),
Hybrid_Specialty =  
(SELECT CASE (SELECT SpecialtySpread FROM ContractTypesFinal WHERE ContractTypesFinal.FirstGroup = Group_Pricing.GroupID)
CASE "Yes"
SpecialtySpread = 1
CASE "No"
SpecialtySpread = 0),
Hybrid_Rebates =  
(SELECT CASE (SELECT Rebates FROM ContractTypesFinal WHERE ContractTypesFinal.FirstGroup = Group_Pricing.GroupID)
CASE "Yes"
Rebates = 1
CASE "No"
Rebates = 0)
WHERE EXISTS (SELECT 1 FROM ContractTypesFinal WHERE ContractTypesFinal.FirstGroup = Group_Pricing.GroupID);

Any help is much appreciated. If this is impossible, just tell me I'm an idiot. Thanks!

Upvotes: 2

Views: 828

Answers (1)

Fionnuala
Fionnuala

Reputation: 91356

You cannot have CASE in MS Access, you can have IIf.

UPDATE Group_Pricing SET 
Contract_Type = 
     (SELECT Contract_Type 
      FROM ContractTypesFinal 
      WHERE ContractTypesFinal.FirstGroup = Group_Pricing.GroupID), 
Hybrid_Retail =  
IIf (SELECT RetailSpread 
     FROM ContractTypesFinal 
     WHERE ContractTypesFinal.FirstGroup = Group_Pricing.GroupID)="Yes",1,0)

<...>

Note that YesNo in MS Access is -1 and 0

It would be much easier, however, to just say:

UPDATE table1 SET AYesNo=(AText="Yes")

EDIT

UPDATE Group_Pricing 
INNER JOIN ContractTypesFinal 
ON Group_Pricing.GroupID = ContractTypesFinal.FirstGroup 
SET Hybrid_Retail = (RetailSpread="Yes")

This (RetailSpread="Yes") will evaluate to True (-1)

Upvotes: 3

Related Questions