0

例如,我有

SELECT SUBSTRING([Path], 0, 5) AS [Directory]
      , COUNT([Directory])
FROM [MyDatabase]
GROUP BY [Directory];

但我收到“无效的列名”错误。

4

2 回答 2

3

这应该有效:

SELECT SUBSTRING([Path], 0, 5) AS [Directory]
      , COUNT(1)
FROM [MyDatabase]
GROUP BY SUBSTRING([Path], 0, 5);
于 2013-06-17T19:14:22.990 回答
1

问题是 count() 中使用的列名中的别名,我也不明白为什么在计算时需要两列。尝试

SELECT COUNT(SUBSTRING([Path], 0, 5))      
    FROM [MyDatabase];

如果你需要两列,那么在这里:

   SELECT SUBSTRING([Path], 0, 5) AS [Directory]
  , COUNT(SUBSTRING([Path], 0, 5) )
   FROM [MyDatabase]
   GROUP BY SUBSTRING([Path], 0, 5);
于 2013-06-17T19:25:23.960 回答