tags:

views:

49

answers:

4

Hi experts,

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**
------                     ---------       --------------
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

Thanks & Regards, Murali

A: 

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?

Rasmus Kaj
doesn't give him correct result for row 3.
AaronLS
A: 

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
Alex K.
@Alex K.: Where did you get the `'F'` from?
Peter Lang
@Alex "I need final select query", "I have a question related to select query"
AaronLS
+1  A: 
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
AaronLS
Thanks a lot sir.. it helped me a lot...
@user294146: If this answer solved your problem, then you should accept it: http://meta.stackoverflow.com/questions/5234/accepting-answers-what-is-it-all-about
Peter Lang
A: 
UPDATE mytable
SET Column3 = Column1
WHERE Column2 in ('C', 'H', 'L')
Anthony Faull