简体   繁体   English

“无效的列名称”,“子查询的别名上的排序依据”

[英]“Invalid column name”, Order By on Alias of subquery

I've created a stored procedure where i want to add an alternative order by clause. 我已经创建了一个存储过程,我想在其中添加一个替代order by子句。 The problen is that the query failed on a "Invalid column name 'aantal regels'" 问题是查询失败了“无效的列名''aantal regels'”

Here is the query I have now. 这是我现在的查询。

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

Hope someone can help me with it! 希望有人可以帮助我!

You can't order by the alias in that way. 你不能以这种方式通过别名订购。

The first option is to repeat the code. 第一种选择是重复代码。 Note: Just because you repeat the code, the SQL Engine isn't so naive as to execute it again, it re-uses the results. 注意:仅仅因为重复代码,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

Or so it all using a sub query... 或者全部使用子查询......

  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

The short version is that, while ORDER BY itself can use aliases quite happily, a CASE statement in the ORDER BY cannot. 简短的版本是,虽然ORDER BY本身可以非常愉快地使用别名,但ORDER BY中的CASE语句却不能。 The CASE statements would be evaluated as part of the SELECT, and thus before any aliases are taken into account. CASE语句将作为SELECT的一部分进行评估,因此在考虑任何别名之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM