[英]Why am I getting invalid column name error in group by?
I am trying to make this request.我正在尝试提出这个要求。 I added the group by because I had duplicates.我添加了分组依据,因为我有重复项。 I am getting the error:我收到错误:
Invalid column name for the columns in the group by.分组依据中列的列名称无效。
I have read a lot of posts regarding this type of error but I am still stuck.我已经阅读了很多关于此类错误的帖子,但我仍然被卡住了。 Also I would like to know if I should use sum()
.我也想知道我是否应该使用sum()
。 Help please.请帮助。
INSERT INTO tabA
([load_number]
,[load_date]
,[no_command]
,[no_document]
,[id_transc]
,[division_cd]
,[activity_cd]
,[project_cd])
select 1 [load_number]
,getdate() [load_date]
,'' [no_command]
,'' [no_document]
,coalesce(c.id_numb,-1) [id_transc]
,coalesce(b.elem_cd,-1) [division_cd]
,coalesce(d.budget_cd,-1) [activity_cd]
,'' [project_cd]
from tabB b
left join tabC c on c.credit = b.account
left join tabD d on d.activity = SUBSTRING([b.name],CHARINDEX('-',[b.name])+1,LEN([b.name])) and d.transc = '1010'
group by [load_number]
,[load_date]
,[no_command]
,[no_document]
,[id_transc]
,[division_cd]
,[activity_cd]
,[project_cd]
If you are concerned about duplicates, you can just use select distinct
:如果您担心重复,您可以使用select distinct
:
select distinct 1 [load_number],
getdate() [load_date],
'' [no_command],
'' [no_document],
coalesce(c.id_numb,-1) [id_transc],
coalesce(b.elem_cd,-1) [division_cd],
coalesce(d.budget_cd,-1) [activity_cd],
'' [project_cd]
from tabB b left join
tabC c
on c.credit = b.account left join
tabD d
on d.activity = SUBSTRING([b.name],CHARINDEX('-',[b.name])+1,LEN([b.name])) and d.transc = '1010';
You can't use column aliases defined in the select
in the group by
in SQL Server.您不能在 SQL Server 的group by
中使用select
中定义的列别名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.