2

I have the the following query and I need to run it many times with different names, But How can I make a function of all the queries so that I only need to run that function instead of all the queries one by one.

update clean.vehicles set make='Ford' where type ilike '%explorer%'
update clean.vehicles set make='Chevrolet' where type ilike '%lumina%'
update clean.vehicles set make='Ford' where type ilike '%crown%'
update clean.vehicles set make='Subaru' where type ilike '%suburu%'
update clean.vehicles set make='Subaru' where type ilike '%legacy%'
update clean.vehicles set make='Infiniti' where type ilike '%infinitie%'
update clean.vehicles set make='Ford' where type ilike '%windstar%'
update clean.vehicles set make='Volkswagen' where type ilike '%vw%'
update clean.vehicles set make='Mitsubishi' where type ilike '%mitsubishi%'
update clean.vehicles set make='Infiniti' where type ilike '%infiniti%'
update clean.vehicles set make='Chevrolet' where type ilike '%chev%'
update clean.vehicles set make='Chrysler' where type ilike '%plymouth%'    
update clean.vehicles set make='Chrysler' where type ilike '%plymoth%

Thanks

2 Answers 2

1

You need a mapping table to look up the make when you specify the type (or vice versa).

Make       Type
Ford       Explorer 
Chevrolet  Lumina 
Ford       Crown 
Subaru     Legacy

Then, when you call your function GetMake(type), it does a query using the Type in the query, returns the Make, and you can then use the results in your query above.

Sign up to request clarification or add additional context in comments.

Comments

1

Alternatively you could just do...

UPDATE clean.vehicles
SET make = CASE
                WHEN type ilike '%explorer%'
                    THEN 'Ford'
                WHEN type ilike '%lumina%'
                    'Chevrolet'
                ELSE
                    make
           END

This will do an update on every record though non-matching records should be unchanged. If you are only updating a subset of the records then you could add a WHERE clause to only match those types.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.