1

次の SQL クエリがあります。

SELECT  w.financial_year ,
        ISNULL(e.entity_name, 'Entity code ' + CAST(w.integer_1 AS VARCHAR) + ' is not available on the entity table. Please add.') ,
        COUNT(w.form_guid)
FROM    portal.workflow AS w
        LEFT OUTER JOIN property.entity AS e -- MS: Do not make inner join! Left outer join allows for exceptions to this case to be handled. Important as source data doesn't use property.entity
            ON w.integer_1 = e.entity_id
GROUP BY
        w.financial_year ,
        w.integer_1 ,
        e.entity_name
ORDER BY
        w.financial_year , e.entity_name

私の順序付けでは、最初に e.entity_name が null の場合を示し、次に残りの列をアルファベット順に並べ替えたいと思います..そのようなことは可能ですか?

4

3 に答える 3

3

もちろん、

  SELECT w.financial_year,
    ISNULL(e.entity_name, 'Entity code ' + CAST(w.integer_1 AS VARCHAR) + 
        ' is not available on the entity table. Please add.') ,
    COUNT(w.form_guid)
  FROM portal.workflow AS w
     LEFT JOIN property.entity AS e
         ON w.integer_1 = e.entity_id
  GROUP BY case When e.entity_name Is Null Then 0 Else 1 End,
        w.financial_year, w.integer_1, e.entity_name
  ORDER BY case When e.entity_name Is Null Then 0 Else 1 End, 
      w.financial_year, e.entity_name
于 2012-11-29T04:36:42.503 に答える
2

これを試すことができます:

ORDER BY
        CASE WHEN e.entity_name IS NULL 
             THEN 0 
             ELSE w.financial_year END 
        ,e.entity_name

このSQLFiddleを参照してください

于 2012-11-29T04:47:23.600 に答える
0

次のように、句 ORDER BY で NULLS FIRST または NULLS LAST を使用します。

SELECT  w.financial_year ,
        ISNULL(e.entity_name, 'Entity code ' + CAST(w.integer_1 AS VARCHAR) + ' is not available on the entity table. Please add.') ,
        COUNT(w.form_guid)
FROM    portal.workflow AS w
        LEFT OUTER JOIN property.entity AS e -- MS: Do not make inner join! Left outer join allows for exceptions to this case to be handled. Important as source data doesn't use property.entity
            ON w.integer_1 = e.entity_id
GROUP BY
        w.financial_year ,
        w.integer_1 ,
        e.entity_name
ORDER BY
        w.financial_year , e.entity_name NULLS FIRST
于 2012-11-29T04:58:46.363 に答える