表構造:
tblCustomer間違った出力、JOINクエリ
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
tbl_TransactionDishout
Trnx_id offerNo TerminalID Created VirtualCard
-------------------------------------------------------------------
1 1014 170924690436418 2010-05-25 12:51:59.547 1234567890
は、日付ごとのレコードを以下のように結果を取得することは可能です:
Enrolled Enrolled as Email Enrolled as Text Deals Redeemed
<First Date> 7 5 2 6
<Next Date> 9 3 6 14
既存のクエリ:今
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,
t1.field14,
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 (t1.field14 <> 'Test Card' and t1.field14 is null) 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),
t1.field14
ORDER BY
convert(varchar, CAST(ISNULL(t1.created,t2.created) AS DATETIME), 111) DESC
、ここで私は「テストを、私はまたはResponseCode = 0000を持っており、昨日tbl_TransactionDishoutテーブル内の1つのレコードを持っているので、4列目は何か間違ったことを実行している一つの問題にされていません直面しています私は0カウントを得ています。
tbl_transactionとtblCustomerが同じCARDNOをしているとの間の関係は...
あなたは本当に 'field1'と' field2'を使用してスキーマを持っています? – MatBailie
サンプルデータにDishoutResponseCodeが表示されていません。また、列4は、フィールド14がヌルであることをチェックします。一致する行にはありません。 – kaj
それでは、正しい解決策が必要です。 私は両方の条件を一致させることができます –