SQL使用CTE选择第一行

时间:2018-07-03 16:50:07

标签: sql sql-server common-table-expression

我有如下的sql join查询,该查询返回多行相同但列paylinelineitem不同的行。

DECLARE @fidelisInterestItems TABLE  (claim float,payment float,code varchar(50),interest money,paymentlineitem float);

INSERT INTO @fidelisInterestItems
Select 4250692,4104926,'T4535',2.04,9425737 Union All
Select 3677431,3685465,'L1820',2.63,8485100

CREATE TABLE #tmpReport(Claim int,HCPCS varchar(50),ClaimItem int,PaymentLineItem int,Interest money,Payment int)

Insert INTO #tmpReport
 Select P.Claim, ct.code, pl.claimItem, PL.PaymentLineItem,fi.Interest,P.Payment
FROM payment p with(nolock)
        JOIN paymentlineitem pl on p.payment=pl.payment 
        JOIN cpt ct on ct.cpt=pl.cpt 
        JOIN Eob e on e.eob=p.eob 
        JOIN Eobbatch eb on eb.eobbatch=e.eobbatch 
        JOIN @fidelisInterestItems fi on p.payment=fi.payment AND p.claim=fi.claim AND ct.code=fi.code 
        WHERE p.DisbursementDate IS NULL
SELECT * FROM #tmpReport
DROP TABLE #tmpReport

它返回如下结果:

+---------+-------+-----------+-----------------+----------+---------+
|  Claim  | HCPCS | ClaimItem | PaymentLineItem | Interest | Payment |
+---------+-------+-----------+-----------------+----------+---------+
| 4250692 | T4535 |   5552227 |         9425733 |     2.04 | 4104926 |
| 3677431 | L1820 |   4151237 |         8485098 |     2.63 | 3685465 |
| 3677431 | L1820 |   4151238 |         8485099 |     2.63 | 3685465 |
+---------+-------+-----------+-----------------+----------+---------+

在哪里我想使用cte并在插入记录时仅获得第一行。我曾尝试使用选择前1名,但记录更多。 预期结果应仅为前两行,其中第三行具有两个相同的记录,而第二个记录需要排除。

插入后的预期结果应仅在#tmpReport中插入以下两个记录:

+---------+-------+-----------+-----------------+----------+---------+
|  Claim  | HCPCS | ClaimItem | PaymentLineItem | Interest | Payment |
+---------+-------+-----------+-----------------+----------+---------+
| 4250692 | T4535 |   5552227 |         9425733 |     2.04 | 4104926 |
| 3677431 | L1820 |   4151237 |         8485098 |     2.63 | 3685465 |
+---------+-------+-----------+-----------------+----------+---------+

3 个答案:

答案 0 :(得分:3)

尝试一下:

DECLARE @fidelisInterestItems TABLE  (claim float,payment float,code varchar(50),interest money,paymentlineitem float);

INSERT INTO @fidelisInterestItems
Select 4250692,4104926,'T4535',2.04,9425737 Union All
Select 3677431,3685465,'L1820',2.63,8485100

CREATE TABLE #tmpReport(Claim int,HCPCS varchar(50),ClaimItem int,PaymentLineItem int,Interest money,Payment int)

with cte as 
(
Select P.Claim, ct.code, pl.claimItem, PL.PaymentLineItem,fi.Interest,P.Payment, 
ROW_NUMBER() over (partition by P.Claim, ct.code order by (select null)) as ranking 
-- Include other columns in `partition by` which have to be equal to omit the next record
FROM payment p with(nolock)
        JOIN paymentlineitem pl on p.payment=pl.payment 
        JOIN cpt ct on ct.cpt=pl.cpt 
        JOIN Eob e on e.eob=p.eob 
        JOIN Eobbatch eb on eb.eobbatch=e.eobbatch 
        JOIN @fidelisInterestItems fi on p.payment=fi.payment AND p.claim=fi.claim AND ct.code=fi.code 
        WHERE p.DisbursementDate IS NULL
)
Insert INTO #tmpReport
Select * from cte where ranking = 1 
SELECT * FROM #tmpReport
DROP TABLE #tmpReport

答案 1 :(得分:1)

如果我正确理解了该问题,则该方法应该起作用:

DECLARE @fidelisInterestItems TABLE  (claim float,payment float,code varchar(50),
interest money,paymentlineitem float);

INSERT INTO @fidelisInterestItems
Select 4250692,4104926,'T4535',2.04,9425737 Union All
Select 3677431,3685465,'L1820',2.63,8485100

CREATE TABLE #tmpReport(Claim int,HCPCS varchar(50),ClaimItem int,
PaymentLineItem int,Interest money,Payment int)

INSERT INTO #tmpReport
SELECT P.Claim, ct.code, pl.claimItem, PL.PaymentLineItem,fi.Interest,P.Payment
FROM payment p with(nolock)
        JOIN paymentlineitem pl on p.payment = pl.payment 
        JOIN cpt ct on ct.cpt = pl.cpt 
        JOIN Eob e on e.eob = p.eob 
        JOIN Eobbatch eb on eb.eobbatch = e.eobbatch 
        JOIN @fidelisInterestItems fi on p.payment = fi.payment 
            AND p.claim = fi.claim AND ct.code = fi.code 
        WHERE p.DisbursementDate IS NULL
            AND P.payment = 3685465

SELECT TOP 1 * FROM #tmpReport

DROP TABLE #tmpReport

但是,我仍然不知道第一行的条件是什么,例如ORDER BY pl.ClaimItem等。但是,也许没关系吗?

答案 2 :(得分:0)

您可以只使用row_number()

SELECT t.*
FROM (SELECT t.*, row_number() over (partition by Claim, code, claimItem, Interest, Payment order by PaymentLineItem) as seqnum
      FROM #tmpReport t
     ) t
WHERE seqnum = 1;

或聚合:

select Claim, code, claimItem, Interest, Payment, MIN(PaymentLineItem) as PaymentLineItem
from #tmpReport t
group by Claim, code, claimItem, Interest, Payment;