繁体   English   中英

从嵌套选择T-SQL中选择

[英]Select from nested select T-SQL

我希望通过在内部查询中选择它们来修改数据并计算其中一个被修改..它给出错误..

select count(cvs) from
(
  select 
  cvs,
  (case Citycode when 123 then 'test' else 'other' end) as CityName ,
  (case ProductCode when '000' then 'test3' when 'ss' then 'xtr' else 'ddd' end) as CardName
  from Applications
)

你需要给子查询一个别名:

select count(x.cvs) from
(
  select 
  cvs,
  (case Citycode when 123 then 'test' else 'other' end) as CityName ,
  (case ProductCode when '000' then 'test3' when 'ss' then 'xtr' else 'ddd' end) as CardName
  from Applications
) x

为什么不这样做呢?

SELECT COUNT(cvs)
    FROM Applications

您的查询似乎可以简化为..

SELECT COUNT(cvs) FROM Applications

您是否有选择嵌套的原因而忽略了所选的其他列?

我看到的两件事:

1 - 您不需要嵌套子查询来处理示例中的操作。 你可以轻松地做到:

SELECT COUNT(cvs) FROM application

2 - 您需要子查询的别名,如(<subquery>) as SubQ

暂无
暂无

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

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