2016-05-09 90 views
0

我正在處理比較查詢,該比較查詢將比較不同信用卡類型處理的內容與我們的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_ranx.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列似乎我沒有得到一個問題乘以隨機間隔,我不知道爲什麼。

+0

[SQL服務器:在查詢中用0替換NULL]的可能重複(http://stackoverflow.com/questions/16840522/sql-server-replacing-null-with-0-in-a-query) – Laurel

+0

謝謝你的鏈接,但我已經嘗試過'ISNULL(SUM(t.amount)* - 1,0)AS集合'以及不顯示行 –

+0

的結果是'pay_desc' null? – Laurel

回答

1

這聽起來像你需要的是一個日曆表。您可以生成一個作爲CTE的一部分(本網站上的許多示例或僅搜索Google),但IMO應該在數據庫中包含一個持久表。這樣,您就可以標記某些天爲節假日,什麼四分之一的企業認爲他們在等

一旦你有一個表中的地方,你可以從該表從SELECT日期,然後LEFT OUTER JOIN到該表的Transactions表。在Transactions中沒有匹配行的任何日期仍會顯示爲一行,但您的SUM的價值爲0.00。

+0

這樣的聲音肯定會起作用,我會爲當年的測試創建一個快速表格,並在結果通知後再生成其餘部分。 –