2

I need to write a select which will list all of the Clients however here is trick if client has status 1 or 2 it should mark this client with * before name. It should looks like

  Vasya Pupkin 
* Masha Pupkina

select looks like

select FirstName + ' '+ LastName, Address, DOB
from Clients 
Order By FirstName 

that means Masha is active client. Spend almost 2 hours for searching in internet but cannot find anything useful. Because of that asking question here.

4

2 に答える 2

3

ステータスフィールドを確認するには、CASE構造の使用に依存する必要があります。

SQLFiddleのサンプルデータでこの完全に機能するコードを確認してください

SELECT (CASE
             WHEN status IN (1, 2) THEN '* '
             ELSE ''
        END) + FirstName + ' '+ LastName as Client_List
  FROM Clients
 ORDER BY FirstName

データ:

[id]  [status]  [FirstName]  [LastName]
 1        3        Vasya       Pupkin
 2        2        Masha       Pupkina
 3        3        Sasha       Alexeivich
 4        1        Katya       Alexeivna

結果:

CLIENT_LIST
* Katya Alexeivna
* Masha Pupkina
Sasha Alexeivich
Vasya Pupkin

痛い編集 !遅すぎる、muhmudの答えは正しい

于 2013-02-27T08:47:24.660 に答える
3
select (case when status in (1, 2) then '* ' else '' end) + FirstName + ' '+ LastName, Address, DOB
from Clients 
Order By FirstName
于 2013-02-27T08:36:58.317 に答える