在SQL Server中运行该查询时,有没有人可以帮助我获取重复记录。我甚至尝试了左和右BILL_DETAILID
的两个场景中的内部联接,我得到了重复记录&也使用" distinct"但仍面临同样的问题。我想摆脱这个。
SELECT
BILL_DETAIL.id AS BILLDETAILID,
BILL_HEADER.id AS BILLHEADERID,
adjustment.id adjustmentID
FROM
BILLHEADER
INNER JOIN
BILL_DETAIL ON BILL_DETAIL.billdetail2billhdr = BILLHEADER.id
LEFT JOIN
adjustment ON adjustment.adjustment2bill_detail = BILLDETAIL.id
INNER JOIN
incident ON incident.case2billhdr = BILLHEADER.id
答案 0 :(得分:0)
试试这个
WITH CTE
AS
(
select
SeqNo = ROW_NUMBER() OVER(PARTITON BY BILL_DETAIL.id,BILL_HEADER.id ORDER BY adjustment.id DESC ),
BILL_DETAIL.id as BILLDETAILID,
BILL_HEADER.id as BILLHEADERID,
adjustment.id adjustmentID
from BILLHEADER
Inner JOIN BILL_DETAIL ON BILL_DETAIL.billdetail2billhdr=BILLHEADER.id
left join adjustment on adjustment.adjustment2bill_detail=BILLDETAIL.id
inner join incident on incident.case2billhdr=BILLHEADER.id
)
SELECT
*
FROM CTE
WHERE SeqNo = 1
这将返回每个BILL_DETAIL.id + BILL_HEADER.id组合的第一个adjust.id
答案 1 :(得分:0)
我想我明白你要做什么,试试:
<击> 撞击>
<击>select
BILL_DETAIL.id as BILLDETAILID,
BILL_HEADER.id as BILLHEADERID,
MAX(adjustment.id) as adjustmentID
from BILLHEADER
Inner JOIN BILL_DETAIL ON BILL_DETAIL.billdetail2billhdr=BILLHEADER.id
left join adjustment on adjustment.adjustment2bill_detail=BILLDETAIL.id
inner join incident on incident.case2billhdr=BILLHEADER.id
group by BILL_DETAIL.id,BILL_HEADER.id
击> <击> 撞击>
select billdetailid,
billheadid,
adjustmentid
from
(SELECT
BILL_DETAIL.id AS BILLDETAILID,
BILL_HEADER.id AS BILLHEADERID,
adjustment.id adjustmentID,
row_number() over(partition by bill_detail.id, bill_header.id order by adjustment.id desc) rn
FROM
BILLHEADER
INNER JOIN
BILL_DETAIL ON BILL_DETAIL.billdetail2billhdr = BILLHEADER.id
LEFT JOIN
adjustment ON adjustment.adjustment2bill_detail = BILLDETAIL.id
INNER JOIN
incident ON incident.case2billhdr = BILLHEADER.id) t1
where t1.rn = 1
答案 2 :(得分:0)
您可以尝试使用此脚本。
MySqlConnection