我正在进行比较查询,该比较查询将比较针对不同信用卡类型处理的内容与我们的POS服务实际处理的内容。我遇到了一个问题,如果他们在某一天没有处理任何付款但我可能已退款,我没有看到某些卡类型的行。 IE:没有购买任何VISA但是已经给出了VISA退款,但由于没有购买,即使该类型有退款,该行也不会显示。
这是我的问题:
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是选择,但扫描探索)然后我觉得仍然会错过行。
我想要的是,即使pay_desc
没有值,tran_date
也会显示每个SUM(t.amount)
。我已经尝试了一个案例陈述来完成这个没有任何运气。
t.time_ran
变量。如果没有给定pay_desc
的付款,那么也不会有t.time_ran
,是否有一个函数可以列出GETDATE() - 10和GETDATE()之间的日期那种事情只是让t.time_ran
和x.settle_date
匹配到那个变量?
对此有何想法?提前致谢
更新:
我创建了一个包含各个日期的日历表,并尝试了以下查询:
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 :(得分:1)
听起来你需要的是一个Calendar表。您可以生成一个作为CTE的一部分(本网站上的许多示例或只搜索Google),但IMO您应该在数据库中有一个持久表。通过这种方式,您可以将特定日期标记为银行假日,您的企业认为它们所处的季度等等。
在您准备好该表后,您可以SELECT
从该表中{1}}开始日期,然后从LEFT OUTER JOIN
表格Transactions
到该表格。 Transactions
中没有匹配行的任何日期仍会显示一行,但您SUM
的值为0.00。