[英]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.