ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 4031|回复: 16

[讨论] 请帮忙简化以下SQL语句,谢谢!

[复制链接]

TA的精华主题

TA的得分主题

发表于 2010-4-17 21:41 | 显示全部楼层 |阅读模式
SELECT * FROM (
SELECT "EO" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[EO$] UNION ALL
SELECT "IF" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[IF$] UNION ALL
SELECT "KG" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[HK_KG$] UNION ALL
SELECT "KG" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[USA_KG$] UNION ALL
SELECT "JP" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[JP$] UNION ALL
SELECT "SL" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[SL$] UNION ALL
SELECT "TM" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[TM$] UNION ALL
SELECT "WW" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[WW$] UNION ALL
SELECT "TY" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[TY$] UNION ALL
SELECT "WO" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[WO$] UNION ALL
SELECT "KA" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[KA$] UNION ALL
SELECT "TI" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[TI$] UNION ALL
SELECT "NK" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[NK$] UNION ALL
SELECT "MT" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[MT$] UNION ALL
SELECT "WD" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[WD$]
)
WHERE WEEKLY LIKE 'WK__'


上面提到的字段是每个表中都存在的,你们看,所有的SHEET都在同一个工作簿中,能否先给文件名及路径赋值,然后再引用其赋值?
原先我是先每一句SELECT FROM语句后都有加一个WHERE WEEKLY LIKE ‘WK__',后来想来想去才发现可以用SELECT * FROM 来引用整个句子,
再给他一个条件,这样语句就省了很多,因为在OE LDB语句不能超过2024个字符?连空格?这是我反复试验的.

另外一个问题就是,上面表中只是引用了15个工作表,不能再多一个了,如果达到16个的话,就会提示超出系统资源。如何达到60个以上的引用?

谢谢!

[ 本帖最后由 fenghcl 于 2010-4-17 22:20 编辑 ]

TA的精华主题

TA的得分主题

发表于 2010-4-17 23:00 | 显示全部楼层
试着简化一下,但没有条件验证
  1. SELECT * FROM (
  2. SELECT 'EO' AS 客户,ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[EO$] UNION ALL
  3. SELECT 'IF',ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[IF$] UNION ALL
  4. SELECT 'KG',ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[HK_KG$] UNION ALL
  5. SELECT 'KG',ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[USA_KG$] UNION ALL
  6. SELECT 'JP',ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[JP$] UNION ALL
  7. SELECT 'SL',ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[SL$] UNION ALL
  8. SELECT 'TM',ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[TM$] UNION ALL
  9. SELECT 'WW',ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[WW$] UNION ALL
  10. SELECT 'TY',ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[TY$] UNION ALL
  11. SELECT 'WO',ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[WO$] UNION ALL
  12. SELECT 'KA',ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[KA$] UNION ALL
  13. SELECT 'TI',ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[TI$] UNION ALL
  14. SELECT 'NK',ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[NK$] UNION ALL
  15. SELECT 'MT',ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[MT$] UNION ALL
  16. SELECT 'WD',ITEM,PRICE,QTY,WEEKLY FROM `I:\Weekly Loading Report-WK02`.[WD$])
  17. WHERE WEEKLY LIKE 'WK__'
复制代码
如果各数据源表中只有ITEM,PRICE,QTY,WEEKLY这四列,则为
  1. SELECT * FROM (
  2. SELECT 'EO' AS 客户,* FROM `I:\Weekly Loading Report-WK02`.[EO$] UNION ALL
  3. SELECT 'IF',* FROM `I:\Weekly Loading Report-WK02`.[IF$] UNION ALL
  4. SELECT 'KG',* FROM `I:\Weekly Loading Report-WK02`.[HK_KG$] UNION ALL
  5. SELECT 'KG',* FROM `I:\Weekly Loading Report-WK02`.[USA_KG$] UNION ALL
  6. SELECT 'JP',* FROM `I:\Weekly Loading Report-WK02`.[JP$] UNION ALL
  7. SELECT 'SL',* FROM `I:\Weekly Loading Report-WK02`.[SL$] UNION ALL
  8. SELECT 'TM',* FROM `I:\Weekly Loading Report-WK02`.[TM$] UNION ALL
  9. SELECT 'WW',* FROM `I:\Weekly Loading Report-WK02`.[WW$] UNION ALL
  10. SELECT 'TY',* FROM `I:\Weekly Loading Report-WK02`.[TY$] UNION ALL
  11. SELECT 'WO',* FROM `I:\Weekly Loading Report-WK02`.[WO$] UNION ALL
  12. SELECT 'KA',* FROM `I:\Weekly Loading Report-WK02`.[KA$] UNION ALL
  13. SELECT 'TI',* FROM `I:\Weekly Loading Report-WK02`.[TI$] UNION ALL
  14. SELECT 'NK',* FROM `I:\Weekly Loading Report-WK02`.[NK$] UNION ALL
  15. SELECT 'MT',* FROM `I:\Weekly Loading Report-WK02`.[MT$] UNION ALL
  16. SELECT 'WD',* FROM `I:\Weekly Loading Report-WK02`.[WD$])
  17. WHERE WEEKLY LIKE 'WK__'
复制代码
如果就在本工作簿中建立此查询
那么 `I:\Weekly Loading Report-WK02`. 全部替换为空

[ 本帖最后由 livewire 于 2010-4-18 10:35 编辑 ]

TA的精华主题

TA的得分主题

发表于 2010-4-17 23:15 | 显示全部楼层
关于如何达到60个以上的引用?   我看ACCESS 倒是挺适合!

TA的精华主题

TA的得分主题

 楼主| 发表于 2010-4-18 10:23 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
多谢2,3楼监察大人的回复。
是我没有讲清楚,这是在另一个文件建立的真正意义上的引用,源文件中有30个SHEET,每个SHEET都有着相同的结构,
现在只是抽取部分字段出来做报表,因为在厂里工作,部门一些老大是看要看每个客户的分表,这样他们看起来会比较直观,
而每周又要汇总一次这个星期的所有客户总出货是多少,就必须做一个透视分析。下一步还要根据这个分析做其他条件的分析。
如果OE LDB中的语句总长度不能超过2024,接下来的表是没办法再添加上去的,
首先文件名及路径是上面指定放在一个服务器上的某个目录下的某个目录的某个目录,长着呢。
对于笨的人,只能想些聪明的方法去应对的吧?

[ 本帖最后由 fenghcl 于 2010-4-18 10:32 编辑 ]

TA的精华主题

TA的得分主题

发表于 2010-4-18 10:38 | 显示全部楼层
明白了! 只能简化成这样子
  1. SELECT 客户,ITEM,PRICE,QTY,WEEKLY FROM (
  2. SELECT 'EO' AS 客户,* FROM `I:\Weekly Loading Report-WK02`.[EO$] UNION ALL
  3. SELECT 'IF',* FROM `I:\Weekly Loading Report-WK02`.[IF$] UNION ALL
  4. SELECT 'KG',* FROM `I:\Weekly Loading Report-WK02`.[HK_KG$] UNION ALL
  5. SELECT 'KG',* FROM `I:\Weekly Loading Report-WK02`.[USA_KG$] UNION ALL
  6. SELECT 'JP',* FROM `I:\Weekly Loading Report-WK02`.[JP$] UNION ALL
  7. SELECT 'SL',* FROM `I:\Weekly Loading Report-WK02`.[SL$] UNION ALL
  8. SELECT 'TM',* FROM `I:\Weekly Loading Report-WK02`.[TM$] UNION ALL
  9. SELECT 'WW',* FROM `I:\Weekly Loading Report-WK02`.[WW$] UNION ALL
  10. SELECT 'TY',* FROM `I:\Weekly Loading Report-WK02`.[TY$] UNION ALL
  11. SELECT 'WO',* FROM `I:\Weekly Loading Report-WK02`.[WO$] UNION ALL
  12. SELECT 'KA',* FROM `I:\Weekly Loading Report-WK02`.[KA$] UNION ALL
  13. SELECT 'TI',* FROM `I:\Weekly Loading Report-WK02`.[TI$] UNION ALL
  14. SELECT 'NK',* FROM `I:\Weekly Loading Report-WK02`.[NK$] UNION ALL
  15. SELECT 'MT',* FROM `I:\Weekly Loading Report-WK02`.[MT$] UNION ALL
  16. SELECT 'WD',* FROM `I:\Weekly Loading Report-WK02`.[WD$])
  17. WHERE WEEKLY LIKE 'WK__'
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2010-4-18 12:18 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册

回复 5楼 livewire 的帖子

去除小小的纰漏后(第三行开始,每个新增字段后要加一个“AS 客户“),发现提示“在联合查询中所选定的两个数据表或查询的列数不匹配”,
但很奇怪,我的每个SHEET都是统一由第一个SHEET中复制过去再删除除表头之外的其他内容,怎么会提示列数不匹配呢,而且,我已经再一次用CTRL+END来看表的结尾,都是到我最后一行,最后一行数据处,应该说不用有这种现象。
提示列数不匹配:
SELECT 客户,ITEM,PRICE,QTY,WEEKLY FROM (
SELECT 'EO' AS 客户, * from [EO$] UNION ALL
SELECT 'IF' AS 客户, * from [IFi$] UNION ALL
SELECT 'KG' AS 客户, * from [HK_KG$] UNION ALL
SELECT 'KG' AS 客户, * from [USA_KG$] UNION ALL
SELECT 'JP' AS 客户, * from [JP$] UNION ALL
SELECT 'SL' AS 客户, * from [SL$] UNION ALL
SELECT 'TM' AS 客户, * from [TM$] UNION ALL
SELECT 'WW' AS 客户, * from [WW$] UNION ALL
SELECT 'TY' AS 客户, * from [TY$] UNION ALL
SELECT 'WO' AS 客户, * from [WO$] UNION ALL
SELECT 'KA' AS 客户, * from [KA$] UNION ALL
SELECT 'TI' AS 客户, * from [TI$] UNION ALL
SELECT 'NK' AS 客户, * from [NK$] UNION ALL
SELECT 'MT' AS 客户, * from [MT$] UNION ALL
SELECT 'WD' AS 客户, * from [WD$])
WHERE WEEKLY LIKE 'WK__'

另外,因为所有的数据源都在同一个工作簿中,而且已经在连接中指定了源,能否在后面的语句中不用再反映呢?试试看后,是行的,但新问题又来了,QTY求和时都是0,原来它将QTY数据当成了文本形式。也许是电脑的原因吧。

最后的问题还是没有解决,再多加一个SHEET上去的话,就不行了,提示超出系统资源:(已经删除最后一行的SHEET,可以用,但QTY求和变文本了,求出来是0。)
select * from (SELECT "EO" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM [EO$] UNION ALL
SELECT "IFI" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM [IFI$] UNION ALL
SELECT "KG" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM [HK_KG$] UNION ALL
SELECT "KG" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM [USA_KG$] UNION ALL
SELECT "JP" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM [JP$] UNION ALL
SELECT "SL" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM [SL$] UNION ALL
SELECT "TM" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM [TM$] UNION ALL
SELECT "WW" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM [WW$] UNION ALL
SELECT "TY" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM [TY$] UNION ALL
SELECT "WO" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM [WO$] UNION ALL
SELECT "KA" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM [KA$] UNION ALL
SELECT "TI" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM [TI$] UNION ALL
SELECT "NK" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM [NK$] UNION ALL
SELECT "MT" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM [MT$] UNION ALL
SELECT "WD" AS 客户,ITEM,PRICE,QTY,WEEKLY FROM [WD$]) WHERE WEEKLY LIKE 'WK__‘


[ 本帖最后由 fenghcl 于 2010-4-18 15:58 编辑 ]

连接源路径已经表达,应该可以省去。

连接源路径已经表达,应该可以省去。

TA的精华主题

TA的得分主题

发表于 2010-4-18 12:44 | 显示全部楼层
呵呵!俺只是提供一个思路,未经验证。
数据源文件只在楼主手上,钦佩楼主的钻研精神!

至于超出系统资源, 3楼已回复。 退而求其次,看看2007能否胜任?

另外, 透视表可以再透视,同样地,查询可以再查询。

TA的精华主题

TA的得分主题

发表于 2010-4-18 13:18 | 显示全部楼层

回复 6楼 fenghcl 的帖子

没有附件,只能从SQL中猜测原因:
一:用*时,发现查询列数不匹配的可能原因,说明连接的表的字段数不一致。
可能部分表存在假空的列或有多余的列。检查方法可以用记事本、OLE DO 直接生成表和MQ。假如存在F1等含Fx的字段名称,则数据源存在假空列。假如有其他名称,则存在多余的字段。这时候我们只需要将表精确定位即可。如:指明列。[IFI$A:H]
二:字段求和为0。这个与SQL查询有关。假如表的字段下第一条记录为NULL值,SQL往往容易将此字段定义为文本属性,自动将非文本处理成NULL值。解决方法:将一条记录输入0,或用四则运算符。如QTY字段可以用QTY+0代替。
PS:假如可以的话,楼主最好上传附件。名称可以更改,数值也可以更改,只要格式保持不变就可以分析出错原因。

TA的精华主题

TA的得分主题

 楼主| 发表于 2010-4-18 15:52 | 显示全部楼层

回复 7楼 livewire 的帖子

你意思是说,用ACCESS将EXCEL几十个SHEET导入,然后再在ACCESS中透视,最后再导出到EXCEL?
由于文件是另一个部门的同事的,我只是对新问题感兴趣,也想利用这种机会来了解并学习,要是这样的话,ACCESS在办公室里的人不会有几个懂的,我这个三脚猫功夫,别的同事都对我另眼相看了,可见他们的水平.
我取得了那同事的同意,现上传文件给大伙把把脉,看问题在哪?谢谢!

TA的精华主题

TA的得分主题

 楼主| 发表于 2010-4-18 15:55 | 显示全部楼层

回复 8楼 wuxiang_123 的帖子

你的建议很好.特别是第二点,关于求和数出现0的情况,我再研究研究.
附件我上传了,有时间的话,帮我分析分析.谢谢!

Weekly Loading Report-WK02.rar

52.39 KB, 下载次数: 14

第二SHEET以后的全部要在另一个文件中汇总透视.

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-28 20:50 , Processed in 0.055713 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表