没有为'a'的第4列指定列名

时间:2017-04-28 03:06:57

标签: sql group-by sql-order-by sql-server-2016 columnname

select a.*, b.remark from 
(
    SELECT 
        [TypeSize] , 
        [ShapeCode] , 
        CASE 
            WHEN c.dima <> 0 THEN 
                 'A=' + CONVERT(varchar, cast(c.dima AS decimal(18, 0)))
            ELSE '' 
          END + 
            CASE 
                WHEN c.dimb <> 0 THEN 
                   ', B=' + CONVERT(varchar, cast(c.dimb AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN c.dimc <> 0 THEN 
                   ', C=' + CONVERT(varchar, cast(c.dimc AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN c.dimd <> 0 THEN 
                   ', D=' + CONVERT(varchar, cast(c.dimd AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN c.dime <> 0 THEN 
                   ', E=' + CONVERT(varchar, cast(c.dime AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN c.dimf <> 0 THEN 
                   ', F=' + CONVERT(varchar, cast(c.dimf AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN c.dimg <> 0 THEN 
                   ', G=' + CONVERT(varchar, cast(c.dimg AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN c.dimn <> 0 THEN 
                   ', N=' + CONVERT(varchar, cast(c.dimn AS decimal(18, 0)))
                ELSE '' 
            END
          AS dimension,
          min(c.CreatedDtTm)
    FROM            bar_schedule_detail c
    WHERE           active = 1 
    AND             barid = @BarId    
    GROUP BY
        [TypeSize] , 
        [ShapeCode] , 
        CASE 
            WHEN dima <> 0 THEN 
                 'A=' + CONVERT(varchar, cast(dima AS decimal(18, 0)))
            ELSE '' 
          END + 
            CASE 
                WHEN dimb <> 0 THEN 
                   ', B=' + CONVERT(varchar, cast(dimb AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN dimc <> 0 THEN 
                   ', C=' + CONVERT(varchar, cast(dimc AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN dimd <> 0 THEN 
                   ', D=' + CONVERT(varchar, cast(dimd AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN dime <> 0 THEN 
                   ', E=' + CONVERT(varchar, cast(dime AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN dimf <> 0 THEN 
                   ', F=' + CONVERT(varchar, cast(dimf AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN dimg <> 0 THEN 
                   ', G=' + CONVERT(varchar, cast(dimg AS decimal(18, 0)))
                ELSE '' 
            END + 
            CASE 
                WHEN dimn <> 0 THEN 
                   ', N=' + CONVERT(varchar, cast(dimn AS decimal(18, 0)))
                ELSE '' 
            END
) as a, bar_schedule_detail b
where a.typesize = b.typesize
and a.shapecode = b.shapecode
and 
(
   CASE 
     WHEN b.dima <> 0 THEN 
          'A=' + CONVERT(varchar, cast(b.dima AS decimal(18, 0)))
     ELSE '' 
   END + 
     CASE 
         WHEN b.dimb <> 0 THEN 
            ', B=' + CONVERT(varchar, cast(b.dimb AS decimal(18, 0)))
         ELSE '' 
     END + 
     CASE 
         WHEN b.dimc <> 0 THEN 
            ', C=' + CONVERT(varchar, cast(b.dimc AS decimal(18, 0)))
         ELSE '' 
     END + 
     CASE 
         WHEN b.dimd <> 0 THEN 
            ', D=' + CONVERT(varchar, cast(b.dimd AS decimal(18, 0)))
         ELSE '' 
     END + 
     CASE 
         WHEN b.dime <> 0 THEN 
            ', E=' + CONVERT(varchar, cast(b.dime AS decimal(18, 0)))
         ELSE '' 
     END + 
     CASE 
         WHEN b.dimf <> 0 THEN 
            ', F=' + CONVERT(varchar, cast(b.dimf AS decimal(18, 0)))
         ELSE '' 
     END + 
     CASE 
         WHEN b.dimg <> 0 THEN 
            ', G=' + CONVERT(varchar, cast(b.dimg AS decimal(18, 0)))
         ELSE '' 
     END + 
     CASE 
         WHEN b.dimn <> 0 THEN 
            ', N=' + CONVERT(varchar, cast(b.dimn AS decimal(18, 0)))
         ELSE '' 
     END
) = a.dimension
and a.CreatedDtTm = b.CreatedDtTm
and b.active = 1 
and b.barid = @BarId 

当我执行此代码时,我收到此错误:

  

没有为'a'的第4列指定列名。

有谁知道这个问题是什么?这部分是我遇到错误的地方:

as a, bar_schedule_detail b
where a.typesize = b.typesize
and a.shapecode = b.shapecode

1 个答案:

答案 0 :(得分:0)

为所有子查询列提供别名:

  SELECT ....., 
         .....,
         ....., MIN(c.CreatedDtTm) AS CreatedDT

在您的子查询SELECT子句中,为min(c.CreatedDtTm)列提供别名