在SQL中实现案例

时间:2013-10-09 08:45:05

标签: sql tsql sql-server-2008-r2 case

大家好,我有这个疑问:

SELECT B.fld_ActionName,
    ISNULL(A.COUNT, 0) count,
    ISNULL(A.GRAMS, 0) grams,
    ISNULL(A.PRINCIPAL, 0) principal
FROM #PULLEDOUT A
RIGHT JOIN Reference.tbl_RefAction B
    ON A.OSD = B.fld_ActionID
WHERE B.fld_ActionID IN (100, 200, 360, 454, 457)

我希望将B.fld_ActionName上的字词转换为以下内容

New         (still the same no change)
Checked     (still the same no change)
For Recall -->recall
EL -->indexed EL
Watch -->Scrap watch

有可能吗?如果是,那怎么样?我真的不知道案例是否适用于此,我是TSQL的新手,我不知道如何实现它。

谢谢

2 个答案:

答案 0 :(得分:2)

SELECT 
    CASE
        WHEN B.fld_ActionName IN ('New','Checked') THEN B.fld_ActionName
        WHEN B.fld_ActionName 'For Recall' THEN 'recall'
        WHEN B.fld_ActionName 'EL' THEN 'indexed EL'
        WHEN B.fld_ActionName 'Watch' THEN 'Scrap watch'
    END,
    ISNULL(A.COUNT,0) count,
    ISNULL(A.GRAMS,0) grams,
    ISNULL(A.PRINCIPAL,0) principal
FROM #PULLEDOUT A
     RIGHT JOIN Reference.tbl_RefAction B
         ON A.OSD =B.fld_ActionID
WHERE B.fld_ActionID in (100,200,360,454,457)

答案 1 :(得分:1)

试试这个:

SELECT
  CASE b.fld_ActionName
   WHEN 'For Recall' THEN 'recall'
   WHEN 'EL' THEN 'indexed EL'
   WHEN 'Watch' THEN 'Scrap watch'
   ELSE b.fld_ActionName 
  END AS 'Action',
  ISNULL(A.COUNT,0) AS 'count',
  ISNULL(A.GRAMS,0) AS 'grams',
  ISNULL(A.PRINCIPAL,0) AS 'principal' 
FROM #PULLEDOUT A
RIGHT JOIN Reference.tbl_RefAction B
ON A.OSD =B.fld_ActionID
WHERE B.fld_ActionID in (100,200,360,454,457)