• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
PowerCoder
博客园    首页    新随笔    联系   管理    订阅  订阅

SQL Server 子查询错误:No column name was specified for column 2 of 'a' error (转载)

问:


I have a MySQL query and I ran it working fine but same query showing error in SQL Server.

SQL Server query:

SELECT 
    COUNT(*) cnt 
FROM 
    (SELECT DISTINCT 
         tc_id, MAX(exn_time), STATUS 
     FROM 
         release_details a, tc_details b  
     WHERE 
         a.project = b.project 
         AND a.tc_id = b.tc_name 
         AND logicaldel = 0 
         AND a.project = 'test' 
     GROUP BY 
         tc_id, STATUS) a 
WHERE 
    a.status = 'PASS';

Error:

No column name was specified for column 2 of 'a'.

How do I modify the above query?

 

答:


Use the Alias name for your inner query.You are getting the MAX(exn_time) but not specified the name for that column that's why throwing the error. And you can use the Joins to the tables to make it more readable.

SELECT COUNT(*) cnt 
FROM (
     SELECT DISTINCT 
         tc_id,
         MAX(exn_time) AS Maxtime ,
         STATUS 
      FROM 
         release_details a JOIN tc_details b  
           ON a.project= b.project 
             AND a.tc_id = b.tc_name 
      WHERE 
           logicaldel = 0  
           AND a.project ='test' 
      GROUP BY 
         tc_id,
         STATUS 
      ) a 
 WHERE a.status='PASS';

 

所以记住SQL Server的子查询,每一列都要起个列名,否者会报错!

 

原文链接

 

posted @ 2018-10-25 19:01  PowerCoder  阅读(3948)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3