根据别名列名进行过滤

发布于 2024-08-22 15:47:58 字数 430 浏览 6 评论 0原文

我正在使用 SqlServer 2005,并且有一个我命名的列。

查询类似于:

SELECT id, CASE WHEN <snip extensive column definition> END AS myAlias
FROM myTable
WHERE myAlias IS NOT NULL

但是,这给了我错误:

“列名‘myAlias’无效。”

有办法解决这个问题吗?过去,我在 WHERE 或 HAVING 部分中包含了列定义,但这些大多都很简单,IE COUNT(*) 或其他什么。我可以在此临时查询中包含整个列定义,但如果由于某种原因我需要在生产查询中执行此操作,我宁愿只将列定义包含一次,这样我就不必同时更新两者(并且忘记在某个时候做一件事)

I'm using SqlServer 2005 and I have a column that I named.

The query is something like:

SELECT id, CASE WHEN <snip extensive column definition> END AS myAlias
FROM myTable
WHERE myAlias IS NOT NULL

However, this gives me the error:

"Invalid column name 'myAlias'."

Is there a way to get around this? In the past I've included the column definition in either the WHERE or the HAVING section, but those were mostly simple, IE COUNT(*) or whatever. I can include the whole column definition in this ad-hoc query, but if for some reason I needed to do this in a production query I'd prefer to have the column definition only once so I don't have to update both (and forget to do one at some point)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

妄想挽回 2024-08-29 15:47:58

您不能在这样的 where 子句中引用别名...您要么必须在 WHERE 中复制 CASE,要么可以使用如下子查询:

SELECT id, myAlias
FROM
(
    SELECT id, CASE WHEN <snip extensive column definition> END AS myAlias
    FROM myTable
) data
WHERE myAlias IS NOT NULL

You can't reference aliases in a where clause like that... you either have to duplicate the CASE in the WHERE, or you can use a subquery like this:

SELECT id, myAlias
FROM
(
    SELECT id, CASE WHEN <snip extensive column definition> END AS myAlias
    FROM myTable
) data
WHERE myAlias IS NOT NULL
缘字诀 2024-08-29 15:47:58

使用 CTE 也是一种选择:

;with cte (id, myAlias)
 as (select id, case when <snip extensive column definition> end as myAlias 
      from myTable)
 select id, myAlias
  from cte
  where myAlias is not null

Using CTEs is also an option:

;with cte (id, myAlias)
 as (select id, case when <snip extensive column definition> end as myAlias 
      from myTable)
 select id, myAlias
  from cte
  where myAlias is not null
征棹 2024-08-29 15:47:58

将相同的 CASE 语句放在 WHERE 子句中:

SELECT id, CASE WHEN <snip extensive column definition> END AS myAlias
FROM myTable
WHERE CASE WHEN <snip extensive column definition> END IS NOT NULL

EDIT

另一种选择是嵌套查询:(

SELECT id, myAlias
FROM (
    SELECT id, CASE WHEN <snip extensive column definition> END AS myAlias
    FROM myTable
) AS subTable
WHERE myAlias IS NOT NULL

编辑:删除 HAVING 选项,因为这是不正确的(感谢@OMG Ponies))

Put the same CASE statement in the WHERE clause:

SELECT id, CASE WHEN <snip extensive column definition> END AS myAlias
FROM myTable
WHERE CASE WHEN <snip extensive column definition> END IS NOT NULL

EDIT

Another option is to nest the query:

SELECT id, myAlias
FROM (
    SELECT id, CASE WHEN <snip extensive column definition> END AS myAlias
    FROM myTable
) AS subTable
WHERE myAlias IS NOT NULL

(Edit: removed HAVING option, as this was incorrect (thanks @OMG Ponies))

奶气 2024-08-29 15:47:58

把箱子放在哪里。 SQL Server 将足够聪明,只需对其进行一次评估,这样您就不必真正重复代码:

SELECT id, CASE WHEN <snip extensive column definition> END AS myAlias
FROM myTable
WHERE CASE WHEN <snip extensive column definition> END IS NOT NULL

您可以将其包装在派生表中:

SELECT dt.id, dt.myAlias
    FROM (
          SELECT id, CASE WHEN <snip extensive column definition> END AS myAlias
          FROM myTable
         ) dt
    WHERE dt.myAlias IS NOT NULL

但是,我尽量避免使用没有限制性 WHERE 的派生表。您可以尝试一下,看看是否影响性能。

put the case in the where. SQL Server will be smart enough to just evaluate it one time so you aren't really duplicating the code:

SELECT id, CASE WHEN <snip extensive column definition> END AS myAlias
FROM myTable
WHERE CASE WHEN <snip extensive column definition> END IS NOT NULL

you could wrap it in a derived table:

SELECT dt.id, dt.myAlias
    FROM (
          SELECT id, CASE WHEN <snip extensive column definition> END AS myAlias
          FROM myTable
         ) dt
    WHERE dt.myAlias IS NOT NULL

However, I try to avoid having derived tables without a restrictive WHERE. You can try it to see if it affects performance or not.

野稚 2024-08-29 15:47:58

我最终创建了一个临时表来执行此操作。这里有一些伪代码可以给您一个想法。这适用于复杂的连接,我在这里仅展示一个简单的案例。

--Check to see if the temp table already exists
If(OBJECT_ID('tempdb..#TempTable') Is Not Null)
Begin
    DROP TABLE #TempTable
End

--Create the temp table
CREATE TABLE #TempTable
{
     YourValues NVARCHAR(100)
}

--Insert your data into the temp table        
INSERT INTO #TempTable(YourValues)
SELECT yt.Column1 as YourColumnOne FROM YourTable yt

--Query the filtered data based on the aliased column    
SELECT * 
FROM #TempTable
WHERE YourColumnOne = 'DataToFilterOn'

--Don't forget to remove the temp table
DROP TABLE #TempTable

I ended up creating a temp table to do this. Here is some pseudo code to give you an idea. This worked with a complex join, I am just showing a simple case here.

--Check to see if the temp table already exists
If(OBJECT_ID('tempdb..#TempTable') Is Not Null)
Begin
    DROP TABLE #TempTable
End

--Create the temp table
CREATE TABLE #TempTable
{
     YourValues NVARCHAR(100)
}

--Insert your data into the temp table        
INSERT INTO #TempTable(YourValues)
SELECT yt.Column1 as YourColumnOne FROM YourTable yt

--Query the filtered data based on the aliased column    
SELECT * 
FROM #TempTable
WHERE YourColumnOne = 'DataToFilterOn'

--Don't forget to remove the temp table
DROP TABLE #TempTable
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文