• 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的子查询,每一列都要起个列名,否者会报错!

    原文链接

  • 相关阅读:
    API
    MVC判断是否登录
    MVC收藏店铺
    MVC显示界面
    MVC登录
    MVC登录跳转到显示
    MVC退单
    MVC判断登录
    Oracle 千位符转换,及格式转换
    【转】Java 服务端 和 C# 客户端 实现 Socket 通信
  • 原文地址:https://www.cnblogs.com/OpenCoder/p/9851950.html
Copyright © 2020-2023  润新知