如果case为TRUE,则IIF不能除以给定值

时间:2018-01-05 08:58:31

标签: sql sql-server

'ItemCount'等于2,但CASE WHENIIF函数无法显示TRUE结果值。

QUERY:

SELECT
  c.Dscription,
  b.BaseRef AS 'DocNum',
  b.Ref2 AS 'NumatCard',
  CASE
    WHEN c.ItemCount = 2 THEN (b.BalDueDeb - b.BalDueCred) / 2
    ELSE (b.BalDueDeb - b.BalDueCred)
  END AS 'Balance'
FROM OJDT a
INNER JOIN JDT1 b
  ON a.TransID = b.Transid
LEFT JOIN (SELECT DISTINCT
  t2.docnum,
  t3.Dscription,
  t2.TransId,
  (COUNT(t3.ItemCode) AS 'ItemCount'
FROM OINV T2
INNER JOIN INV1 T3
  ON T2.DocEntry = T3.DocEntry
LEFT JOIN ORIN v
  ON LEFT(v.NumAtCard, 6) = LEFT(t2.NumAtCard, 6)
LEFT JOIN RCT2 s
  ON s.baseAbs = t2.DocEntry
WHERE t3.Quantity > 0
AND t2.CANCELED = 'N'
GROUP BY t2.docnum,
         T2.TransId,
         t3.Dscription,
         t3.ItemCode
HAVING COUNT(t2.DocNum) = 1) c
  ON a.TransId = c.TransId
INNER JOIN OACT T5
  ON T5.AcctCode = b.Account
INNER JOIN OCRD T1
  ON b.ShortName = T1.CardCode
  AND T1.CardType = 'C'
WHERE T5.FORMATCODE = 11020103
AND (b.balduecred <> 0
OR b.balduedeb != 0)
AND b.BaseRef = 100078166
GROUP BY c.Dscription,
         b.BaseRef,
         b.Ref2,
         b.BalDueDeb,
         b.BalDueCred,
         c.ItemCount

结果:

Dscription      DocNum      NumatCard   Balance
CAREER GUIDE    100078166   88920-42    10560
CLASSIFINDER    100078166   88920-42    10560

应该是:

Dscription      DocNum      NumatCard   Balance
CAREER GUIDE    100078166   88920-42    5280
CLASSIFINDER    100078166   88920-42    5280

DATA:

Dscription      DocNum      NumatCard   Balance   ItemCount   Quantity
CAREER GUIDE    100078166   88920-42    5280      2           24
CLASSIFINDER    100078166   88920-42    5280      2           24
DISPLAY         100048374   551493      9623      1           15
DISPLAY         100048586   551894      10483     2           17
CAREER GUIDE    100048586   551894      10483     2           17

1 个答案:

答案 0 :(得分:0)

我意识到这不是问题的答案,但我们没有基于它的样本数据。我必须做的是修改SQL一点点;也许它会帮助别人回答这个问题。

我在评论中写了几点。不需要在SELECT语句中使用GROUP BY a。这只会增加服务器的开销,但不会带来额外的好处。

另外,别名。为对象设置别名时,请使用代表该对象的内容。在每个查询中调用bt1t2FROM内容都没有用,因为阅读代码的其他人必须通过C每次他们查看别名时都会使用子句(特别是在原始查询中,它们遍布整个地方)。无论如何,查询中的表都有很短的名称,所以真的不需要别名。但是,如果你有更大的名字,那么使用有用的别名。例如Customer可能COCustomerOrder BADBusinessAddressData Customer(您明白了)。

正如我希望你能够理解的那样,如果在一个查询中表格T1是别名CustomerOrderT2Customer,那么对某些人来说会很困惑。另一个T3BusinessAddressDataCustomerOrder是T1,aWITH Docs AS ( --Put your LEFT JOIN on a SELECT Statement into a CTE SELECT OINV.docnum, --There was no need for a DISTINCT here, you are using GROUP BY INV1.Dscription, O.TransId, COUNT(INV1.ItemCode) AS ItemCount --Why cast a COUNT to an INT? COUNT returns an INT. The CAST would just create extra overhead. FROM OINV --Use helpful Aliases. These object only have 4 character names, so really no need to use an Alias INNER JOIN INV1 ON OINV.DocEntry = INV1.DocEntry LEFT JOIN ORIN ON LEFT(ORIN.NumAtCard, 6) = LEFT(OINV.NumAtCard, 6) LEFT JOIN RCT2 ON RCT2.baseAbs = OINV.DocEntry WHERE INV1.Quantity > 0 AND OINV.CANCELED = 'N' GROUP BY OINV.docnum, OINV.TransId, INV1.Dscription, INV1.ItemCode HAVING COUNT(OINV.DocNum) = 1) SELECT Docs.Dscription, JDT1.BaseRef AS DocNum, JDT1.Ref2 AS NumatCard, CASE WHEN Docs.ItemCount = 2 THEN (JDT1.BalDueDeb - JDT1.BalDueCred) / 2 ELSE (JDT1.BalDueDeb - JDT1.BalDueCred) END AS Balance FROM OJDT --Again, no need for Aliases such as a, b, c. They aren't helpful. When looking at code, an Alias should be representative of the object's name INNER JOIN JDT1 ON OJDT.TransID = JDT1.Transid LEFT JOIN Docs ON OJDT.TransId = Docs.TransId INNER JOIN OACT ON JDT1.Account = OACT.AcctCode INNER JOIN OCRD ON JDT1.CardCode = OCRD.ShortName WHERE OCRD.CardType = 'C';

无论如何,这是(我觉得)同一查询的更具可读性的版本:

{{1}}