lundi 9 février 2015

Update from a Case in Sqlite

I'd like to update a table, each row must be tested, and a textual field must be updated to a numéric value depending on the primary key of another table. the 3 possible textual values could be associated to one of the 3 numeric values. I've found this request, which return at each row the same first numeric value, the '2' !



UPDATE HIV_adults_or_women_or_children_living_with_HIV SET indicateur_reduit=(
CASE
WHEN ((select indicateur_in FROM HIV_adults_or_women_or_children_living_with_HIV) = "Number of adults aged 15 and over living with HIV")
THEN (1)
WHEN ((SELECT indicateur_in FROM HIV_adults_or_women_or_children_living_with_HIV) = "Number of women aged 15 and over living with HIV" )
THEN (2)
WHEN ((select indicateur_in FROM HIV_adults_or_women_or_children_living_with_HIV) = "Number of children under 15 living with HIV")
THEN (3)
END )


Moreover I tried this next one not valid because it gives "not such column idResult" :



UPDATE HIV_adults_or_women_or_children_living_with_HIV SET indicateur_reduit=(
SELECT (SELECT ID FROM HIV_adults_or_women_or_children_living_with_HIV) as idResult,
CASE
WHEN ((SELECT indicateur_in FROM HIV_adults_or_women_or_children_living_with_HIV WHERE id) = "Number of women aged 15 and over living with HIV" )
THEN (1)
WHEN ((select indicateur_in FROM HIV_adults_or_women_or_children_living_with_HIV WHERE id) = "Number of children under 15 living with HIV")
THEN (2)
WHEN ((select indicateur_in FROM HIV_adults_or_women_or_children_living_with_HIV WHERE id) = "Number of adults aged 15 and over living with HIV")
THEN (3)
END )
WHERE id=idResult


So I could test and update each row by Flex, in first I'll use the select directive to return one row and secondly I'll update it... This could be a valid solution but it will make much ado about nothing, to much working.


Aucun commentaire:

Enregistrer un commentaire