2015-07-20 92 views
1

我從遷移水晶報表在那裏我能夠創建一列,並使用這個公式填...創建一列並填充基於現有數據的SQL

if ({@SD Profile}="null") and ({@HD Profile 2}="null") 
then "" 
else 
    if ({@HD Profile 2}="null") 
    then "SD only" 
    else 
     if ({@SD Profile}="null") 
     then "HD only" 
     else 
      if({@SD Companion}="1") 
      then "HD+SD" 
      else "HD/SD" 

我想完成同樣的事情使用SQL,其中我根據其他3列中的數據填充列的值。請注意,我並沒有在數據庫中添加一列,只是插入到報告中。

我已經在我的使用

CAST(NULL AS VARCHAR(30)) as "Content Type", 
"SD_format"."name" as "SD Format", 
"HD_format"."name" as "HD Format", 
"destination"."sd_companion_required_flag" 

基本上類似... SELECT語句創建的空列。如果SD_Format.name = '空',那麼CONTENT_TYPE = '' 等

+0

可以請你一些樣品d共享表結構ata,以便我們能理解你的問題? –

+0

好吧,我正在加入多個表格,而且我的查詢太長,無法剪切並粘貼到評論中。我更好奇,如果有一種好的方式來說,「如果列a包含一個空白單元格,用c填充列c」如果列A包含任何內容並且列B不包含任何內容,則使用Y填充列c「 – mfisher84

回答

2

你應該能夠取代「if」語句由一個「案例」聲明:

select 
    case 
    when SD_Profile is null and HD_Profile is null then '' 
    when HD_Profile is null then 'SD only' 
    when SD_Profile is null then 'HD only' 
    when Destination = '1' then 'HD + SD' 
    else 'HD/SD' 
    end as 'Content_Type' 
from ... 
+0

謝謝 - 我現在就來試試吧,讓你知道它是如何工作的 – mfisher84

+0

這就像一個魅力,謝謝HashPsi - 欣賞快速反應和堅實的激情。 – mfisher84

0
*"SD_format"."name" as "SD Format", 
"HD_format"."name" as "HD Format", 
    case 
    when "SD_format"."name" is null and "HD_format"."name" is null then '' 
    when "HD_format"."name" is null then 'SD only' 
    when "SD_format"."name" is null then 'HD only' 
    when "destination"."sd_companion_required_flag" = '1' then 'HD + SD' 
    else 'HD/SD' 
    end as "Content_Type",