我有一张桌子发票。我需要排除所有具有round( sum(QtyInvoiced),1) = 0 and round(sum(Amount),1) =0
我尝试了两个使用不同过滤器的查询:
查询1:返回12745行
select sum( QtyInvoiced ) , sum(Amount) ,salesNumber
from invoicesales
group by salesNumber
having round( sum(QtyInvoiced),1) <> 0 and round(sum(Amount),1)<>0
查询2 ::比Query1多返回13265行
select sum( QtyInvoiced ) , sum(Amount) ,salesNumber
from invoicesales
group by salesNumber
HAVING NOT(ROUND( SUM(QtyInvoiced),1) = 0 AND ROUND(SUM(Amount),1) = 0);
我不明白两个过滤器之间的区别是什么?
答案 0 :(得分:2)
您的second
查询可能有结果,在会议either of conditions
中,first
查询将只有在会议both conditions
中有结果。
如果您希望看到发票金额为正数时,我想您的sum(quantity)
是销售和退货净额。因此,如果您还想查看净值为0的数量,请使用第二个查询
如果您只想看到同时满足两个条件的行,请使用第一个查询。 这是示例
declare @table table (Salesnumber Int, QtyInvoiced int,Amount money )
insert @table
select 1 ,2,20 union all
select 1 ,1,-10 union all
select 1 ,1,0 union all
select 2 ,2,20 union all
select 2 ,2,20 union all
select 2 ,2,20 union all
select 3 ,2,20 union all
select 3 ,1,-10 union all
select 4 ,1,0 union all
select 5 ,2,-20 union all
select 5 ,2,-20 union all
select 5 ,2,20
--select * from @table
select sum( QtyInvoiced ) Quantity , sum(Amount) Amount ,salesNumber
from @table
group by salesNumber
having round( sum(QtyInvoiced),1) <> 0 and round(sum(Amount),1)<>0
select sum( QtyInvoiced )Quantity , sum(Amount) Amount ,salesNumber
from @table
group by salesNumber
HAVING NOT(ROUND( SUM(QtyInvoiced),1) = 0 AND ROUND(SUM(Amount),1) = 0);