SQL查询有错,新手请各位指教。
本帖最后由 u011932802 于 2013-09-03 09:12:38 编辑
我写了一段SQL查询,但是预期的查询效果没有实现。新手菜鸟求教。该怎么写?
SELECT TOP (100) PERCENT NULL AS 林业局, SUBSTRING(单位代码, 5, 2) AS 林场, SUBSTRING(单位代码, 5, 2) AS 单位, (LEN(相关林班号) - LEN(REPLACE(相关林班号, ',',
''))) / LEN(',') + 1 AS 林班数, SUM(管护面积) AS 规划管护面积, SUM(管护面积) AS 实际管护面积, SUM(CASE WHEN 管护方式 = 1 THEN 1 ELSE 0 END)
+ SUM(CASE WHEN 管护方式 = 2 THEN 1 ELSE 0 END) AS 专业管护人数, SUM(CASE WHEN [管护方式] = 1 THEN [管护面积] ELSE 0 END)
+ SUM(CASE WHEN [管护方式] = 2 THEN [管护面积] ELSE 0 END) AS 专业管护面积, SUM(CASE WHEN 管护方式 = 1 THEN 1 ELSE 0 END) AS 设站管护人数,
SUM(CASE WHEN [管护方式] = 1 THEN [管护面积] ELSE 0 END) AS 设站管护面积, SUM(CASE WHEN 管护方式 = 2 THEN 1 ELSE 0 END) AS 责任区人数,
SUM(CASE WHEN [管护方式] = 2 THEN [管护面积] ELSE 0 END) AS 责任区面积, SUM(CASE WHEN 管护方式 = 3 THEN 1 ELSE 0 END) AS 承包管护人数,
SUM(CASE WHEN [管护方式] = 3 THEN [管护面积] ELSE 0 END) AS 承包管护面积, SUM(CASE WHEN 管护方式 = 1 THEN LEN(相关林班号)
- LEN(REPLACE(相关林班号, ',', '')) + 1 ELSE 0 END) AS 设站管护林班数, SUM(CASE WHEN 管护方式 = 2 THEN LEN(相关林班号) - LEN(REPLACE(相关林班号, ',', ''))
+ 1 ELSE 0 END) AS 责任区林班数, SUM(CASE WHEN 管护方式 = 3 THEN LEN(相关林班号) - LEN(REPLACE(相关林班号, ',', '')) + 1 ELSE 0 END) AS 承包管护林班数,