从SQL中的操作忽略0值

时间:2015-08-24 17:58:13

标签: jquery sql sql-server

我有一个查询,我选择供应商代码,供应商名称,借记卡,贷记和借记减去贷方作为最终余额。

如何忽略最终余额的0值?

SELECT min(t1.ShortName), min(t0.cardname), 
    str(sum(t1.debit),15,2), str( sum(t1.Credit),13,2), 
    str(isnull(sum(T1.DEBIT) - abs(sum(T1.CREDIT)),0),13,2)[FINAL] 
FROM JDT1 t1 
INNER JOIN OCRD t0 ON t1.ShortName = t0.CardCode 
WHERE t0.CardType = 'S' AND year(t1.taxDate) = year(@Fecha)
GROUP BY t1.ShortName, t0.CardName
ORDER BY t1.ShortName

2 个答案:

答案 0 :(得分:3)

您所要做的就是添加到having子句:

SELECT Min(t1.ShortName),
       Min(t0.cardname),
       Str(Sum(t1.debit), 15, 2),
       Str(Sum(t1.Credit), 13, 2),
       Str(Isnull(Sum(T1.DEBIT) - Abs(Sum(T1.CREDIT)), 0), 13, 2)[FINAL]

  FROM JDT1 t1
       INNER JOIN OCRD t0
               ON t1.ShortName = t0.CardCode
 WHERE t0.CardType = 'S'
   AND Year(t1.taxDate) = Year(@Fecha)
 GROUP BY t1.ShortName,
          t0.CardName
 having Str(Isnull(Sum(T1.DEBIT) - Abs(Sum(T1.CREDIT)), 0), 13, 2) <> 0 
 ORDER BY t1.ShortName 

答案 1 :(得分:2)

将结果转储到#temp表中,然后从带有条件的where子句的表中选择,如下所示:

SELECT   Employee.Name      
, Employee.ID       
, Sales.Date         
, Sales.Money            
FROM Database.Sales.Sales       
INNER JOIN Database.Employee.Employee        
ON Sales.ID=Employee.ID        
WHERE Sales.Date BETWEEN '2000-01-01' AND '2001-01-01'            
ORDER BY Employee.Name