我在SELECT中有CASE条件。以下是查询条件
查询来自视图
SELECT expt_id,stain_type,control_stain, test_stain
CASE WHEN stain_type = 'Blue' THEN control_stain = 'NA'
CASE WHEN stain_type = 'Hemat' THEN test_stain = 'NA'
FROM experiment_results__view
这里的值是' NA'必须填充stain_type的条件。非常感谢您提供有关如何撰写此查询的建议
谢谢!
答案 0 :(得分:2)
我希望您能够使用该行中的内容:
SELECT
expt_id,
stain_type,
CASE WHEN stain_type = 'Blue' THEN 'NA' ELSE NULL END control_stain,
CASE WHEN stain_type = 'Hemat' THEN 'NA' ELSE NULL END test_stain
FROM experiment_results__view
获取你的价值' NA'到名为control_stain和test_stain的列中。 但是,如果您想要覆盖这些列中的现有值,并使用值' NA'在这些情况下,然后使用:
SELECT
expt_id,
stain_type,
CASE WHEN stain_type = 'Blue' THEN 'NA' ELSE control_stain END control_stain,
CASE WHEN stain_type = 'Hemat' THEN 'NA' ELSE test_stain END test_stain
FROM experiment_results__view
如果您打算更新表格,那么您应该将其重写为:
UPDATE
experiment_results__view
SET
control_stain = CASE WHEN stain_type = 'Blue' THEN 'NA' ELSE control_stain END,
test_stain = CASE WHEN stain_type = 'Hemat' THEN 'NA' ELSE test_stain END
-- if you don't want to update the complete table add an WHERE clause
WHERE
<some condition>
答案 1 :(得分:1)
你需要END每个案例陈述
SELECT expt_id,stain_type,
CASE WHEN stain_type = 'Blue' THEN 'NA' ELSE control_stain END AS 'Control Stain'
CASE WHEN stain_type = 'Hemat' THEN 'NA' ELSE test_stain END AS 'Test Stain'
FROM experiment_results__view
如果您打算更新表格,可以进行更新查询
UPDATE
experiment_results__view
SET
control_stain = CASE WHEN stain_type = 'Blue' THEN 'NA' ELSE control_stain END,
test_stain = CASE WHEN stain_type = 'Hemat' THEN 'NA' ELSE test_stain END