节前送分了:大家帮我看看这句SQL语句错在哪里?

barljy 2008-04-03 09:43:12
Create Table #Test(FID int, FNumber varchar(200),FBillNo varchar(200),FAmount decimal(18,4))
insert into #Test(FID,FNumber,FBillNo)
select distinct t1.FID,t1.FCustomer,t2.FExplanation
from A t1 left join B t2 on t1.FContactID=t2.FID
where t1.FType=4
update t1 set t1.FAmount=(select sum(FAmount) from A where FType=6 and FID=t1.FID) from #Test t1
select * from #Test
drop table #Test
错误提示:列 't1.FID' 在选择列表中无效,因为该列未包含在聚合函数中,并且没有 GROUP BY 子句。
我不太明白,请高手赐教!个人愚见UPDATE语句中无需GROUP BY子句。
...全文
112 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhuhuahuan 2008-04-05
  • 打赏
  • 举报
回复
我觉得嘛你还是在看看。..
qiudan198206 2008-04-05
  • 打赏
  • 举报
回复
如果只是update的話那是不用Group by 的,問題出在
(select sum(FAmount) from A where FType=6 and FID=t1.FID)這里,
這一句你調用了sum聚合函數,而且後面你又用了FID=t1.FID這一條件,而
t1.FID的值并不是唯一的,所以建議在
select sum(FAmount) from A where FType=6 and FID=t1.FID group by FID,
沒有試過不知道對不對.
dl110 2008-04-03
  • 打赏
  • 举报
回复
10楼回答有误:

/* 第2句 */ update #Test set #Test.FAmount=(select sum(FAmount) from A where FType=6 and FID=#Test.FID) -- from #Test t1
-狙击手- 2008-04-03
  • 打赏
  • 举报
回复
可是没有异常呀


Create Table #Test(FID int, FNumber varchar(200),FBillNo varchar(200),FAmount decimal(18,4)) 
create table a (ftype int,fid int,famount int)
go

update t1
set t1.FAmount=(select sum(FAmount) from A where FType=6 and FID=t1.FID)
from #Test t1

drop table #test,a

/*


(所影响的行数为 0 行)


*/
barljy 2008-04-03
  • 打赏
  • 举报
回复
16楼的语句是错误的
pt1314917 2008-04-03
  • 打赏
  • 举报
回复

--try
update #Test set FAmount=(select sum(FAmount) from A where FType=6 and FID=t1.FID) from #Test t1
barljy 2008-04-03
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 happyflystone 的回复:]
update应该没问题


主要是那个insert 列名有点不对劲
[/Quote]
INSERT语句有什么不对啊
我把UPDATE语句去掉,完全没有错误啊
说了错误在UPDATE语句上
-狙击手- 2008-04-03
  • 打赏
  • 举报
回复
update应该没问题


主要是那个insert 列名有点不对劲
pt1314917 2008-04-03
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 barljy 的回复:]
引用 8 楼 pt1314917 的回复:
SQL code
--恩。估计跟二楼说的一样
select distinct t1.FID,t1.FCustomer,t2.FExplanation
from A t1 left join B t2 on t1.FContactID=t2.FID
where t1.FType=4

看A表中是否有FID列。。。

A表中有FID列的,不然插入语句就会报错了
提示错误在UPDATE语句中的FID
[/Quote]

--try
update #Test set FAmount=(select sum(FAmount) from A where FType=6 and FID=t1.FID) from #Test t1
barljy 2008-04-03
  • 打赏
  • 举报
回复
-- 第2句 语法错误:
/* 第2句 */ update #Test t1 set t1.FAmount=(select sum(FAmount) from A where FType=6 and FID=t1.FID) -- from #Test t1

10楼的朋友,你写的这个语句也有语法错误
我刚才测试过了
barljy 2008-04-03
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 pt1314917 的回复:]
SQL code
--恩。估计跟二楼说的一样
select distinct t1.FID,t1.FCustomer,t2.FExplanation
from A t1 left join B t2 on t1.FContactID=t2.FID
where t1.FType=4

看A表中是否有FID列。。。
[/Quote]
A表中有FID列的,不然插入语句就会报错了
提示错误在UPDATE语句中的FID
dl110 2008-04-03
  • 打赏
  • 举报
回复
以楼主提供的SQL语句看应该有以下4句:

/* 第1句 */ Create Table #Test(FID int, FNumber varchar(200),FBillNo varchar(200),FAmount decimal(18,4))
insert into #Test(FID,FNumber,FBillNo)
select distinct t1.FID,t1.FCustomer,t2.FExplanation
from A t1 left join B t2 on t1.FContactID=t2.FID
where t1.FType=4
/* 第2句 */ update t1 set t1.FAmount=(select sum(FAmount) from A where FType=6 and FID=t1.FID) from #Test t1
/* 第3句 */ select * from #Test
/* 第4句 */ drop table #Test

-- 第2句 语法错误:
/* 第2句 */ update #Test t1 set t1.FAmount=(select sum(FAmount) from A where FType=6 and FID=t1.FID) -- from #Test t1
barljy 2008-04-03
  • 打赏
  • 举报
回复
提示UPDATE语句有错误
PS:3楼的什么意思?写的代码和我的没什么区别啊
pt1314917 2008-04-03
  • 打赏
  • 举报
回复

--恩。估计跟二楼说的一样
select distinct t1.FID,t1.FCustomer,t2.FExplanation
from A t1 left join B t2 on t1.FContactID=t2.FID
where t1.FType=4

看A表中是否有FID列。。。
barljy 2008-04-03
  • 打赏
  • 举报
回复
不是的
要取的就是表T1中的FID字段
而表t1和t2是通过t1.FContactID=t2.FID关联的
提示有错的是UPDATE语句中的t1.FID啊
tianhuo_soft 2008-04-03
  • 打赏
  • 举报
回复
Create Table #Test(FID int, FNumber varchar(200),FBillNo varchar(200),FAmount decimal(18,4))
insert into #Test(FID,FNumber,FBillNo)

这条能用? 这就有问题 FID应该插入 数字 ,FNumber 插入字符串
pt1314917 2008-04-03
  • 打赏
  • 举报
回复

--语法上似乎没什么错误。


-狙击手- 2008-04-03
  • 打赏
  • 举报
回复
是了,

insert into #Test(FID,FNumber,FBillNo)
select distinct t1.FID,t1.FCustomer,t2.FExplanation
from A t1 left join B t2 on t1.FContactID=t2.FID
where t1.FType=4

两个不 一样
-狙击手- 2008-04-03
  • 打赏
  • 举报
回复
create table a(ftype int,fid int,famount int)
go
Create Table #Test(FID int, FNumber varchar(200),FBillNo varchar(200),FAmount decimal(18,4))
go
insert into #Test(FID,FNumber,FBillNo)
select distinct t1.FID,t1.FCustomer,t2.FExplanation
from A t1 left join B t2 on t1.FContactID=t2.FID
where t1.FType=4


update t1
set t1.FAmount=(select sum(FAmount) from A where FType=6 and FID=t1.FID )
from #Test t1
select * from #Test
drop table #Test
中国风 2008-04-03
  • 打赏
  • 举报
回复
t1.FID改为t1.FContactID
加载更多回复(1)

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧