仅显示非零行数

时间:2012-03-15 10:52:27

标签: sql-server sql-server-2008

表结构:
tblCustomer

Customer_id  created                 field1            field2        cardno       field14
------------------------------------------------------------------------------------------------    
1014         2010-05-25 12:51:59.547 Cell Phone        abc@lmn.com   1234567890   Test Card
1015         2010-08-15 12:51:59.547 Email             abc@xyz.com   2345678891   NULL

tbl_TransactionDishout

Trnx_id   offerNo   TerminalID      Created                  VirtualCard
-------------------------------------------------------------------
1         1014      170924690436418 2010-05-25 12:51:59.547  1234567890

tbl_transaction和tblCustomer之间的关系具有相同的cardno。

现有查询

SELECT 
convert(varchar, CAST(t2.created AS DATETIME), 111) created, 
COUNT(CASE WHEN t2.DishoutResponseCode = '0000' and t1.field14 <> 'Test card' THEN 1      END) as  Deals_Redeemed,
COUNT(CASE WHEN t2.DishoutResponseCode <> '0000' and t1.field14 <> 'Test card' THEN 1   END) as Non_Deals 
FROM tblCustomer AS t1, tbl_TransactionDishout t2 
where t1.cardno = t2.VirtualCard
and convert(varchar, CAST(t2.created AS DATETIME), 111) >= (select        MAX(convert(varchar, a.Created, 111)) 
from tbl_Offer as a,tbl_TransactionDishout as b where cast(a.OFID as varchar) =     b.OfferNo and a.Live = '1')
GROUP BY convert(varchar, CAST(t2.created AS DATETIME), 111) 
ORDER BY convert(varchar, CAST(t2.created AS DATETIME), 111) DESC       



created     Deals_Redeemed  Non_Deals    
-------------------------------------
2012/03/02  0               0
2012/03/01  0               1
2012/02/28  2               0
2012/02/27  0               0      

现在我想要非零计数的行..

1 个答案:

答案 0 :(得分:2)

使用HAVING条款。

GROUP BY放置后

HAVING  COUNT(CASE WHEN t2.DishoutResponseCode = '0000' and t1.field14 <> 'Test card' THEN 1      END) > 0
AND COUNT(CASE WHEN t2.DishoutResponseCode <> '0000' and t1.field14 <> 'Test card' THEN 1   END) > 0