2013-01-17 57 views
0

我將以儘可能清晰的方式提問這個問題。這是相當混亂,所以我提前道歉,如果它遇到不清楚:根據另一個表中的條件在一個表中設置字段

我有一組表:表1,表2,表3

各表中有一欄「時代」

我想創建一個表,檢查年齡列是否包含每個表的「年齡」列中的任何記錄是否大於60,如果是,則在新列中設置記錄等於「紅色」。我還想指定一些其他標準來設置等於「黃色」和「綠色」的列;然而,一次一步。

事情是這樣的:

表|年齡

表1 |紅色

表2 |黃色

表3 |綠色

任何可用的幫助表示讚賞

回答

1

select case when max(t1.age) > 60 then 'RED' else 'GREEN' end table1_age, 
      case when max(t2.age) > 60 then 'RED' else 'GREEN' end table2_age, 
      case when max(t3.age) > 60 then 'RED' else 'GREEN' end table3_age 
     from (select max(age) age from table1) t1 
      cross join (select max(age) age from table2) t2 
      cross join (select max(age) age from table2) t3; 

select 'table 1' tabl, 
     case when max(age) > 60 then 'RED' else 'GREEN' end 
    from table1 t1 
union all 
select 'table 2' tabl, 
     case when max(age) > 60 then 'RED' else 'GREEN' end 
    from table2 
union all 
select 'table 3' tabl, 
     case when max(age) > 60 then 'RED' else 'GREEN' end 
    from table3; 
+0

情況下,當最高(年齡)> 60,然後 '紅' 其他情況下,當最高(年齡)< 30然後'GREEN'結束 是否有可能在第二個標準之間或第三個之間的其他標準之間,因爲我已經在上面做了?我上面發佈的語法是錯誤的。 – Deprecated

+0

@ user1821973當然是。你的錯誤是你在別的時候。你只需要什麼時候。其他的就是「全部」。例如,當最大(年齡)> 60時,則...當最大(年齡)在30和60之間時,則...等等,並在末尾有可選的「其他」。 – DazzaL

+0

太棒了!這工作,謝謝。 – Deprecated

相關問題