简体   繁体   English

SQL,使用 if then else,将某个值从一列移动到另一列

[英]SQL, with if then else, move some value from a column to another

in SQL Server,在 SQL Server 中,

how can I move value from a column to another with conditional (if then else) ?如何在有条件的情况下将值从一列移动到另一列(if then else)? I'm explain with an example.我用一个例子来解释。

I have a table with我有一张桌子

Article文章 Desc.描述 UM Value价值
Item1项目1 Flower PC个人电脑 5001 5001
Item2项目2 Paper PC个人电脑 6001 6001
Item2项目2 Paper CT电脑断层扫描 6002 6002
Item3第 3 项 Pen CT电脑断层扫描 7001 7001
Item4项目4 Eraser橡皮 PC个人电脑 8001 8001

I need to have我需要有

Article文章 Desc.描述 Value PC价值电脑 ValueCT价值CT
Item1项目1 Flower 5001 5001 NULL无效的
Item2项目2 Paper 6001 6001 NULL无效的
Item2项目2 Paper NULL无效的 6002 6002
Item3第 3 项 Pen NULL无效的 7001 7001
Item4项目4 Eraser橡皮 8001 8001 NULL无效的

I thought to do a If then else我想先做一个 If then else

Something like that: if UM = 'PC' then begin instruction to move value to "Value PC" end else begin instruction to move value to "Value CT" end类似的东西: if UM = 'PC' then begin instruction to move value to "Value PC" end else begin instruction to move value to "Value CT" end

But I don't know which instruction use to move only value that I need to move.但我不知道使用哪个指令仅移动我需要移动的值。

Can you help me ?你能帮助我吗 ?

This looks like you just need a simple case expression,看起来你只需要一个简单的 case 表达式,

select Article, [Desc.],
    case when UM = 'PC' then Value end ValuePC,
    case when UM = 'CT' then Value end ValueCT
from t;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL Else If语句从另一列的数值返回文本值-不起作用 - SQL Else If statement to return text value from numeric value of another column - not working 更新 SQL 服务器中的列,而另一个表中某些列的值存在差异 - Updating a column in SQL Server with difference in value of some column in another table 从 SQL 中的另一列派生一列的值 - Deriving value of 1 column from another column in SQL SQL - 仅在匹配的地方用另一个表列替换列值,否则保留原始值? - SQL - Replace column values with another table column ONLY where they match, else keep original value? 将值从下面的行移动到 SQL 中的相邻列 - Move value from row below to adjacent column in SQL 如何从 Teradata SQL 的一列中获取一些值? - How to take some value from value in one column in Teradata SQL? PL/SQL - 简单触发器,从一列和 IF-THEN-ELSE 到另一列 - PL/SQL - Simple trigger, take from one column and IF-THEN-ELSE to another column SQL 根据值将值移动到新列 - SQL to move value to new column based on value 如果行不存在则返回0,否则返回列值 - SQL if row not present 0 else column value SQL如何通过检查列值来实现if和else - SQL how to implement if and else by checking column value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM