| Author |
Topic |
|
Exir
Posting Yak Master
151 Posts |
Posted - 2009-02-01 : 06:51:52
|
| I have a table and i want to write a select command on it and i want to write a command that trace each row and show a field in select result depends on other field of table.forexample:my table isAli - USA - 3Tom - UK - 1John - Canada - 2I want to write a select command which adds one column depends on the numbers in the third column. |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-01 : 06:55:39
|
| can u explain wt result shoould be in ur third columnshow some sample output |
 |
|
|
Exir
Posting Yak Master
151 Posts |
Posted - 2009-02-01 : 07:04:14
|
| for example i want to write a select which have a result like this:NAME - COUNTRY - NUMBER -MAJOR-------------------------------Ali - USA - 3 - computerTom - UK - 1 - physicsJohn - Canada - 2 - statisticsthe major column is not in database and i want to have it only for showing to the user.number 3 means computer,2 means physics and 1 means statisticsthis was a very simple example similar to what i wantthanks for your help |
 |
|
|
Exir
Posting Yak Master
151 Posts |
Posted - 2009-02-01 : 07:16:03
|
| I think i sould use cursor. what IS your suggestion? |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-01 : 07:18:18
|
| declare @tab table (name varchar(32), country varchar(32), number int)insert into @tab select 'Ali' , 'USA' , 3 union all select 'Tom' , 'UK' , 1 union all select 'John' , 'Canada' , 2 select *,case when number = 3 then 'computer' when number = 2 then 'statistics' when number = 1 then 'physics' end as majorfrom @tab |
 |
|
|
Exir
Posting Yak Master
151 Posts |
Posted - 2009-02-01 : 07:26:12
|
| why you have added all rows in @tab one by one ? i have huge number of rows in my table. how can i add rows one by one? |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-01 : 07:28:27
|
| if u are having 1,2,3 in ur number columnthen use select * ,case when number = 3 then 'computer'when number = 2 then 'statistics'when number = 1 then 'physics' end as majorfrom urtablename |
 |
|
|
Exir
Posting Yak Master
151 Posts |
Posted - 2009-02-01 : 07:59:29
|
| yes, thank you :) |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-01 : 08:03:16
|
quote: Originally posted by Exir yes, thank you :)
ur welcome |
 |
|
|
|