Raz*_*t4x 22 sql sql-server group-by calculated-columns common-table-expression
我有这个查询,但它不能正常工作,
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
Run Code Online (Sandbox Code Playgroud)
当我跑这个,我得到了
Msg 8155,Level 16,State 2,Line 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
Run Code Online (Sandbox Code Playgroud)
我明白了
Msg 8155,Level 16,State 2,Line 1
没有为'd'的第1 列指定列.
Msg 8155,Level 16,State 2,Line 1
没有为'd'的第2列指定列.
Tie*_*ran 24
您只需要在CTE中为聚合列提供别名
d as (SELECT
duration,
sum(totalitems) as sumtotalitems
FROM
[DrySoftBranch].[dbo].[mnthItemWiseTotalQty] ('1') AS BkdQty
group by duration
)
Run Code Online (Sandbox Code Playgroud)
Vik*_*dor 18
[编辑]
我试图重写您的查询,但是一旦您将别名与定义'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
Run Code Online (Sandbox Code Playgroud)
第二个:
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
Run Code Online (Sandbox Code Playgroud)
小智 9
单个 with 子句可以通过用逗号分隔来引入多个查询名称,但每个列都必须有一个名称
在本例中,第二个查询有一列没有:
as (SELECT
duration,
sum(totalitems) --**HERE IS A MISSING NAME**
FROM ...
Run Code Online (Sandbox Code Playgroud)
就这样。
我有一个类似的查询和一个类似的问题。
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
Run Code Online (Sandbox Code Playgroud)
我发现SQL Server阻塞了该COUNT(*)
列,并给我错误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
Run Code Online (Sandbox Code Playgroud)
因为您正在创建表表达式,所以必须指定该表的结构,您可以通过两种方式实现:
1:在选择中,您可以使用原始列名(如第一个示例中所示),但对于聚合,您必须使用别名(也在冲突名称中)。喜欢
sum(totalitems) as bkdqty
Run Code Online (Sandbox Code Playgroud)
2:您需要在talbe名称之后指定列名rigth,然后您只需要注意名称的计数应该与查询中选择的库尔数相匹配。喜欢:
d (duration, bkdqty)
AS (Select.... )
Run Code Online (Sandbox Code Playgroud)
使用第二个解决方案,您的两个查询都将起作用!
相当直观的错误信息 - 只需要给出 d 中的列名称
更改为
d as
(
select
[duration] = month(clothdeliverydate),
[bkdqty] = SUM(CONVERT(INT, deliveredqty))
FROM
barcodetable
where
month(clothdeliverydate) is not null
group by month(clothdeliverydate)
)
Run Code Online (Sandbox Code Playgroud)
或者您可以在 cte 的定义中显式声明字段:
d ([duration], [bkdqty]) as
(
select
month(clothdeliverydate),
SUM(CONVERT(INT, deliveredqty))
FROM
barcodetable
where
month(clothdeliverydate) is not null
group by month(clothdeliverydate)
)
Run Code Online (Sandbox Code Playgroud)