yeafee@夜飛郎

          it's usually better to solve problems with simplicity and finesse rather than muscle.

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            33 隨筆 :: 3 文章 :: 2 評論 :: 0 Trackbacks

          來源:SQL幫助文檔

          CASE
          計算條件列表并返回多個可能結果表達式之一。

          CASE 具有兩種格式:

          簡單 CASE 函數 將某個表達式與一組簡單表達式進行比較以確定結果。
          CASE 搜索函數? 計算一組布爾表達式以確定結果。

          兩種格式都支持可選的 ELSE 參數。

          語法:
          簡單 CASE 函數:

          CASE ?input_expression
          ????
          WHEN ?when_expression? THEN ?result_expression
          ????????
          [ ?n? ]
          ????
          [
          ????????ELSE?else_result_expression
          ????END

          CASE 搜索函數:

          CASE
          ????
          WHEN ?Boolean_expression? THEN ?result_expression
          ????????
          [ ?n? ]
          ????
          [
          ????????ELSE?else_result_expression
          ????END

          參數:

          input_expression 是使用簡單 CASE 格式時所計算的表達式。Input_expression 是任何有效的 Microsoft? SQL Server? 表達式。

          WHEN when_expression 是使用簡單 CASE 格式時 input_expression 所比較的簡單表達式。When_expression 是任意有效的 SQL Server 表達式。Input_expression 和每個 when_expression 的數據類型必須相同,或者是隱性轉換。

          n

          占位符,表明可以使用多個 WHEN when_expression THEN result_expression 子句或 WHEN Boolean_expression THEN result_expression 子句。

          THEN result_expression

          當 input_expression = when_expression 取值為 TRUE,或者 Boolean_expression 取值為 TRUE 時返回的表達式。result_expression 是任意有效的 SQL Server 表達式。

          ELSE else_result_expression

          當比較運算取值不為 TRUE 時返回的表達式。如果省略此參數并且比較運算取值不為 TRUE,CASE 將返回 NULL 值。Else_result_expression 是任意有效的 SQL Server 表達式。Else_result_expression 和所有 result_expression 的數據類型必須相同,或者必須是隱性轉換。

          WHEN Boolean_expression

          使用 CASE 搜索格式時所計算的布爾表達式。Boolean_expression 是任意有效的布爾表達式。

          結果類型
          從 result_expressions 和可選 else_result_expression 的類型集合中返回最高的優先規則類型。有關更多信息,請參見數據類型的優先順序。

          結果值

          簡單 CASE 函數:
          計算 input_expression,然后按指定順序對每個 WHEN 子句的 input_expression = when_expression 進行計算。
          返回第一個取值為 TRUE 的 (input_expression = when_expression) 的 result_expression。


          如果沒有取值為 TRUE 的 input_expression = when_expression,則當指定 ELSE 子句時 SQL Server 將返回 else_result_expression;若沒有指定 ELSE 子句,則返回 NULL 值。

          CASE 搜索函數:
          按指定順序為每個 WHEN 子句的 Boolean_expression 求值。
          返回第一個取值為 TRUE 的 Boolean_expression 的 result_expression。
          如果沒有取值為 TRUE 的 Boolean_expression,則當指定 ELSE 子句時 SQL Server 將返回 else_result_expression;若沒有指定 ELSE 子句,則返回 NULL 值。

          示例

          A. 使用帶有簡單 CASE 函數的 SELECT 語句
          在 SELECT 語句中,簡單 CASE 函數僅檢查是否相等,而不進行其它比較。

          下面的示例使用 CASE 函數更改圖書分類顯示,以使其更易于理解。

          USE ?pubs
          GO
          SELECT ???Category? =
          ??????
          CASE ?type
          ?????????
          WHEN ? ' popular_comp ' ? THEN ? ' Popular?Computing '
          ?????????
          WHEN ? ' mod_cook ' ? THEN ? ' Modern?Cooking '
          ?????????
          WHEN ? ' business ' ? THEN ? ' Business '
          ?????????
          WHEN ? ' psychology ' ? THEN ? ' Psychology '
          ?????????
          WHEN ? ' trad_cook ' ? THEN ? ' Traditional?Cooking '
          ?????????
          ELSE ? ' Not?yet?categorized '
          ??????
          END ,
          ???
          CAST (title? AS ? varchar ( 25 ))? AS ? ' Shortened?Title ' ,
          ???price?
          AS ?Price
          FROM ?titles
          WHERE ?price? IS ? NOT ? NULL
          ORDER ? BY ?type,?price
          COMPUTE ? AVG (price)? BY ?type
          GO

          下面是結果集:

          Category??????????? Shortened Title?????????? Price?????????????????????
          ------------------- ------------------------- --------------------------
          Business??????????? You Can Combat Computer S 2.99??????????????????????
          Business??????????? Cooking with Computers: S 11.95?????????????????????
          Business??????????? The Busy Executive's Data 19.99?????????????????????
          Business??????????? Straight Talk About Compu 19.99?????????????????????

          ????????????????????????????????????????????? avg
          ????????????????????????????????????????????? ==========================
          ????????????????????????????????????????????? 13.73?????????????????????

          Category??????????? Shortened Title?????????? Price?????????????????????
          ------------------- ------------------------- --------------------------
          Modern Cooking????? The Gourmet Microwave???? 2.99??????????????????????
          Modern Cooking????? Silicon Valley Gastronomi 19.99?????????????????????

          ????????????????????????????????????????????? avg
          ????????????????????????????????????????????? ==========================
          ????????????????????????????????????????????? 11.49?????????????????????

          Category??????????? Shortened Title?????????? Price?????????????????????
          ------------------- ------------------------- --------------------------
          Popular Computing?? Secrets of Silicon Valley 20.00?????????????????????
          Popular Computing?? But Is It User Friendly?? 22.95?????????????????????

          ????????????????????????????????????????????? avg
          ????????????????????????????????????????????? ==========================
          ????????????????????????????????????????????? 21.48?????????????????????

          Category??????????? Shortened Title?????????? Price?????????????????????
          ------------------- ------------------------- --------------------------
          Psychology????????? Life Without Fear???????? 7.00??????????????????????
          Psychology????????? Emotional Security: A New 7.99??????????????????????
          Psychology????????? Is Anger the Enemy??????? 10.95?????????????????????
          Psychology????????? Prolonged Data Deprivatio 19.99?????????????????????
          Psychology????????? Computer Phobic AND Non-P 21.59?????????????????????

          ????????????????????????????????????????????? avg
          ????????????????????????????????????????????? ==========================
          ????????????????????????????????????????????? 13.50?????????????????????

          Category??????????? Shortened Title?????????? Price?????????????????????
          ------------------- ------------------------- --------------------------
          Traditional Cooking Fifty Years in Buckingham 11.95?????????????????????
          Traditional Cooking Sushi, Anyone???????????? 14.99?????????????????????
          Traditional Cooking Onions, Leeks, and Garlic 20.95?????????????????????

          ????????????????????????????????????????????? avg
          ????????????????????????????????????????????? ==========================
          ????????????????????????????????????????????? 15.96?????????????????????

          (21 row(s) affected)

          B. 使用帶有簡單 CASE 函數和 CASE 搜索函數的 SELECT 語句
          在 SELECT 語句中,CASE 搜索函數允許根據比較值在結果集內對值進行替換。下面的示例根據圖書的價格范圍將價格(money 列)顯示為文本注釋。

          USE ?pubs
          GO
          SELECT ???? ' Price?Category ' ? =
          ??????
          CASE
          ?????????
          WHEN ?price? IS ? NULL ? THEN ? ' Not?yet?priced '
          ?????????
          WHEN ?price? < ? 10 ? THEN ? ' Very?Reasonable?Title '
          ?????????
          WHEN ?price? >= ? 10 ? and ?price? < ? 20 ? THEN ? ' Coffee?Table?Title '
          ?????????
          ELSE ? ' Expensive?book! '
          ??????
          END ,
          ???
          CAST (title? AS ? varchar ( 20 ))? AS ? ' Shortened?Title '
          FROM ?titles
          ORDER ? BY ?price
          GO

          下面是結果集:

          Price Category??????? Shortened Title?????
          --------------------- --------------------
          Not yet priced??????? Net Etiquette???????
          Not yet priced??????? The Psychology of Co
          Very Reasonable Title The Gourmet Microwav
          Very Reasonable Title You Can Combat Compu
          Very Reasonable Title Life Without Fear???
          Very Reasonable Title Emotional Security:?
          Coffee Table Title??? Is Anger the Enemy??
          Coffee Table Title??? Cooking with Compute
          Coffee Table Title??? Fifty Years in Bucki
          Coffee Table Title??? Sushi, Anyone???????
          Coffee Table Title??? Prolonged Data Depri
          Coffee Table Title??? Silicon Valley Gastr
          Coffee Table Title??? Straight Talk About?
          Coffee Table Title??? The Busy Executive's
          Expensive book!?????? Secrets of Silicon V
          Expensive book!?????? Onions, Leeks, and G
          Expensive book!?????? Computer Phobic And?
          Expensive book!?????? But Is It User Frien

          (18 row(s) affected)

          C. 使用帶有 SUBSTRING 和 SELECT 的 CASE 函數
          下面的示例使用 CASE 和 THEN 生成一個有關作者、圖書標識號和每個作者所著圖書類型的列表。

          USE ?pubs
          SELECT ? SUBSTRING (( RTRIM (a.au_fname)? + ? ' ? ' +
          ???
          RTRIM (a.au_lname)? + ? ' ? ' ),? 1 ,? 25 )? AS ?Name,?a.au_id,?ta.title_id,
          ???Type?
          =
          ??
          CASE
          ????
          WHEN ? SUBSTRING (ta.title_id,? 1 ,? 2 )? = ? ' BU ' ? THEN ? ' Business '
          ????
          WHEN ? SUBSTRING (ta.title_id,? 1 ,? 2 )? = ? ' MC ' ? THEN ? ' Modern?Cooking '
          ????
          WHEN ? SUBSTRING (ta.title_id,? 1 ,? 2 )? = ? ' PC ' ? THEN ? ' Popular?Computing '
          ????
          WHEN ? SUBSTRING (ta.title_id,? 1 ,? 2 )? = ? ' PS ' ? THEN ? ' Psychology '
          ????
          WHEN ? SUBSTRING (ta.title_id,? 1 ,? 2 )? = ? ' TC ' ? THEN ? ' Traditional?Cooking '
          ??
          END
          FROM ?titleauthor?ta? JOIN ?authors?a? ON ?ta.au_id? = ?a.au_id

          下面是結果集:

          Name????????????????????? au_id?????? title_id Type???????????????
          ------------------------- ----------- -------- -------------------
          Johnson White???????????? 172-32-1176 PS3333?? Psychology?????????
          Marjorie Green??????????? 213-46-8915 BU1032?? Business???????????
          Marjorie Green??????????? 213-46-8915 BU2075?? Business???????????
          Cheryl Carson???????????? 238-95-7766 PC1035?? Popular Computing??
          Michael O'Leary?????????? 267-41-2394 BU1111?? Business???????????
          Michael O'Leary?????????? 267-41-2394 TC7777?? Traditional Cooking
          Dean Straight???????????? 274-80-9391 BU7832?? Business???????????
          Abraham Bennet??????????? 409-56-7008 BU1032?? Business???????????
          Ann Dull????????????????? 427-17-2319 PC8888?? Popular Computing??
          Burt Gringlesby?????????? 472-27-2349 TC7777?? Traditional Cooking
          Charlene Locksley???????? 486-29-1786 PC9999?? Popular Computing??
          Charlene Locksley???????? 486-29-1786 PS7777?? Psychology?????????
          Reginald Blotchet-Halls?? 648-92-1872 TC4203?? Traditional Cooking
          Akiko Yokomoto??????????? 672-71-3249 TC7777?? Traditional Cooking
          Innes del Castillo??????? 712-45-1867 MC2222?? Modern Cooking?????
          Michel DeFrance?????????? 722-51-5454 MC3021?? Modern Cooking?????
          Stearns MacFeather??????? 724-80-9391 BU1111?? Business???????????
          Stearns MacFeather??????? 724-80-9391 PS1372?? Psychology?????????
          Livia Karsen????????????? 756-30-7391 PS1372?? Psychology?????????
          Sylvia Panteley?????????? 807-91-6654 TC3218?? Traditional Cooking
          Sheryl Hunter???????????? 846-92-7186 PC8888?? Popular Computing??
          Anne Ringer?????????????? 899-46-2035 MC3021?? Modern Cooking?????
          Anne Ringer?????????????? 899-46-2035 PS2091?? Psychology?????????
          Albert Ringer???????????? 998-72-3567 PS2091?? Psychology?????????
          Albert Ringer???????????? 998-72-3567 PS2106?? Psychology?????????

          (25 row(s) affected)

          ?


          作者Blog: http://blog.csdn.net/kgdiwss/
          posted on 2007-07-11 11:24 @yeafee 閱讀(428) 評論(0)  編輯  收藏 所屬分類: 數據庫
          主站蜘蛛池模板: 任丘市| 石阡县| 剑川县| 安泽县| 高邮市| 德格县| 大冶市| 云龙县| 张家口市| 怀集县| 仲巴县| 延庆县| 伊金霍洛旗| 蓝山县| 奇台县| 大城县| 福海县| 平罗县| 凌云县| 南澳县| 浮山县| 纳雍县| 茌平县| 阜城县| 威远县| 乌拉特前旗| 青海省| 华容县| 鸡东县| 台南市| 连城县| 广元市| 南投县| 张家口市| 射洪县| 镇安县| 岳普湖县| 乌拉特后旗| 金乡县| 镇雄县| 沙坪坝区|