开发者

Iterating result of Select Query

开发者 https://www.devze.com 2022-12-26 06:52 出处:网络
I have a question related to select query. here I am explaining down below. I have a table with the following data

I have a question related to select query. here I am explaining down below.

I have a table with the following data

**Column1(Primary Key)      Column2         Column3**
------                开发者_StackOverflow中文版     ---------       --------------
1                             C
2                             C
3                             Null 
4                             H
5                             L 
6                             H

my problem is I have to replace the value of Column3 with the corresponding value of Column1 for every occurrence of data "C", "H" and "L". Please provide me query related to this problem. How can I solve this using query or stored procedure. Please elaborate the same.

I need final select query result as follows

**Column1(Primary Key)      Column2         Column3**
------                     ---------       --------------
1                             C                1
2                             C                2
3                             Null 
4                             H                4
5                             L                5
6                             H                6


Select Column1, Column2,
CASE
  WHEN Column2 is not null THEN Column1
  ELSE null --or whatever value you want for blank
END as Column3
From TableName t1

Alternatively you could it it like this:

Select Column1, Column2,
CASE
  WHEN Column2 = 'L' or Column2 = 'H' or Column2 = 'C' THEN Column1
  ELSE null --or whatever value you want for blank
END as Column3
From TableName t1


Do you mean UPDATE?

UPDATE tbl SET Column3 = Column1 WHERE Column2 IN ('C', 'H', 'L', 'F')

Or for all;

UPDATE tbl SET Column3 = Column1 WHERE Column2 IS NOT NULL


UPDATE mytable
SET Column3 = Column1
WHERE Column2 in ('C', 'H', 'L')


like this?

select Column1, Column2, Column1 as Column3 from table

Or I'm not sure what you are actually asking for ..?

Ah, sorry. Dind't read all of the question there ...

Most SQL dialects have a way to do conditional subselects, but they are different in differend dialects. So which SQL are you using?

0

精彩评论

暂无评论...
验证码 换一张
取 消