当前位置: 代码迷 >> Sql Server >> 两表之间的查询有关问题
  详细解决方案

两表之间的查询有关问题

热度:85   发布时间:2016-04-24 09:57:33.0
两表之间的查询问题
有两个表,ycsy.dbo.pos_tranpay(flowno,operDate,payWay)对应项为(交易号,操作时间,支付方式)和
                    ycsy.dbo.tc_inoutflow(voncherno,operDate,SalePice,PreQty,leftQty,Remark)对应项为(交易号,操作时间,单价,售前数量,售后数量,操作类型)。其中操作类型为“前台正常%”“前台销售退货”的为计算项。
我欲按照操作时间查找出支付方式为A的交易金额和交易笔数。现有语句如下:
select convert(varchar(100),i.operDate,23) as 日期,       
       count(distinct i.voucherno) as 交易笔数,
       sum(SalePrice*(PreQty-leftQty)) as 现金消费金额
from ycsy.dbo.tc_inoutflow i
       where i.Remark like '前台正常%' or i.Remark='前台销售退货'
       and i.voucherno in (
           select  distinct flowno 
           from ycsy.dbo.pos_tranpay t,ycsy.dbo.tc_inoutflow 
           where payWay='A')
group by convert(varchar(100),i.operDate,23)
order by convert(varchar(100),i.operDate,23)

执行出来的交易金额的结果与预先知道的有着很大的出入,语句能够被执行,表项之间的计算公式也验证过是正确的,会不会是逻辑上有什么大错误,看了好久,求教~
------解决思路----------------------
 where i.Remark like '前台正常%' or i.Remark='前台销售退货'
       and i.voucherno in (
           select  distinct flowno 
           from ycsy.dbo.pos_tranpay t,ycsy.dbo.tc_inoutflow 
           where payWay='A')

不知道是不是你条件的问题,and 比or 的优先级高
 where (i.Remark like '前台正常%' or i.Remark='前台销售退货')
       and i.voucherno in (
           select  distinct flowno 
           from ycsy.dbo.pos_tranpay t,ycsy.dbo.tc_inoutflow 
           where payWay='A')
现在看看是否数据正确。
------解决思路----------------------
and和or使用的时候,一定要用括号进行分级。。。要不看着乱套不说,逻辑都可能是错的。
  相关解决方案