生成的列依赖于其他生成的列

发布于 2024-09-08 08:42:01 字数 247 浏览 6 评论 0原文

这样做的最好方法是什么?

select 'blah' as foo,
        CASE 
          WHEN foo='blah' THEN 'fizz'
          ELSE 'buzz'
        END as bar

正如现在所写的那样,我收到一个无效的列名'foo'错误。无论如何,有没有办法在 select 语句可以用作视图的情况下执行此操作?

What would be the best way of doing this?

select 'blah' as foo,
        CASE 
          WHEN foo='blah' THEN 'fizz'
          ELSE 'buzz'
        END as bar

As it is written right now I get an invalid column name 'foo' error. Is there anyway to do this where the select statement could be used as a view?

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

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

发布评论

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

评论(4

隐诗 2024-09-15 08:42:01

您需要像这样使用嵌套选择:

select foo,
  case when foo='blah'
  then 'fizz'
  else 'buzz'
  end as bar
from ( select 'blah' as foo ) a

问题是同一选择语句中的名称无法识别列 foo。

You need to use a nested select like this:

select foo,
  case when foo='blah'
  then 'fizz'
  else 'buzz'
  end as bar
from ( select 'blah' as foo ) a

The problem is that the column foo is not recognized by the name in the same select statement.

往昔成烟 2024-09-15 08:42:01

如果您不想重复列定义,可以使用派生表。

select   case when foo='blah'
  then 'fizz'
  else 'buzz' 
end as bar
FROM
(
select 'blah' as foo
) derived

或者 CTE

;
With blahs As
(
select 'blah' as foo
)


select   case when foo='blah'
  then 'fizz'
  else 'buzz' 
end as bar
FROM blahs

快速测试显示以下所有三个版本的执行计划都是相同的

SELECT foo,
       CASE
              WHEN foo='blah'
              THEN 'fizz'
              ELSE 'buzz'
       END AS bar
FROM   ( SELECT
               CASE
                       WHEN [number] % 5 = 0
                       THEN 'blah'
                       ELSE 'notblah'
               END AS foo
       FROM    [master].[dbo].[spt_values]
       )
       D ;


WITH blahs AS
     ( SELECT
             CASE
                     WHEN [number] % 5 = 0
                     THEN 'blah'
                     ELSE 'notblah'
             END AS foo
     FROM    [master].[dbo].[spt_values]
     )
SELECT foo,
       CASE
              WHEN foo='blah'
              THEN 'fizz'
              ELSE 'buzz'
       END AS bar
FROM   blahs


SELECT
       CASE
              WHEN [number] % 5 = 0
              THEN 'blah'
              ELSE 'notblah'
       END AS foo,
       CASE
              WHEN
                     CASE
                            WHEN [number] % 5 = 0
                            THEN 'blah'
                            ELSE 'notblah'
                     END='blah'
              THEN 'fizz'
              ELSE 'buzz'
       END AS bar
FROM   [master].[dbo].[spt_values]

You can use a derived table if you don't want to repeat the column definition.

select   case when foo='blah'
  then 'fizz'
  else 'buzz' 
end as bar
FROM
(
select 'blah' as foo
) derived

Or a CTE

;
With blahs As
(
select 'blah' as foo
)


select   case when foo='blah'
  then 'fizz'
  else 'buzz' 
end as bar
FROM blahs

A quick test shows the execution plan for all three versions below are the same

SELECT foo,
       CASE
              WHEN foo='blah'
              THEN 'fizz'
              ELSE 'buzz'
       END AS bar
FROM   ( SELECT
               CASE
                       WHEN [number] % 5 = 0
                       THEN 'blah'
                       ELSE 'notblah'
               END AS foo
       FROM    [master].[dbo].[spt_values]
       )
       D ;


WITH blahs AS
     ( SELECT
             CASE
                     WHEN [number] % 5 = 0
                     THEN 'blah'
                     ELSE 'notblah'
             END AS foo
     FROM    [master].[dbo].[spt_values]
     )
SELECT foo,
       CASE
              WHEN foo='blah'
              THEN 'fizz'
              ELSE 'buzz'
       END AS bar
FROM   blahs


SELECT
       CASE
              WHEN [number] % 5 = 0
              THEN 'blah'
              ELSE 'notblah'
       END AS foo,
       CASE
              WHEN
                     CASE
                            WHEN [number] % 5 = 0
                            THEN 'blah'
                            ELSE 'notblah'
                     END='blah'
              THEN 'fizz'
              ELSE 'buzz'
       END AS bar
FROM   [master].[dbo].[spt_values]
傾旎 2024-09-15 08:42:01
WITH TmpTbl as (SELECT 'blah' as foo)
SELECT foo, CASE WHEN foo='blah' THEN 'fizz' 
            ELSE 'buzz' 
            END as bar FROM TmpTbl
WITH TmpTbl as (SELECT 'blah' as foo)
SELECT foo, CASE WHEN foo='blah' THEN 'fizz' 
            ELSE 'buzz' 
            END as bar FROM TmpTbl
忘你却要生生世世 2024-09-15 08:42:01

您可以使用像这样的表查询:

select a.foo,
  case when a.foo = 'blah'
  then 'fizz'
  else 'buzz'
  end as bar
from (select 'blah' as foo) a

You could possibly use a table query like so:

select a.foo,
  case when a.foo = 'blah'
  then 'fizz'
  else 'buzz'
  end as bar
from (select 'blah' as foo) a
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文