标题:分解这条sql语句
只看楼主
linyesu
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2007-9-17
 问题点数:0 回复次数:1 
分解这条sql语句
对这条语句不明的是"结果"两字,
select * from
( select cp_hwxx_系统编号,cp_hwxx_编号,cp_hwxx_品名,cp_hwxx_规格,cp_hwxx_单位, sum(期初数量 + 上期外发加工数量 - 上期耗用数量 + 上期其他增加数量 - 上期其他减少数量 ) as 期初数量, sum(期初数量 + 上期外发加工数量 - 上期耗用数量 + 上期其他增加数量 - 上期其他减少数量 + 本期外发加工数量 - 本期耗用数量 + 本期其他增加数量 - 本期其他减少数量) as 期未数量, 0 as 期初金额, 0 as 期未金额, sum(当前库存量) as 当前库存量, sum(本期外发加工数量) as 本期外发加工数量, 0 as 本期外发加工金额, sum(本期耗用数量) as 本期耗用数量, 0 as 本期耗用金额, sum(本期其他增加数量) as 本期其他增加数量, 0 as 本期其他增加金额, sum(本期其他减少数量) as 本期其他减少数量, 0 as 本期其他减少金额 from
(
SELECT '库存' as 类型, sum(cp_wfb_期初数量) as 期初数量, sum(cp_wfb_数量) as 当前库存量, sum(cp_wfb_期初数量 * cp_hwxx_成本价 ) as 期初金额, sum(cp_wfb_数量 * cp_hwxx_成本价 ) as 当前库存金额, 0 AS 上期外发加工数量, 0 AS 上期外发加工金额, 0 AS 上期耗用数量, 0 AS 上期耗用金额, 0 AS 上期其他增加数量, 0 AS 上期其他增加金额, 0 AS 上期其他减少数量, 0 AS 上期其他减少金额, 0 AS 本期外发加工数量, 0 AS 本期外发加工金额, 0 AS 本期耗用数量, 0 AS 本期耗用金额, 0 AS 本期其他增加数量, 0 AS 本期其他增加金额, 0 AS 本期其他减少数量, 0 AS 本期其他减少金额, cp_hwxx_系统编号,cp_hwxx_编号,cp_hwxx_品名,cp_hwxx_规格,cp_hwxx_单位 FROM cp_外发表 LEFT OUTER JOIN cp_货物信息 ON cp_外发表.cp_wfb_货物系编 = cp_hwxx_系统编号 Where ( cp_hwxx_编号 like '%' or cp_hwxx_品名 like '%%' or cp_hwxx_品名缩写 like '%%' or cp_hwxx_规格缩写 like '%%' or cp_hwxx_规格 like '%%') and (cp_wfb_加工厂系编 = 48) GROUP BY cp_hwxx_系统编号,cp_hwxx_编号 , cp_hwxx_品名, cp_hwxx_规格, cp_hwxx_单位 UNION                                                                                                                       SELECT '上期外发加工' as 类型, 0 AS 期初数量, 0 AS 期初金额, 0 AS 当前库存量, 0 AS 当前库存金额,SUM(cp_wfjgb_数量) AS 上期外发加工数量,SUM(cp_wfjgb_数量 * cp_hwxx_成本价) AS 上期外发加工金额, 0 AS 上期耗用数量, 0 AS 上期耗用金额, 0 AS 上期其他增加数量, 0 AS 上期其他增加金额, 0 AS 上期其他减少数量, 0 AS 上期其他减少金额, 0 AS 本期外发加工数量, 0 AS 本期外发加工金额, 0 AS 本期耗用数量, 0 AS 本期耗用金额, 0 AS 本期其他增加数量, 0 AS 本期其他增加金额, 0 AS 本期其他减少数量, 0 AS 本期其他减少金额, cp_hwxx_系统编号,cp_hwxx_编号,cp_hwxx_品名,cp_hwxx_规格,cp_hwxx_单位 FROM cp_外发加工表 LEFT OUTER JOIN cp_货物信息 ON cp_wfjgb_货物系编 = cp_hwxx_系统编号 LEFT OUTER JOIN cp_外发加工单 ON cp_wfjgb_货单系编 = cp_外发加工单.cp_wfjgd_系统编号 Where ( cp_hwxx_编号 like '%' or cp_hwxx_品名 like '%%' or cp_hwxx_品名缩写 like '%%' or cp_hwxx_规格缩写 like '%%' or cp_hwxx_规格 like '%%') and cp_wfjgd_建单时间 < '2007-12-01' and (cp_wfjgd_加工厂系编 = 48) GROUP BY cp_hwxx_系统编号,cp_hwxx_编号 , cp_hwxx_品名, cp_hwxx_规格, cp_hwxx_单位 UNION                                                                                                                          SELECT '上期耗用' as 类型, 0 AS 期初数量, 0 AS 期初金额, 0 AS 当前库存量, 0 AS 当前库存金额, 0 AS 上期外发加工数量, 0 AS 上期外发加工金额,SUM(cp_wfhshyb_数量) AS 上期耗用数量,SUM(cp_wfhshyb_数量 * cp_hwxx_成本价) AS 上期耗用金额, 0 AS 上期其他增加数量, 0 AS 上期其他增加金额, 0 AS 上期其他减少数量, 0 AS 上期其他减少金额, 0 AS 本期外发加工数量, 0 AS 本期外发加工金额, 0 AS 本期耗用数量, 0 AS 本期耗用金额, 0 AS 本期其他增加数量, 0 AS 本期其他增加金额, 0 AS 本期其他减少数量, 0 AS 本期其他减少金额, cp_hwxx_系统编号,cp_hwxx_编号,cp_hwxx_品名,cp_hwxx_规格,cp_hwxx_单位 FROM cp_外发回收耗用表 LEFT OUTER JOIN cp_货物信息 ON cp_wfhshyb_货物系编 = cp_hwxx_系统编号 LEFT OUTER JOIN cp_外发回收单 ON cp_wfhshyb_货单系编 = cp_外发回收单.cp_wfhsd_系统编号 Where ( cp_hwxx_编号 like '%' or cp_hwxx_品名 like '%%' or cp_hwxx_品名缩写 like '%%' or cp_hwxx_规格缩写 like '%%' or cp_hwxx_规格 like '%%') and cp_wfhsd_建单时间 < '2007-12-01' and (cp_wfhsd_加工厂系编 = 48) GROUP BY cp_hwxx_系统编号,cp_hwxx_编号 , cp_hwxx_品名, cp_hwxx_规格, cp_hwxx_单位 UNION                                                                                                                          SELECT '上期其他增加' as 类型, 0 AS 期初数量, 0 AS 期初金额, 0 AS 当前库存量, 0 AS 当前库存金额, 0 AS 上期外发加工数量, 0 AS 上期外发加工金额, 0 AS 上期耗用数量, 0 AS 上期耗用金额, SUM(cp_wfjgzjb_数量) AS 上期其他增加数量, SUM(cp_wfjgzjb_数量 * cp_hwxx_成本价) AS 上期其他增加金额, 0 AS 上期其他减少数量, 0 AS 上期其他减少金额, 0 AS 本期外发加工数量, 0 AS 本期外发加工金额, 0 AS 本期耗用数量, 0 AS 本期耗用金额, 0 AS 本期其他增加数量, 0 AS 本期其他增加金额, 0 AS 本期其他减少数量, 0 AS 本期其他减少金额, cp_hwxx_系统编号,cp_hwxx_编号,cp_hwxx_品名,cp_hwxx_规格,cp_hwxx_单位 FROM cp_外发加工增加表 LEFT OUTER JOIN cp_货物信息 ON cp_外发加工增加表.cp_wfjgzjb_货物系编 = cp_hwxx_系统编号 LEFT OUTER JOIN cp_外发加工增加单 ON cp_外发加工增加表.cp_wfjgzjb_货单系编 = cp_外发加工增加单.cp_wfjgzjd_系统编号 Where ( cp_hwxx_编号 like '%' or cp_hwxx_品名 like '%%' or cp_hwxx_品名缩写 like '%%' or cp_hwxx_规格缩写 like '%%' or cp_hwxx_规格 like '%%') and cp_wfjgzjd_建单时间 < '2007-12-01' and (cp_wfjgzjd_加工厂系编 = 48) GROUP BY cp_hwxx_系统编号,cp_hwxx_编号 , cp_hwxx_品名, cp_hwxx_规格, cp_hwxx_单位 UNION                                                                                                                          SELECT '上期其他减少' as 类型, 0 AS 期初数量, 0 AS 期初金额, 0 AS 当前库存量, 0 AS 当前库存金额, 0 AS 上期外发加工数量, 0 AS 上期外发加工金额, 0 AS 上期耗用数量, 0 AS 上期耗用金额, 0 AS 上期其他增加数量, 0 AS 上期其他增加金额, SUM(cp_wfjgjsb_数量) AS 上期其他减少数量, SUM(cp_wfjgjsb_数量 * cp_hwxx_成本价) AS 上期其他减少金额, 0 AS 本期外发加工数量, 0 AS 本期外发加工金额, 0 AS 本期耗用数量, 0 AS 本期耗用金额, 0 AS 本期其他增加数量, 0 AS 本期其他增加金额, 0 AS 本期其他减少数量, 0 AS 本期其他减少金额, cp_hwxx_系统编号,cp_hwxx_编号,cp_hwxx_品名,cp_hwxx_规格,cp_hwxx_单位 FROM cp_外发加工减少表 LEFT OUTER JOIN cp_货物信息 ON cp_外发加工减少表.cp_wfjgjsb_货物系编 = cp_hwxx_系统编号 LEFT OUTER JOIN cp_外发加工减少单 ON cp_外发加工减少表.cp_wfjgjsb_货单系编 = cp_外发加工减少单.cp_wfjgjsd_系统编号 Where ( cp_hwxx_编号 like '%' or cp_hwxx_品名 like '%%' or cp_hwxx_品名缩写 like '%%' or cp_hwxx_规格缩写 like '%%' or cp_hwxx_规格 like '%%') and cp_wfjgjsd_建单时间 < '2007-12-01' and (cp_wfjgjsd_加工厂系编 = 48) GROUP BY cp_hwxx_系统编号,cp_hwxx_编号 , cp_hwxx_品名, cp_hwxx_规格, cp_hwxx_单位 UNION                                                                                                                          SELECT '本期外发加工' as 类型, 0 AS 期初数量, 0 AS 期初金额, 0 AS 当前库存量, 0 AS 当前库存金额, 0 AS 上期外发加工数量, 0 AS 上期外发加工金额, 0 AS 上期耗用数量, 0 AS 上期耗用金额, 0 AS 上期其他增加数量, 0 AS 上期其他增加金额, 0 AS 上期其他减少数量, 0 AS 上期其他减少金额,SUM(cp_wfjgb_数量) AS 本期外发加工数量,SUM(cp_wfjgb_数量 * cp_hwxx_成本价) AS 本期外发加工金额, 0 AS 本期耗用数量, 0 AS 本期耗用金额, 0 AS 本期其他增加数量, 0 AS 本期其他增加金额, 0 AS 本期其他减少数量, 0 AS 本期其他减少金额, cp_hwxx_系统编号,cp_hwxx_编号,cp_hwxx_品名,cp_hwxx_规格,cp_hwxx_单位 FROM cp_外发加工表 LEFT OUTER JOIN cp_货物信息 ON cp_wfjgb_货物系编 = cp_hwxx_系统编号 LEFT OUTER JOIN cp_外发加工单 ON cp_wfjgb_货单系编 = cp_外发加工单.cp_wfjgd_系统编号 Where ( cp_hwxx_编号 like '%' or cp_hwxx_品名 like '%%' or cp_hwxx_品名缩写 like '%%' or cp_hwxx_规格缩写 like '%%' or cp_hwxx_规格 like '%%') and cp_wfjgd_建单时间 >= '2007-12-01' and cp_wfjgd_建单时间 <= '2007-12-30' and (cp_wfjgd_加工厂系编 = 48) GROUP BY cp_hwxx_系统编号,cp_hwxx_编号 , cp_hwxx_品名, cp_hwxx_规格, cp_hwxx_单位 UNION                                                                                                                          SELECT '本期耗用' as 类型, 0 AS 期初数量, 0 AS 期初金额, 0 AS 当前库存量, 0 AS 当前库存金额, 0 AS 上期外发加工数量, 0 AS 上期外发加工金额, 0 AS 上期耗用数量, 0 AS 上期耗用金额, 0 AS 上期其他增加数量, 0 AS 上期其他增加金额, 0 AS 上期其他减少数量, 0 AS 上期其他减少金额, 0 AS 本期外发加工数量, 0 AS 本期外发加工金额,SUM(cp_wfhshyb_数量) AS 本期耗用数量,SUM(cp_wfhshyb_数量 * cp_hwxx_成本价) AS 本期耗用金额, 0 AS 本期其他增加数量, 0 AS 本期其他增加金额, 0 AS 本期其他减少数量, 0 AS 本期其他减少金额, cp_hwxx_系统编号,cp_hwxx_编号,cp_hwxx_品名,cp_hwxx_规格,cp_hwxx_单位 FROM cp_外发回收耗用表 LEFT OUTER JOIN cp_货物信息 ON cp_wfhshyb_货物系编 = cp_hwxx_系统编号 LEFT OUTER JOIN cp_外发回收单 ON cp_wfhshyb_货单系编 = cp_外发回收单.cp_wfhsd_系统编号 Where ( cp_hwxx_编号 like '%' or cp_hwxx_品名 like '%%' or cp_hwxx_品名缩写 like '%%' or cp_hwxx_规格缩写 like '%%' or cp_hwxx_规格 like '%%') and cp_wfhsd_建单时间 >= '2007-12-01' and cp_wfhsd_建单时间 <= '2007-12-30' and (cp_wfhsd_加工厂系编 = 48) GROUP BY cp_hwxx_系统编号,cp_hwxx_编号 , cp_hwxx_品名, cp_hwxx_规格, cp_hwxx_单位 UNION                                                                                                                          SELECT '本期其他增加' as 类型, 0 AS 期初数量, 0 AS 期初金额, 0 AS 当前库存量, 0 AS 当前库存金额, 0 AS 上期外发加工数量, 0 AS 上期外发加工金额, 0 AS 上期耗用数量, 0 AS 上期耗用金额, 0 AS 上期其他增加数量, 0 AS 上期其他增加金额, 0 AS 上期其他减少数量, 0 AS 上期其他减少金额, 0 AS 本期外发加工数量, 0 AS 本期外发加工金额, 0 本期耗用数量, 0 AS 本期耗用金额, SUM(cp_wfjgzjb_数量) AS 本期其他增加数量, SUM(cp_wfjgzjb_数量 * cp_hwxx_成本价) AS 本期其他增加金额, 0 AS 本期其他减少数量, 0 AS 本期其他减少金额, cp_hwxx_系统编号,cp_hwxx_编号,cp_hwxx_品名,cp_hwxx_规格,cp_hwxx_单位 FROM cp_外发加工增加表 LEFT OUTER JOIN cp_货物信息 ON cp_外发加工增加表.cp_wfjgzjb_货物系编 = cp_hwxx_系统编号 LEFT OUTER JOIN cp_外发加工增加单 ON cp_外发加工增加表.cp_wfjgzjb_货单系编 = cp_外发加工增加单.cp_wfjgzjd_系统编号 Where ( cp_hwxx_编号 like '%' or cp_hwxx_品名 like '%%' or cp_hwxx_品名缩写 like '%%' or cp_hwxx_规格缩写 like '%%' or cp_hwxx_规格 like '%%') and cp_wfjgzjd_建单时间 >= '2007-12-01' and cp_wfjgzjd_建单时间 <= '2007-12-30' and (cp_wfjgzjd_加工厂系编 = 48) GROUP BY cp_hwxx_系统编号,cp_hwxx_编号 , cp_hwxx_品名, cp_hwxx_规格, cp_hwxx_单位 UNION                                                                                                                          SELECT '本期其他减少' as 类型, 0 AS 期初数量, 0 AS 期初金额, 0 AS 当前库存量, 0 AS 当前库存金额, 0 AS 上期外发加工数量, 0 AS 上期外发加工金额, 0 AS 上期耗用数量, 0 AS 上期耗用金额, 0 AS 上期其他增加数量, 0 AS 上期其他增加金额, 0 AS 上期其他减少数量, 0 AS 上期其他减少金额, 0 AS 本期外发加工数量, 0 AS 本期外发加工金额, 0 本期耗用数量, 0 AS 本期耗用金额, 0 AS 本期其他增加数量, 0 AS 本期其他增加金额, SUM(cp_wfjgjsb_数量) AS 本期其他减少数量, SUM(cp_wfjgjsb_数量 * cp_hwxx_成本价) AS 本期其他减少金额, cp_hwxx_系统编号,cp_hwxx_编号,cp_hwxx_品名,cp_hwxx_规格,cp_hwxx_单位 FROM cp_外发加工减少表 LEFT OUTER JOIN cp_货物信息 ON cp_外发加工减少表.cp_wfjgjsb_货物系编 = cp_hwxx_系统编号 LEFT OUTER JOIN cp_外发加工减少单 ON cp_外发加工减少表.cp_wfjgjsb_货单系编 = cp_外发加工减少单.cp_wfjgjsd_系统编号 Where ( cp_hwxx_编号 like '%' or cp_hwxx_品名 like '%%' or cp_hwxx_品名缩写 like '%%' or cp_hwxx_规格缩写 like '%%' or cp_hwxx_规格 like '%%') and cp_wfjgjsd_建单时间 >= '2007-12-01' and cp_wfjgjsd_建单时间 <= '2007-12-30' and (cp_wfjgjsd_加工厂系编 = 48) GROUP BY cp_hwxx_系统编号,cp_hwxx_编号 , cp_hwxx_品名, cp_hwxx_规格, cp_hwxx_单位)


结果 GROUP BY cp_hwxx_系统编号,cp_hwxx_编号,cp_hwxx_品名,cp_hwxx_规格,cp_hwxx_单位 )结果1
搜索更多相关主题的帖子: 数量 语句 sql 分解 sum 
2008-01-04 17:37
linyesu
Rank: 1
等 级:新手上路
帖 子:77
专家分:0
注 册:2007-9-17
得分:0 
还有结果1
2008-01-04 17:38



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-195886-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.262365 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved