access里面的sql语句转换sqlserver的sql语句问题 (急,急,急,)难,难,难,(10分)

  • 主题发起人 主题发起人 zyt_1978
  • 开始时间 开始时间
Z

zyt_1978

Unregistered / Unconfirmed
GUEST, unregistred user!
select M.*, <br> &nbsp; &nbsp; &nbsp; &nbsp;(select HH from CGDD where CGDD.ZBID=M.ZBID and CGDD.HH=M.HH) as TMP, <br> &nbsp; &nbsp; &nbsp; &nbsp;(select sum(SL) from CGSH where DDID=M.ZBID and HPID=M.HPID) as aSHSL, <br> &nbsp; &nbsp; &nbsp; &nbsp;case &nbsp;M.SL &gt; aSHSL when 1 then M.SL - aSHSL else 0 end as aMSSL, <br> &nbsp; &nbsp; &nbsp; &nbsp;--iif(M.SL &gt; aSHSL, M.SL - aSHSL, 0) as aMSSL, <br> &nbsp; &nbsp; &nbsp; &nbsp;H.BH as aBH, H.PM as aPM, H.DW as aDW <br> &nbsp; &nbsp; &nbsp; &nbsp;from CGDD M &nbsp;inner join HP H on M.HPID=H.ID <br> &nbsp;里面的iif已经让我屏蔽了<br>但是 <br> &nbsp; 第 4 行: '&gt;' 附近有语法错误。 <br> 后面还提示 &nbsp;列名 'aSHSL' 无效。
 
Access里好像不支持CASE
 
select M.*, <br> &nbsp; &nbsp; &nbsp; &nbsp;(select HH from CGDD where CGDD.ZBID=M.ZBID and CGDD.HH=M.HH) as TMP, <br> &nbsp; &nbsp; &nbsp; &nbsp;(select sum(SL) SL from CGSH where DDID=M.ZBID and HPID=M.HPID) as aSHSL, <br> &nbsp; &nbsp; &nbsp; &nbsp;case &nbsp;M.SL &gt; aSHSL.SL when 1 then M.SL - aSHSL.SL else 0 end as aMSSL, <br> &nbsp; &nbsp; &nbsp; &nbsp;--iif(M.SL &gt; aSHSL, M.SL - aSHSL, 0) as aMSSL, <br> &nbsp; &nbsp; &nbsp; &nbsp;H.BH as aBH, H.PM as aPM, H.DW as aDW <br> &nbsp; &nbsp; &nbsp; &nbsp;from CGDD M &nbsp;inner join HP H on M.HPID=H.ID <br><br>你的aSHSL相当于一个临时表(只是这么打比方,实际不是)了,做比较是要指出这个临时表的字段。。而不是把这个临时表做为字段。。
 
还是出错<br> 列前缀 'aSHSL' 与查询中所用的表名或别名不匹配。<br>aSHSL看来不算是临时表呀
 
对不起是我理解错了,你可以试试再套一层查询。<br>Select K.*,case &nbsp;K.SL &gt; aSHSL when 1 then K.SL - K.aSHSL else 0 end as K.aMSSL, <br>( select M.*, <br> &nbsp; &nbsp; &nbsp; &nbsp;(select HH from CGDD where CGDD.ZBID=M.ZBID and CGDD.HH=M.HH) as TMP, <br> &nbsp; &nbsp; &nbsp; &nbsp;(select sum(SL) from CGSH where DDID=M.ZBID and HPID=M.HPID) as aSHSL, <br> &nbsp; &nbsp; &nbsp; &nbsp;H.BH as aBH, H.PM as aPM, H.DW as aDW <br> &nbsp; &nbsp; &nbsp; &nbsp;from CGDD M &nbsp;inner join HP H on M.HPID=H.ID &nbsp;) K<br>如果还不行可以加我QQ120437873。
 
写错了。。<br>Select K.*,<br> &nbsp; case &nbsp;K.SL &gt; aSHSL when 1 then K.SL - K.aSHSL else 0 end as K.aMSSL from <br>( select M.*, <br> &nbsp; &nbsp; &nbsp; &nbsp;(select HH from CGDD where CGDD.ZBID=M.ZBID and CGDD.HH=M.HH) as TMP, <br> &nbsp; &nbsp; &nbsp; &nbsp;(select sum(SL) from CGSH where DDID=M.ZBID and HPID=M.HPID) as aSHSL, <br> &nbsp; &nbsp; &nbsp; &nbsp;H.BH as aBH, H.PM as aPM, H.DW as aDW <br> &nbsp; &nbsp; &nbsp; &nbsp;from CGDD M &nbsp;inner join HP H on M.HPID=H.ID &nbsp;) K
 
原来的那个语句是用到access中的,没有问题<br> &nbsp;换到sqlserver真是差别不小呀,改动量也很大。<br><br>select M.*, (select HH from CGDD where CGDD.ZBID=M.ZBID and CGDD.HH=M.HH) as TMP, <br>(select sum(SL) from CGSH where DDID=M.ZBID and HPID=M.HPID) as aSHSL, <br>--case when (M.SL &gt; aSHSL) &nbsp;then M.SL - aSHSL else 0 end as aMSSL, <br>H.BH as aBH, H.PM as aPM, H.DW as aDW from CGDD M &nbsp;<br>inner join HP H on M.HPID=H.ID where ZBID = 0<br><br>关键是case那行我屏蔽掉就没有问题了,看来aSHSL不能在别处使用,access却可以
 
我机器没装SqlServer所以没法测试 。。。在Orocle的环境下测试了一下。。。是会无法识别aSHSL。。。。不过再套一层查询就没问题了。。Case还是可以用的。。
 
(select sum(SL) from CGSH where DDID=M.ZBID and HPID=M.HPID)<br>替换aSHSL 就可以了,不过这样就 太长了,太乱了。
 
这样写就可以了<br>Select K.*,<br> &nbsp; case &nbsp;K.SL &gt; aSHSL when 1 then K.SL - K.aSHSL else 0 end as K.aMSSL from <br>( select M.*, <br> &nbsp; &nbsp; &nbsp; &nbsp;(select HH from CGDD where CGDD.ZBID=M.ZBID and CGDD.HH=M.HH) as TMP, <br> &nbsp; &nbsp; &nbsp; &nbsp;(select sum(SL) from CGSH where DDID=M.ZBID and HPID=M.HPID) as aSHSL, <br> &nbsp; &nbsp; &nbsp; &nbsp;H.BH as aBH, H.PM as aPM, H.DW as aDW <br> &nbsp; &nbsp; &nbsp; &nbsp;from CGDD M &nbsp;inner join HP H on M.HPID=H.ID &nbsp;) K
 
功夫不负有心人,终于在绝望的时候,解决了。<br> &nbsp; 呵呵。sql语句的使用技术提高了一大截。
 
把第四句改一下:<br> case &nbsp;M.SL-aSHSL when 1 then M.SL - aSHSL else 0 end as aMSSL,就可以了
 

Similar threads

后退
顶部