Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Derived columns where 2nd is based on 1st

Author  Topic 

RandyK
Starting Member

5 Posts

Posted - 2008-10-09 : 12:53:17
I'm want to create two new derived (if that is the right word)
columns using case statements in a view. The second based on the first so that I don't have to repeat the same logic over again. I'm trying to avoid that to speed up the query.

Invalid column name 'Trans_Sym'.

Trans_Sym = case
when A.trans_rte_ty = 10 and B.road_act_stat_cd in (10,20,99) and F.road_sur_ty = 1 then 1
when A.trans_rte_ty = 10 and B.road_act_stat_cd in (10,20,99) and F.road_sur_ty in (2,3,4,5) then 2
when A.trans_rte_ty = 10 and B.road_act_stat_cd in (10,20,99) and F.road_sur_ty = 9 then 3
when A.trans_rte_ty = 10 and B.road_act_stat_cd = 30 then 4
when A.trans_rte_ty = 10 and B.road_act_stat_cd = 40 then 5
when A.trans_rte_ty = 30 then 6
when A.trans_rte_ty = 31 then 7
when A.trans_rte_ty = 20 then 8
when A.trans_rte_ty = 50 then 9
end,
Trans_Sym_Desc = case
when Trans_Sym = 1 then 'Paved Road'
when Trans_Sym = 2 then 'Unpaved Road'
when Trans_Sym = 3 then 'Surface Unknown Road'
when Trans_Sym = 4 then 'Abandoned Road'
when Trans_Sym = 5 then 'Orphaned Road'
when Trans_Sym = 6 then 'Railroad'
when Trans_Sym = 7 then 'Railroad Grade'
when Trans_Sym = 8 then 'Trail'
when Trans_Sym = 9 then 'Ferry Crossings'
end,

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2008-10-09 : 13:02:42
you can't create a computed column based on another computed column. by having the same logic again won't slow down your query.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-09 : 13:06:28
you can do like this

SELECT othercolumns...,
Trans_Sym_Desc = case
when Trans_Sym = 1 then 'Paved Road'
when Trans_Sym = 2 then 'Unpaved Road'
when Trans_Sym = 3 then 'Surface Unknown Road'
when Trans_Sym = 4 then 'Abandoned Road'
when Trans_Sym = 5 then 'Orphaned Road'
when Trans_Sym = 6 then 'Railroad'
when Trans_Sym = 7 then 'Railroad Grade'
when Trans_Sym = 8 then 'Trail'
when Trans_Sym = 9 then 'Ferry Crossings'
end
FROM
(
SELECT othercolumns....,
Trans_Sym = case
when A.trans_rte_ty = 10 and B.road_act_stat_cd in (10,20,99) and F.road_sur_ty = 1 then 1
when A.trans_rte_ty = 10 and B.road_act_stat_cd in (10,20,99) and F.road_sur_ty in (2,3,4,5) then 2
when A.trans_rte_ty = 10 and B.road_act_stat_cd in (10,20,99) and F.road_sur_ty = 9 then 3
when A.trans_rte_ty = 10 and B.road_act_stat_cd = 30 then 4
when A.trans_rte_ty = 10 and B.road_act_stat_cd = 40 then 5
when A.trans_rte_ty = 30 then 6
when A.trans_rte_ty = 31 then 7
when A.trans_rte_ty = 20 then 8
when A.trans_rte_ty = 50 then 9
end
FROM .....
.........
)t
Go to Top of Page

mayurgogia
Starting Member

3 Posts

Posted - 2008-10-20 : 03:30:04
Hello Visakh....
I guess you have given the right thing to do but this could not help me a lot. Can you please write complete query. No need of other columns. Just required ones will work.

Mayur Gogia
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-20 : 04:25:22
quote:
Originally posted by mayurgogia

Hello Visakh....
I guess you have given the right thing to do but this could not help me a lot. Can you please write complete query. No need of other columns. Just required ones will work.

Mayur Gogia


then give your table from which you're taking columns. thats why i put ... as i dont from where you're taking columns. or give the full query you used.
Go to Top of Page
   

- Advertisement -