4

別のorderby句を追加するストアドプロシージャを作成しました。問題は、「無効な列名'aantalregels'」でクエリが失敗したことです。

これが私が今持っているクエリです。

SELECT 
            l.lead_id,
            l.afdeling_id,
            l.advertentie_id,
            l.naam,
            l.type,
            l.status,
            l.herkomst,
            l.aanmaakdatum,
            l.klant_id,
            l.overigegegevens,
            af.afdelingsnaam,
            (SELECT 
                COUNT(lead_regel_id) 
            FROM 
                Lead_regel As lr 
            Where 
                Lr.lead_id = l.lead_id And 
                lr.status <> 100
            ) 
            AS aantal_regels,

            (SELECT 
                COUNT(lead_id) 
            FROM 
                Lead_unread As lu 
            Where 
                lu.lead_id = l.lead_id And 
                lu.user_id = @uid
            ) 
            As lead_ongelezen,

            (SELECT 
                COUNT(lru.lead_regel_id) 
            FROM 
                Lead_regel As lr2 
            INNER JOIN 
                Lead_regel_unread As lru ON 
                lr2.lead_regel_id = lru.lead_regel_id 
            Where 
                lr2.lead_id = l.lead_id And 
                lru.user_id = @uid And 
                lr2.status <> 100
            ) 
            As lead_regel_ongelezen

        FROM 
            Lead AS l

        INNER JOIN 
            Afdeling AS af ON 
            l.afdeling_id = af.afdeling_id

        WHERE 
            l.afdeling_id = @aid AND 
            l.status <> 100

        ORDER BY
            CASE WHEN @orderby = 'default' THEN l.aanmaakdatum END DESC,
            CASE WHEN @orderby = 'type' THEN l.type END ASC, 
            CASE WHEN @orderby = 'naam' THEN l.naam END ASC,
            CASE WHEN @orderby = 'reacties' THEN aantal_regels END DESC

誰かがそれを手伝ってくれることを願っています!

4

2 に答える 2

6

そのようにエイリアスで注文することはできません。

最初のオプションは、コードを繰り返すことです。注: コードを繰り返したからといって、SQL エンジンは再実行するほど単純ではなく、結果を再利用します。

ORDER BY
  CASE WHEN @orderby = 'default' THEN l.aanmaakdatum END DESC,
  CASE WHEN @orderby = 'type' THEN l.type END ASC, 
  CASE WHEN @orderby = 'naam' THEN l.naam END ASC,
  CASE WHEN @orderby = 'reacties' THEN (SELECT 
                                          COUNT(lead_regel_id) 
                                        FROM 
                                          Lead_regel As lr 
                                        WHERE
                                          Lr.lead_id = l.lead_id And 
                                          Lr.status <> 100
                                       ) END DESC

または、すべてサブクエリを使用して...

  SELECT
    *
  FROM
  (
    yourQuery
  )
    AS sub_query
  ORDER BY
      CASE WHEN @orderby = 'default'  THEN aanmaakdatum  END DESC,
      CASE WHEN @orderby = 'type'     THEN type          END ASC, 
      CASE WHEN @orderby = 'naam'     THEN naam          END ASC,
      CASE WHEN @orderby = 'reacties' THEN aantal_regels END DESC
于 2012-05-29T13:00:50.197 に答える
4

The short version is that, while ORDER BY itself can use aliases quite happily, a CASE statement in the ORDER BY cannot. The CASE statements would be evaluated as part of the SELECT, and thus before any aliases are taken into account.

于 2012-05-29T13:02:57.723 に答える