我正在處理比較查詢,該比較查詢將比較不同信用卡類型處理的內容與我們的POS服務實際處理的內容。我遇到了一個問題,如果某些卡類型未在特定日期處理任何付款,但可能已退款,我不會看到某些類型的行。 IE:沒有用VISA購買,但簽發了VISA退款,但由於沒有購買,該行不顯示,即使該類型退款。顯示0爲空行
這裏是我的查詢:
WITH tran_total AS (
SELECT CONCAT(c.id_str, ' - ', c.clinic_str) AS Clinics,
t.clinic,
c.xcharge_mid,
p.pay_desc,
CAST(t.time_ran AS date) AS tran_date,
CASE WHEN SUM(t.amount) <> 0 THEN SUM(t.amount) * - 1
ELSE 0.00 END AS collection
FROM dbo.transactions AS t INNER JOIN
dbo.clinic_master AS c ON t.clinic = c.clinic INNER JOIN
dbo.paytype AS p ON t.clinic = p.clinic AND t.paytype_id = p.paytype_id
WHERE (t.time_ran > GETDATE() - 10)
AND (t.paytype_id IS NOT NULL)
AND (p.pay_desc = 'Visa' OR
p.pay_desc = 'MasterCard' OR
p.pay_desc = 'American Express' OR
p.pay_desc = 'Discover')
GROUP BY c.id_str, c.clinic_str, t.clinic, c.xcharge_mid, p.pay_desc, CAST(t.time_ran AS date))
SELECT w.Clinics,
w.pay_desc,
w.tran_date,
w.collection,
CASE WHEN w.pay_desc = 'Visa' THEN (SUM(VI_pur) - SUM(VI_ref))
WHEN w.pay_desc = 'MasterCard' THEN (SUM(MC_pur) - SUM(MC_ref))
WHEN w.pay_desc = 'American Express' THEN (SUM(AX_pur) - SUM(AX_ref))
WHEN w.pay_desc = 'Discover' THEN (SUM(DI_pur) - SUM(DI_ref)) END AS xcharge
FROM tran_total AS w LEFT OUTER JOIN
dbo.xcharge AS x ON w.xcharge_mid = x.xcharge_mid AND w.tran_date = x.settle_date
GROUP BY w.Clinics, w.pay_desc, w.tran_date, w.collection
ORDER BY w.Clinics, w.tran_date
我想過圍繞這種開關,並與來自xcharge
表的比較開始,但沒有一個pay_desc
,可以輕鬆地連接這些如果事情被處理爲錯誤的卡(選擇了VISA,但發現掃描),那麼我覺得行仍然會被遺漏。
我想要的是,即使SUM(t.amount)
沒有值,每個pay_desc
都會出現在每個tran_date
之間。我已經試過一個案例陳述來完成這個沒有任何運氣。
編輯:我覺得這個問題更多是由於t.time_ran
變量。如果沒有給定的pay_desc
的支付,那麼也不會有t.time_ran
,有沒有我可以做的函數來列出GETDATE() - 10和GETDATE()之間的某種東西,只需要t.time_ran
和x.settle_date
是否匹配該變量?
對此有何看法?在此先感謝
UPDATE:
我創建了一個日曆表,個別日期,並曾嘗試以下查詢:
WITH tran_test AS(
SELECT cal.calendardate AS cd,
p.clinic,
p.pay_desc,
p.paytype_id
FROM calendar cal, paytype p
WHERE cal.calendardate BETWEEN GETDATE()-10 AND GETDATE()+1
AND (p.pay_desc = 'Visa' OR
p.pay_desc = 'MasterCard' OR
p.pay_desc = 'American Express' OR
p.pay_desc = 'Discover'))
SELECT w.cd,
CONCAT(c.id_str, ' - ', c.clinic_str) AS Clinics,
w.pay_desc,
ISNULL(SUM(t.amount)*-1, 0) AS collection,
CASE WHEN w.pay_desc = 'Visa' THEN (SUM(x.VI_pur) - SUM(x.VI_ref))
WHEN w.pay_desc = 'MasterCard' THEN (SUM(x.MC_pur) - SUM(x.MC_ref))
WHEN w.pay_desc = 'American Express' THEN (SUM(x.AX_pur) - SUM(x.AX_ref))
WHEN w.pay_desc = 'Discover' THEN (SUM(x.DI_pur) - SUM(x.DI_ref)) END AS xcharge
FROM tran_test w
INNER JOIN clinic_master c
ON (w.clinic=c.clinic)
LEFT OUTER JOIN transactions t
ON (t.clinic=w.clinic AND t.paytype_id=w.paytype_id AND CAST(t.time_ran AS date) = w.cd)
LEFT OUTER JOIN xcharge x
ON (w.cd=x.settle_date AND c.xcharge_mid=x.xcharge_mid)
GROUP BY w.cd, c.id_str, c.clinic_str, w.pay_desc
ORDER BY w.cd
然而,當我xcharge
列似乎我沒有得到一個問題乘以隨機間隔,我不知道爲什麼。
[SQL服務器:在查詢中用0替換NULL]的可能重複(http://stackoverflow.com/questions/16840522/sql-server-replacing-null-with-0-in-a-query) – Laurel
謝謝你的鏈接,但我已經嘗試過'ISNULL(SUM(t.amount)* - 1,0)AS集合'以及不顯示行 –
的結果是'pay_desc' null? – Laurel