0

如何在CASE语句中添加默认子句?我有以下查询:

DECLARE @SortColumn VARCHAR(50)

SELECT *
FROM @GroupList
ORDER BY 
    CASE WHEN @SortColumn = 'GroupName ASC' THEN GroupName END ASC,
    CASE WHEN @SortColumn = 'GroupName DESC' THEN GroupName END DESC,
    CASE WHEN @SortColumn = 'LCC ASC' THEN LiveConnectEmailClosure END ASC, 
    CASE WHEN @SortColumn = 'LCC DESC' THEN LiveConnectEmailClosure END DESC, 
    CASE WHEN @SortColumn = 'ES ASC' THEN EnableSharing END ASC,
    CASE WHEN @SortColumn = 'ES DESC' THEN EnableSharing END DESC,
    CASE WHEN @SortColumn = 'Description ASC' THEN GroupDescription END ASC, 
    CASE WHEN @SortColumn = 'Description DESC' THEN GroupDescription END DESC

    ELSE GroupName END ASC; -- this one doesn't work
4

2 回答 2

2

好的,这是我为你准备的最好的你仍然需要改变 1 或 2 件事

假设:

create table GroupList (GroupName varchar(50),
                  LiveConnectEmailClosure varchar(50),
                  EnableSharing varchar(50),
                  GroupDescription varchar(50));

insert into GroupList values('GroupName1','YES','True','SomeDescrition');
insert into GroupList values('GroupName1','YES','True','SomeDescrition');
insert into GroupList values('GroupName1','NO','False','SomeDescrition');
insert into GroupList values('GroupName2','YES','True','SomeDescrition');
insert into GroupList values('GroupName2','YES','False','SomeDescrition');
insert into GroupList values('GroupName3','NO','True','SomeDescrition');
insert into GroupList values('GroupName3','NO','False','SomeDescrition');
insert into GroupList values('GroupName4','NO','True','SomeDescrition');
insert into GroupList values('GroupName4','NO','True','SomeDescrition');
insert into GroupList values('GroupName4','YES','False','SomeDescrition');

然后

SELECT *
FROM GroupList
WHERE
CASE @SortColumn
WHEN 'GroupName ASC' THEN GroupName END ASC,
WHEN 'GroupName DESC' THEN GroupName END DESC,
WHEN 'LCC ASC' THEN LiveConnectEmailClosure END ASC, 
WHEN 'LCC DESC' THEN LiveConnectEmailClosure END DESC, 
WHEN 'ES ASC' THEN EnableSharing END ASC,
WHEN 'ES DESC' THEN EnableSharing END DESC,
WHEN 'Description ASC' THEN GroupDescription END ASC, 
WHEN 'Description DESC' THEN GroupDescription END DESC
ELSE GroupName END ASC;

这是我能用 oa T-sql 编辑器做的最好的希望这有帮助

除此之外:请看Florian Reischl 的回答

SELECT
   CASE 
   WHEN xyz.something = 1 THEN 'SOMETEXT'
   WHEN xyz.somethingelse = 1 THEN 'SOMEOTHERTEXT'
   WHEN xyz.somethingelseagain = 2 THEN 'SOMEOTHERTEXTGOESHERE'
   ELSE 'SOMETHING UNKNOWN'
   END AS ColumnName;
于 2013-08-19T16:18:51.667 回答
2
SELECT list
     , of
     , columns
     , GroupName
FROM   (
        SELECT list
             , of
             , columns
             , GroupName
             , CASE @SortColumn
                  WHEN 'GroupName ASC'    THEN GroupName
                  WHEN 'LCC ASC'          THEN LiveConnectEmailClosure
                  WHEN 'ES ASC'           THEN EnableSharing
                  WHEN 'Description ASC'  THEN GroupDescription
                END As ascending_order
              , CASE @SortColumn
                  WHEN 'GroupName DESC'   THEN GroupName
                  WHEN 'LCC DeSC'         THEN LiveConnectEmailClosure
                  WHEN 'ES DESC'          THEN EnableSharing
                  WHEN 'Description DESC' THEN GroupDescription
                END As descending_order
        FROM   @GroupList
       ) As x
ORDER
    BY CASE WHEN ascending_order IS NULL AND descending_order IS NULL THEN GroupName END ASC
     , ascending_order  ASC
     , descending_order DESC

备选方案:

DECLARE @SortDirection char(4) = 'ASC';

IF @SortColumn LIKE '%DESC' THEN
  BEGIN
    SET @SortDirection = 'DESC';
  END;

SET @SortColumn = Replace(Replace(@SortColumn, ' DESC', ''), ' ASC', '');

SELECT list
     , of
     , columns
FROM   (
        SELECT list
             , of
             , columns
             , CASE @SortColumn
                  WHEN 'GroupName'   THEN GroupName
                  WHEN 'LCC'         THEN LiveConnectEmailClosure
                  WHEN 'ES'          THEN EnableSharing
                  WHEN 'Description' THEN GroupDescription
                  ELSE GroupName
                END As sort_column
        FROM   @GroupList
       ) As x
ORDER
    BY CASE WHEN @SortDirection =  'ASC' THEN sort_column END  ASC
     , CASE WHEN @SortDirection = 'DESC' THEN sort_column END DESC;
于 2013-08-19T16:05:34.677 回答