0

如何在查询中放置两个选择,并添加/更改列?希望在下面解释我正在尝试做的一个例子。

Ex: Table [TESTS]
TESTID   TEST
   1     Login
   2     Password
   3     Create Account
   4     Edit Account
   5     Close Account

Would like to end up with a query result:
TestType   TESTID     TEST
  Init        1       Login
  Init        2       Pasword
  Func        3       Create Account
  Func        4       Edit Account
  Func        5       Close Account

/* This doesn't work: */
select 'Init' as [TestType], TESTID, TEST from TESTS where (TESTID >= 1 AND TESTID <= 2)
select 'Func' as [TestType], TESTID, TEST from TESTS where (TESTID >= 3 AND TESTID <= 5)
4

1 回答 1

1

尝试使用案例:

select 
   CASE WHEN (TESTID = 1 OR TESTID = 2) THEN 'Init' ELSE 'Func' END TestType
 , TESTID
 , TEST 
 from 
   TESTS 
于 2012-12-07T04:32:58.643 回答