2012-03-15 40 views
0

表结构:
tblCustomer tbl_transaction和tblCustomer由具有相同cardno之间只显示非零行计数

Customer_id created     field1   field2  cardno  field14 
------------------------------------------------------------------------------------------------  
1014   2010-05-25 12:51:59.547 Cell Phone  [email protected] 1234567890 Test Card 
1015   2010-08-15 12:51:59.547 Email    [email protected] 2345678891 NULL 

tbl_TransactionDishout

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

关系。

现有查询

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  

现在我想用非零计数行..

回答

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 
+0

相反的,你必须使用或...否则你是对的.. – 2012-03-15 11:32:33