2012-03-13 6 views
0

테이블 구조 : tbl_transaction 및 tblCustomer 사이
tblCustomer다중 쿼리 서브 쿼리 가입

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 

관계는 동일 cardno 데있다. ..

   Enrolled Enrolled as Email Enrolled as Text Deals Redeemed 
<First Date> 7   5     2    6 
<Next Date> 9   3     6    14 

날짜 제로 기록이있는 경우에도 두 테이블에서해야

Enrolled   - Total No of records that is summation of Enrolled as Email and Enrolled as Text.  
Enrolled as Email - Where field1 = 'Email' from tblCustomer table 
Enrolled as Text - Where field1 = 'cell phone' from tblCustomer table 
Deals Redeemed - Where field14 <> 'Test Card' from tblCustomer table and  
        where DishoutResponsecode = '0000' from tbl_Transaction table 

내 기존 쿼리 :


는 일이 많다는 기록 아래와 같은 결과를 얻을 그것은 가능 tblCustomer 테이블에
SELECT 
convert(varchar, CAST(ISNULL(t1.created,t2.created) AS DATETIME), 111) created, 
COUNT(CASE WHEN (t1.field1 = 'E-mail' or t1.field1 = 'Cell Phone') and (t1.field14 <> 'Test Card' or t1.field14 is null) THEN 1 END) Enrolled, 
COUNT(CASE WHEN t1.field1 = 'E-mail'   and (t1.field14 <> 'Test Card' and t1.field14 is null) THEN 1 END) Enrolled_as_Email, 
COUNT(CASE WHEN t1.field1 = 'Cell Phone'  and (t1.field14 <> 'Test Card' and t1.field14 is null) THEN 1 END) Enrolled_as_Cell, 
COUNT(CASE WHEN t2.DishoutResponseCode = '0000' and (IsNull(t1.field14, '') <> 'Test Card') THEN 1 END) Deals_Redeemed 
FROM tblCustomer AS t1 
FULL OUTER JOIN 
     tbl_TransactionDishout t2 
ON t1.cardno = t2.VirtualCard and t1.created = t2.created 
GROUP BY 
     convert(varchar, CAST(ISNULL(t1.created,t2.created) AS DATETIME),  111) 
ORDER BY 
     convert(varchar, CAST(ISNULL(t1.created,t2.created) AS DATETIME), 111) DESC   

마지막 4-5 기록

tbl_TransactionDisout 테이블에 515,
created     cardno    field14 
---------------------------------------------------------- 
2012-03-07 10:03:00.034 1234007600101240  
2012-03-05 04:02:00.040 1234007600602122  
2012-03-01 06:25:50.400 1234010400972168  Test Card 
2012-03-01 30:05:30.022 555566669999   Test Card 
2012-03-01 50:50:20.450 666677778888   Test Card  

마지막 4-5 기록

created     VirtualCard   DishoutResponseCode 
----------------------------------------------------------------------- 
2012-03-09 13:18:02.703 1234010400972168 0010 
2012-03-09 13:17:35.307 1234010400972168 0002 
2012-03-09 13:17:14.237 1234010400972168 0007 
2012-03-09 13:16:57.650 1234010400972168 0002 
2012-03-08 21:13:57.137 1234010400475686 0000 
2012-03-08 16:50:38.273 1234010400972168 0002 
2012-03-08 16:50:26.070 1234010400972168 0007 
2012-03-08 16:49:49.793 1234010400972168 0002  

그래서 거기에 응답 코드 '0000'도 아닌 '테스트 카드'를 가진이 단지 하나 개의 카드입니다 ..하지만 코드 = '0000'을 가진 모든 카드를 가져오고 또한 '테스트 카드'를 가지고 있기 때문에 field14가 다른 테이블과 다른 날짜에 있기 때문에 비교할 수 없습니다.

+0

이 같은 질문을 물어 아마 세 번째 날입니다. 매일 새로운 질문을하는 이유는 무엇입니까? – Jaques

+0

@@Jaques 원하는 응답을 얻지 못하기 때문에 –

+0

DishoutResponseCode를 지정했기 때문에 tbl_TransactionDishout 테이블의 레이아웃은 무엇입니까?하지만 상단에 표시하지 않습니다. 두 번째는 이 시간을 제외한 날짜입니까? 하루에 그룹으로 묶으시겠습니까? – Jaques

답변

0

이것은 내가 돌아 오는 결과입니다 주요 질문에서 지정한 데이터를 사용할 때이 쿼리와 함께 :

SELECT 
    convert(varchar, CAST(ISNULL(t1.created,t2.created) AS DATETIME), 111) created, 
    COUNT(CASE WHEN (t1.field1 = 'Email' or t1.field1 = 'Cell Phone') and (t1.field14 <> 'Test Card' or t1.field14 is null) THEN 1 END) Enrolled, 
    COUNT(CASE WHEN t1.field1 = 'Email'   and (IsNull(t1.field14, '') <> 'Test Card') THEN 1 END) Enrolled_as_Email, 
    COUNT(CASE WHEN t1.field1 = 'Cell Phone'  and (IsNull(t1.field14, '') <> 'Test Card') THEN 1 END) Enrolled_as_Cell, 
    COUNT(CASE WHEN t2.DishoutResponseCode = '0000' and (IsNull(t1.field14, '') <> 'Test Card') THEN 1 END) Deals_Redeemed 
FROM 
    tblCustomer AS t1 
FULL OUTER JOIN 
    tbl_TransactionDishout t2 
    ON t1.cardno = t2.VirtualCard 
    AND t1.created = t2.created 
GROUP BY 
    convert(varchar, CAST(ISNULL(t1.created,t2.created) AS DATETIME), 111) 
ORDER BY 
    convert(varchar, CAST(ISNULL(t1.created,t2.created) AS DATETIME), 111) DESC 

결과 :

created      Enrolled Enrolled_as_Email Enrolled_as_Cell Deals_Redeemed 
------------------------------ ----------- ----------------- ---------------- -------------- 
2012/03/09      0   0     0    0 
2012/03/08      0   0     0    1 
2012/03/07      1   0     1    0 
2012/03/05      1   0     1    0 
2012/03/01      0   0     0    0 
+0

지금 ..ditoutResponsecode = '0000'인데 모든 열을 보여 주지만 tblcustomer에서 field14를 확인하지 않습니다 ... 그래서 내가 발견 한 문제는 다음과 같습니다. field14는 tblCustomer 테이블의 날짜도 확인합니다. 그래서 corss 수표가 작동하지 않습니다 .. @ Jaques –

+0

tblcustomer의 field14를 확인하지 않음으로써 무슨 뜻인지 이해가되지 않습니다. 코드가 올바르게 지정 되었습니까? 어쩌면 다시 사용하는 쿼리를 게시해야합니다. 생성 된 필드는 항상 tblCusomer와 tbl_TransactionDishout에서 동일합니까? 추가 데이터를 추가 할 수 있습니까? 왜냐하면 테스트 할 수 있도록 만들었 기 때문입니다. – Jaques

+0

@VishalSuthar 지금 작성한 테이블에서이 쿼리를 사용했는데 예상되는 결과를 얻었습니다.예상되는 결과를 산출하지 못하는 데이터를 제공하십시오. 그런 다음 잘못된 결과를 볼 수 있습니다. – Jaques