41

我有这个查询,但它不能正常工作,

with c as (select 
               month(bookingdate) as duration, 
               count(*) as totalbookings 
           from 
               entbookings
           group by month(bookingdate)
          ),
     d as (SELECT 
               duration, 
               sum(totalitems) 
           FROM 
               [DrySoftBranch].[dbo].[mnthItemWiseTotalQty] ('1') AS BkdQty
           group by duration
          )

select 
    c.duration, 
    c.totalbookings, 
    d.bkdqty 
from
    c 
    inner join d 
    on c.duration = d.duration

当我运行这个时,我得到

消息 8155,级别 16,状态 2,第 1 行
没有为“d”的第 2 列指定列。

谁能告诉我我做错了什么?

另外,当我运行这个时,

with c as (select 
               month(bookingdate) as duration, 
               count(*) as totalbookings 
           from 
               entbookings
           group by month(bookingdate)
          ),
     d as (select 
               month(clothdeliverydate), 
               SUM(CONVERT(INT, deliveredqty)) 
           FROM 
               barcodetable
           where 
               month(clothdeliverydate) is not null
               group by month(clothdeliverydate)
          )

select 
    c.duration, 
    c.totalbookings, 
    d.bkdqty 
from
    c 
    inner join d 
    on c.duration = d.duration

我明白了

消息 8155,级别 16,状态 2,第 1 行
没有为“d”的第 1 列指定列。
消息 8155,级别 16,状态 2,第 1 行
没有为“d”的第 2 列指定列。

4

9 回答 9

33

您只需为 CTE 中的聚合列提供别名

d as (SELECT 
   duration, 
   sum(totalitems) as sumtotalitems
FROM 
   [DrySoftBranch].[dbo].[mnthItemWiseTotalQty] ('1') AS BkdQty
group by duration
)
于 2012-08-25T04:23:28.000 回答
32

[编辑]

我试图重写您的查询,但是一旦您将别名与定义“d”的查询中的聚合列相关联,即使您的查询也将起作用。


我认为您正在寻找以下内容:

第一:

select 
    c.duration, 
    c.totalbookings, 
    d.bkdqty 
from
    (select 
               month(bookingdate) as duration, 
               count(*) as totalbookings 
           from 
               entbookings
           group by month(bookingdate)
    ) AS c 
    inner join 
    (SELECT 
               duration, 
               sum(totalitems) 'bkdqty'
           FROM 
               [DrySoftBranch].[dbo].[mnthItemWiseTotalQty] ('1') AS BkdQty
           group by duration
    ) AS d 
    on c.duration = d.duration

第二个:

select 
    c.duration, 
    c.totalbookings, 
    d.bkdqty 
from
    (select 
               month(bookingdate) as duration, 
               count(*) as totalbookings 
           from 
               entbookings
           group by month(bookingdate)
    ) AS c 
    inner join 
    (select 
               month(clothdeliverydate) 'clothdeliverydatemonth', 
               SUM(CONVERT(INT, deliveredqty)) 'bkdqty'
           FROM 
               barcodetable
           where 
               month(clothdeliverydate) is not null
               group by month(clothdeliverydate)
    ) AS d 
    on c.duration = d.duration
于 2012-08-25T04:14:47.897 回答
18

我有一个类似的查询和类似的问题。

SELECT
    *
FROM
    Users ru
    LEFT OUTER JOIN 
    (
        SELECT ru1.UserID, COUNT(*)
        FROM Referral r
        LEFT OUTER JOIN Users ru1 ON r.ReferredUserId = ru1.UserID
        GROUP BY ru1.UserID
    ) ReferralTotalCount ON ru.UserID = ReferralTotalCount.UserID

我发现 SQL Server 在COUNT(*)列上阻塞,并给我错误No column was specified for column 2

在列上添加别名COUNT(*)解决了这个问题。

  SELECT
        *
    FROM
        Users ru
        LEFT OUTER JOIN 
        (
            SELECT ru1.UserID, COUNT(*) AS -->MyCount<--
            FROM Referral r
            LEFT OUTER JOIN Users ru1 ON r.ReferredUserId = ru1.UserID
            GROUP BY ru1.UserID
        ) ReferralTotalCount ON ru.UserID = ReferralTotalCount.UserID
于 2017-03-02T04:02:07.133 回答
6

因为您正在创建一个表表达式,所以您必须指定该表的结构,您可以通过两种方式实现:

1:在选择中,您可以使用原始列名(如在第一个示例中),但对于聚合,您必须使用别名(也在冲突的名称中)。喜欢

sum(totalitems) as bkdqty

2:您需要在表格名称之后指定列名,然后您只需注意名称的数量应该与查询中选择的库尔数相匹配。喜欢:

d (duration, bkdqty) 
AS (Select.... ) 

使用第二种解决方案,您的两个查询都将起作用!

于 2012-08-25T04:37:25.270 回答
5

很直观的错误信息——只需要给出 d 名称中的列

更改为此

d as 
 (
  select                 
     [duration] = month(clothdeliverydate),                 
     [bkdqty] = SUM(CONVERT(INT, deliveredqty))             
  FROM                 
     barcodetable            
  where                 
     month(clothdeliverydate) is not null                
  group by month(clothdeliverydate)           
 ) 

或者您可以在 cte 的定义中显式声明字段:

d ([duration], [bkdqty]) as 
 (
  select                 
     month(clothdeliverydate),                 
     SUM(CONVERT(INT, deliveredqty))             
  FROM                 
     barcodetable            
  where                 
     month(clothdeliverydate) is not null                
  group by month(clothdeliverydate)           
 ) 
于 2012-08-25T10:49:55.123 回答
4

只需添加一个别名,如下
sum(totalitems) 作为 totalitems。

于 2013-01-22T07:34:21.503 回答
3

单个 with 子句可以通过用逗号分隔多个查询名称来引入多个查询名称,但每个列都必须有一个名称

在这种情况下,第二个查询有一列没有:

 as (SELECT 
               duration, 
               sum(totalitems) --**HERE IS A MISSING NAME**
 FROM ...

就这样。

于 2021-08-15T01:21:56.353 回答
1

显然,如解析器响应中所述,两种情况都需要列名。在任何一个版本中,“d”的列都没有命名。

情况 1:您的 d 的第 2 列sum(totalitems)未命名。duration将保留名称“duration”

情况2:两者month(clothdeliverydate)SUM(CONVERT(INT, deliveredqty))必须命名

于 2014-03-24T12:28:18.130 回答
-1

消息 8155,级别 16,状态 2,第 1 行没有为“d”的第 1 列指定列。消息 8155,级别 16,状态 2,第 1 行没有为“d”的第 2 列指定列。回答:

ROUND(AVG(CAST(column_name AS FLOAT)), 2) as column_name

于 2020-08-25T06:41:06.050 回答