我有一个练习管理软件,可以在不同的表中存储不同的活动。 下面列出。我想要实现的目标是找到具有应付余额,处于休眠状态或在过去30天内没有任何活动的帐户。活动类型包括索赔提交,付款和&分类帐。我在exel中实现了五个查询和Vlookup功能。但是,我想用一个SQL查询干净地完成这个。可能吗。以下是我目前使用的五个查询。
SELECT xrxTrnLgr.PatId, xrxTrnLgr.Balance,
FROM xrxTrnLgr
WHERE (xrxTrnLgr.Balance>$.01)
ORDER BY xrxTrnLgr.PatId
此查询旨在让患者达到平衡
SELECT xrxPat.PatId, xrxPat.Coverage, xrxPat.DctId, xrxPat.EntryDate
FROM xrxPat
WHERE (xrxPat.EntryDate>{ts '2008-01-01 00:00:00'})
ORDER BY xrxPat.PatId
此查询是为了获取医生ID
SELECT RecNo, xrxPatNotes.PatId, xrxPatNotes.NoteDate, xrxPatNotes.UserId
FROM xrxPatNotes INNER JOIN
(SELECT xrxPatNotes.PatId, Max (NoteDate) as LastDate
FROM xrxPatNotes
GROUP BY PatId) as B
ON xrxPatNotes.PatId = B.PatId
AND xrxPatNotes.NoteDate = B.LastDate
此查询是获取最新的Ledger Note
SELECT xrxTrnicf.PatId, xrxTrnicf.UserId ,xrxTrnicf.PostDate
FROM xrxTrnicf INNER JOIN
(SELECT xrxTrnicf.PatId, Max (PostDate) as LastDate
FROM xrxTrnicf
GROUP BY PatId) as B
ON xrxTrnicf.PatId = B.PatId
AND xrxTrnicf.PostDate = B.LastDat
此查询是为了获取最新的“已提交索赔”
SELECT xrxTrnpay.PatId, xrxTrnpay.UserId ,xrxTrnpay.PostDate
FROM xrxTrnpay INNER JOIN
(SELECT xrxTrnpay.PatId, Max (PostDate) as LastDate
FROM xrxTrnpay
GROUP BY PatId) as B
ON xrxTrnpay.PatId = B.PatId
AND xrxTrnpay.PostDate = B.LastDate
此查询是为了获取最近的付款
在exel中我然后VLOOKUP所有的值并从今天减去。然后我创建一个列来获取最小值。
如果有人能帮助我至少将sql查询合并到一个查询中,我将非常感激。
由于
答案 0 :(得分:1)
你可以使用5方式联盟。它相当冗长,但它可以解决问题。请注意,它仍然使用五个选择,因此它不会更有效(事实上,我怀疑它可能效率较低)。它只允许一个查询使用一个结果集:
SELECT PatId,
'xrxTrnLgr' AS tablename,
'Balance' AS key1, Balance AS value1,
NULL AS key2, NULL AS value2,
NULL AS key3, NULL AS value3
FROM xrxTrnLgr
WHERE (xrxTrnLgr.Balance>$.01)
UNION ALL
SELECT PatId,
'xrxPat' AS tablename,
'Coverage' AS key1, Coverage AS value1,
'DctId' AS key2, DctId AS value2,
'EntryDate' AS key3, EntryDate AS value3
FROM xrxPat
WHERE (xrxPat.EntryDate>{ts '2008-01-01 00:00:00'})
UNION ALL
SELECT PatId,
'xrxPatNotes' AS tablename,
'RecNo' AS key1, RecNo AS value1,
'NoteDate' AS key2, NoteDate AS value2,
'UserId' AS key3, UserId AS value3
FROM xrxPatNotes INNER JOIN
(SELECT xrxPatNotes.PatId, Max (NoteDate) as LastDate
FROM xrxPatNotes
GROUP BY PatId) as B
ON xrxPatNotes.PatId = B.PatId
AND xrxPatNotes.NoteDate = B.LastDate
UNION ALL
SELECT PatId,
'xrxTrnicf' AS tablename,
'UserId' AS key1, UserId AS value1,
'PostDate' AS key2, PostDate AS value2,
NULL AS key3, NULL AS value3
FROM xrxTrnicf INNER JOIN
(SELECT xrxTrnicf.PatId, Max (PostDate) as LastDate
FROM xrxTrnicf
GROUP BY PatId) as B
ON xrxTrnicf.PatId = B.PatId
AND xrxTrnicf.PostDate = B.LastDat
UNION ALL
SELECT PatId,
'xrxTrnpay' AS tablename,
'UserId' AS key1, UserId AS value1,
'PostDate' AS key2, PostDate AS value2,
NULL AS key3, NULL AS value3
FROM xrxTrnpay INNER JOIN
(SELECT xrxTrnpay.PatId, Max (PostDate) as LastDate
FROM xrxTrnpay
GROUP BY PatId) as B
ON xrxTrnpay.PatId = B.PatId
AND xrxTrnpay.PostDate = B.LastDate
ORDER BY PatId
这将返回一个包含列的结果集(tablename
,PatId
,key1
,value1
,key2
,value2
,{{ 1}},key3
)。 value3
结果将告诉您如何解释键/值对结果。