我很难找到如何只返回结果集中claimtid
的最新迭代。我正在使用此查询:
SELECT
claimid, paiddate
CASE
WHEN actid = '119' THEN 'Channel Exception'
WHEN actid = '127' THEN 'Rejected'
WHEN actid = '128' THEN 'Accepted'
WHEN actid = '130' THEN 'Adjustment Complete'
WHEN actid = '133' THEN 'Channel Ready'
END AS [Status]
FROM
Encounter
WHERE
claimtid LIKE '173225AR0%' OR claimtid LIKE '197565GL0%' OR
claimtid LIKE '293215QW0%' OR claimtid LIKE
ORDER BY
claimtid
此查询返回以下结果:
|claimtid |paiddt |Status |
-+----------+----------+-------------------+
1|173225AR00|2017-03-01|Adjustment Complete|
2|173225AR01|2017-04-11|Accepted |
3|197565GL00|2017-03-17|Accepted |
4|197565GL01|2017-03-19|Adjustment Complete|
5|197565GL02|2017-04-01|Rejected |
6|293215QW00|2017-04-19|Adjustment Complete|
7|293215QW01|2017-04-23|Accepted |
我不确定我可以添加到查询中的内容,以便结果只会返回第2行,第5行和第7行。我的实际查询包含会在结果中生成更多行。
这只是一个例子,但对于这种情况是准确的。我需要回退超过3行,但它需要是最新的迭代。
每次额外的迭代都会使claimtid中的最后一个数字增加一个。我不知道每个claimtid有多少次迭代。
答案 0 :(得分:0)
试试这个,但我不确定在你的情况下使用字符串for claimtid的性能损失:
SELECT claimid, paiddate
FROM
Encounter
WHERE claimtid IN (
SELECT MAX(paiddt), claimtid
FROM Encounter
GROUP BY SUBSTRING (claimtid, 6, 8)
) t
假设您可以依靠paydate字段来了解最新记录。
答案 1 :(得分:0)
如果您无法将声明ID的两个字段分隔为数据表本身的单独列,则可以在查询中将它们分开,以允许您使用max()
聚合查找第二个字段的最大值。
select
claim,
paiddate,
CASE
WHEN actid = '119' THEN 'Channel Exception'
WHEN actid = '127' THEN 'Rejected'
WHEN actid = '128' THEN 'Accepted'
WHEN actid = '130' THEN 'Adjustment Complete'
WHEN actid = '133' THEN 'Channel Ready'
END AS [Status]
from
(
select
left(claimid, 6) as claim,
max(right(claimid,4)) as seq,
from
Encounter
group by
left(claimid, 6)
) as ms
inner join Encounter as e
on e.claimid = ms.claim + ms.seq;
答案 2 :(得分:0)
这应该可以解决问题......
IF OBJECT_ID('tempdb..#ClaimData', 'U') IS NOT NULL
DROP TABLE #ClaimData;
CREATE TABLE #ClaimData (
RN INT NOT NULL IDENTITY(1,1),
claimtid CHAR(10) NOT NULL,
paiddt DATE NOT NULL,
[Status] VARCHAR(20) NOT NULL
);
INSERT #ClaimData (claimtid, paiddt, Status) VALUES
('173225AR00', '2017-03-01', 'Adjustment Complete'),
('173225AR01', '2017-04-11', 'Accepted'),
('197565GL00', '2017-03-17', 'Accepted'),
('197565GL01', '2017-03-19', 'Adjustment Complete'),
('197565GL02', '2017-04-01', 'Rejected'),
('293215QW00', '2017-04-19', 'Adjustment Complete'),
('293215QW01', '2017-04-23', 'Accepted');
--SELECT * FROM #ClaimData cd;
--=========================================================
SELECT TOP 1 WITH TIES
cd.RN, cd.claimtid, cd.paiddt, cd.Status
FROM
#ClaimData cd
CROSS APPLY ( VALUES (SUBSTRING(cd.claimtid, 1, 6), SUBSTRING(cd.claimtid, 7, 4)) ) sc (Claim_1, Claim_2)
ORDER BY
ROW_NUMBER() OVER (PARTITION BY sc.Claim_1 ORDER BY sc.Claim_2 DESC);
结果...
RN claimtid paiddt Status
----------- ---------- ---------- --------------------
2 173225AR01 2017-04-11 Accepted
5 197565GL02 2017-04-01 Rejected
7 293215QW01 2017-04-23 Accepted
编辑... 性能稍好的解决方案可以产生相同的结果...
SELECT
RN = CAST(SUBSTRING(MAX(bv.BinaryValue), 39, 4) AS INT),
claimtid = CAST(SUBSTRING(MAX(bv.BinaryValue), 1, 10) AS CHAR(10)),
paiddt = CAST(SUBSTRING(MAX(bv.BinaryValue), 11, 8) AS DATE),
Status = CAST(SUBSTRING(MAX(bv.BinaryValue), 19, 20) AS VARCHAR(20))
FROM
#ClaimData cd
CROSS APPLY ( VALUES (CAST(cd.claimtid AS BINARY(10)) + CAST(cd.paiddt AS BINARY(8)) + CAST(cd.Status AS BINARY(20)) + CAST(cd.RN AS BINARY(4))) ) bv (BinaryValue)
GROUP BY
SUBSTRING(cd.claimtid, 1, 6);
答案 3 :(得分:0)
尝试这一点,假设你不能及早获得以后付款的声明:
IF OBJECT_ID('tempdb..#ClaimData') IS NOT NULL
DROP TABLE #ClaimData
CREATE TABLE #ClaimData (
ID INT NOT NULL IDENTITY(1,1)
, claimtid CHAR(10) NOT NULL
, paiddt DATE NOT NULL
, [Status] VARCHAR(20) NOT NULL
)
INSERT #ClaimData (claimtid, paiddt, Status) VALUES
('173225AR00', '2017-03-01', 'Adjustment Complete')
,('173225AR01', '2017-04-11', 'Accepted')
,('197565GL00', '2017-03-17', 'Accepted')
,('197565GL01', '2017-03-19', 'Adjustment Complete')
,('197565GL02', '2017-04-01', 'Rejected')
,('293215QW00', '2017-04-19', 'Adjustment Complete')
,('293215QW01', '2017-04-23', 'Accepted')
SELECT
ID
, x.claimtid
, x.paiddt
, x.Status
FROM (
SELECT
ROW_NUMBER() OVER (PARTITION BY LEFT(Claimtid, LEN(Claimtid) - 2) ORDER BY paiddt DESC) RN
, *
FROM #ClaimData
) x
WHERE x.RN = 1
否则将RN
更改为ROW_NUMBER() OVER (PARTITION BY LEFT(Claimtid, LEN(Claimtid) - 2) ORDER BY RIGHT(Claimtid, 2) DESC) RN