从两个表中计算边距SQL

时间:2014-07-08 18:45:53

标签: sql vba

所以我遇到了一个问题,我已将其与WHERE语句的一部分隔离开来,我无法弄明白。我根据与之关联的成本和价格表计算项目的保证金。如果我想将其限制为列表价格低于17%的商品,它可以正常工作,但是如果我想检查其他价格等级,例如定价如果客户购买+100单位,我会收到ODBC错误。这是我的代码不起作用:

            strSQL = "SELECT IMFGR || ICOLOR || IPATT as Item, INAME as Description1, INAME2 as Description2,  IPRCCD as PClass, ICSTCD as CClass, " _
               & "c.TLASTC as Cost, p.$LIST as List, ((p.$LIST-c.TLASTC)/p.$LIST) as GPM, p.$P2 as Price2, ((p.$P2-c.TLASTC)/p.$P2) as GPM2, " _
               & "p.$P3 as Price3, ((p.$P3-c.TLASTC)/p.$P3) as GPM3, p.$P4 as Price4, ((p.$P4-c.TLASTC)/p.$P4) as GPM4, p.$P5 as Price5, ((p.$P5-c.TLASTC)/p.$P5) as GPM5, " _
               & "p.$P6 as Price6, ((p.$P6-c.TLASTC)/p.$P6) as GPM6, p.$P7 as Price7, ((p.$P7-c.TLASTC)/p.$P7) as GPM7, p.$P8 as Price8, ((p.$P8-c.TLASTC)/p.$P8) as GPM8, " _
               & "p.$P9 as Price9, ((p.$P9-c.TLASTC)/p.$P9) as GPM9, p.$P10 as Price10, ((p.$P10-c.TLASTC)/p.$P10) as GPM10, p.$P11 as Price11, ((p.$P11-c.TLASTC)/p.$P11) as GPM11, " _
               & "p.$P12 as Price12, ((p.$P12-c.TLASTC)/p.$P12) as GPM12 " _
               & "FROM ITEM i " _
               & "LEFT JOIN COST c " _
                    & "ON i.ICSTCD = c.TCSTCD " _
               & "LEFT JOIN PRICE p " _
                    & "ON i.IPRCCD = p.$PRCCD " _
               & "WHERE (IPRCCD != '') AND (p.$LIST# = 'LP') AND (IPOL1 != 'DI' AND IPOL2 != 'DI' AND IPOL3 != 'DI') AND (IPOL1 != 'BR' AND IPOL2 != 'BR' AND IPOL3 != 'BR') AND (IMFGR = '" & man & "') AND " _
               & "((ICCTR != 'ZDS') AND (IPRODL != 'XXX')) AND " _
               & "((p.$LIST-c.TLASTC)/p.$LIST) <= '" & margin & "' OR ((p.$P2-c.TLASTC)/p.$P2) <= '" & margin & "' " _
               & "ORDER BY IMFGR"

倒数第二行的OR会生成错误。如果我将其更改为AND,它会运行,但它会查找两个语句为真。如果任何边距低于某个%,我想返回结果。我做错了什么?

不是p。$ LIST#=&#39; LP&#39;因为p。$ p2,p。$ p3等仍然存在于&#39; LP&#39;

1 个答案:

答案 0 :(得分:0)

AND优先于OR。你需要将OR子句包装在parens中:

  & "(((p.$LIST-c.TLASTC)/p.$LIST) <= '" & margin & "' OR ((p.$P2-c.TLASTC)/p.$P2) <= '" & margin & "') " _