请问 SQL 查询高手, 请问下面查询为什么老是报关键字FROM附近有语法错误

SELECT 客户名称,月份,sum(金额) AS 金额
from
(select KHJC AS 客户名称,(case when day(SHDRQ)>28 then left(year(SHDRQ),4)+'-'+right(100+month(SHDRQ+1),2) else left(year(SHDRQ),4)+'-'+right(100+month(SHDRQ),2) end) as 月份,SJYSKZJJE AS 金额,
from XSDDBTXXB)
where 客户名称='金陵'
group by 客户名称,月份
最新回答
我自有舟渡

2024-11-03 07:31:30

SELECT 客户名称,
月份,
sum(金额) AS 金额
from
(select KHJC AS 客户名称,(case when day(SHDRQ)>28 then left(year(SHDRQ),4)+'-'+right(100+month(SHDRQ+1),2) else left(year(SHDRQ),4)+'-'+right(100+month(SHDRQ),2) end) as 月份,SJYSKZJJE AS 金额,
from XSDDBTXXB)
where 客户名称='金陵'
group by 客户名称,月份

"SJYSKZJJE AS 金额,",你这里多了一个逗号,把逗号去掉就可以了,select子句最后一个字段不加逗号的,如果你这里没有写错的话.
再试试看.
初时模样

2024-11-03 11:44:28

参考这样修改就OK了
select a.*
from (select '名称1' as '名称','20100131' as 'dt' union ALL select '名称2' as '名称','20100331' as 'dt') a
where a.名称='名称1'

(select KHJC AS 客户名称,(case when day(SHDRQ)>28 then left(year(SHDRQ),4)+'-'+right(100+month(SHDRQ+1),2) else left(year(SHDRQ),4)+'-'+right(100+month(SHDRQ),2) end) as 月份,SJYSKZJJE AS 金额,
from XSDDBTXXB) a
where a.客户名称='金陵'
group by a.客户名称,a.月份