日期:2014-05-17  浏览次数:20504 次

帮忙指出一下错误,谢谢。
COPTD表-订单
主键:(TD001 单别 TD002单号 TD003序号 )TD008数量 TD009已交数量 TD013预交日期 
COPTH表--销售单身
主键:(TH001 单别 TH002单号) TH004 品号 TH014 订单单别 TH015 订单单号 TH016订单序号
COPTG--销售单头
主键:(TG001 单别 TG002单号) TG003 日期
INVMB
主键:(MB001品号),MD068仓库号
CMSMD
主键:(MD001仓库号),MD002仓库名称

结果要按 TG003时间查询显示

仓库名 到期未交比 到期已交比 过期已交比 过期未交比


DECLARE @YF CHAR(6)
SELECT @YF='201201'

;WITH 
CTE1 AS (SELECT CMSMD.MD001 as cghb, CMSMD.MD002 as cgry,

(SELECT COUNT(*) AS Expr1 FROM COPTH 
LEFT JOIN COPTD ON COPTH.TH015 = COPTD.TD002 AND COPTD.TD001 = COPTH.TH014
LEFT JOIN COPTG ON COPTG.TG002 = COPTH.TH002 AND COPTG.TG001 = COPTH.TH001 AND COPTD.TD013 >= COPTG.TG003 
WHERE COPTD.TD009 >= COPTD.TD008 AND COPTH.TH016 = COPTD.TD003 AND COPTH.TH004 = INVMB.MB001 
AND left(TG003,6)=@YF )AS DQYJ,

(SELECT COUNT(*) AS Expr1 FROM COPTH 
LEFT JOIN COPTD ON COPTH.TH015 = COPTD.TD002 AND COPTD.TD001 = COPTH.TH014 
LEFT JOIN COPTG ON COPTG.TG002 = COPTH.TH002 AND COPTG.TG001 = COPTH.TH001 AND COPTD.TD013 < COPTG.TG003
WHERE COPTD.TD009 >= COPTD.TD008 AND COPTH.TH016 = COPTD.TD003 AND COPTH.TH004 = INVMB.MB001 AND COPTD.TD013 >= COPTG.TG003  
AND left(TG003,6)=@YF)AS GQYJ,

(SELECT COUNT(*) AS Expr1 FROM COPTH 
LEFT JOIN COPTD ON COPTH.TH015 = COPTD.TD002 AND COPTD.TD001 = COPTH.TH014 
LEFT JOIN COPTG ON COPTG.TG002 = COPTH.TH002 AND COPTG.TG001 = COPTH.TH001 
WHERE COPTD.TD009 < COPTD.TD008 AND COPTH.TH016 = COPTD.TD003 AND COPTH.TH004 = INVMB.MB001 AND COPTD.TD013 >= COPTG.TG003 
AND left(TG003,6)=@YF) AS DQWJ,

(SELECT COUNT(*) AS Expr1 FROM COPTH 
LEFT JOIN COPTD ON COPTH.TH015 = COPTD.TD002 AND COPTD.TD001 = COPTH.TH014 
LEFT JOIN COPTG ON COPTG.TG002 = COPTH.TH002 AND COPTG.TG001 = COPTH.TH001 
WHERE COPTD.TD009 < COPTD.TD008 AND COPTH.TH016 = COPTD.TD003 AND COPTH.TH004 = INVMB.MB001 AND COPTD.TD013 < COPTG.TG003 
AND left(TG003,6)=@YF)AS GQWJ

FROM CMSMD 
LEFT OUTER JOIN INVMB ON INVMB.MB068 = CMSMD.MD001)


SELECT cghb, cgry, CONVERT(numeric(10, 2), CONVERT(numeric(10, 2), SUM(DQWJ) * 100) / (SUM(DQYJ+GQYJ+DQWJ+GQWJ))) 
  AS 到期未交比, CONVERT(numeric(10, 2), CONVERT(numeric(10, 2), SUM(DQYJ) * 100) / (SUM(DQYJ+GQYJ+DQWJ+GQWJ))) 
  AS 到期已交比, CONVERT(numeric(10, 2), CONVERT(numeric(10, 2), SUM(GQYJ) * 100) / (SUM(DQYJ+GQYJ+DQWJ+GQWJ))) 
  AS 过期已交比, CONVERT(numeric(10, 2), CONVERT(numeric(10, 2), SUM(GQWJ) * 100) / (SUM(DQYJ+GQYJ+DQWJ+GQWJ))) 
  AS 过期未交比
FROM (SELECT * FROM CTE1)
GROUP BY MD001, MD002
HAVING (SUM(DQYJ+GQYJ+DQWJ+GQWJ) <> 0)




消息 156,级别 15,状态 1,第 41 行
关键字 'GROUP' 附近有语法错误。


------解决方案--------------------
SQL code

FROM (SELECT * FROM CTE1) t--加个表名就可以了